Last active
December 1, 2018 21:10
-
-
Save thedumbtechguy/c17e6c3b2098a9c5d04d to your computer and use it in GitHub Desktop.
Revisions
-
thedumbtechguy revised this gist
Sep 4, 2014 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,9 @@ ---Java //I used this on Android //before you proceed, you need to ensure your private key is PKCS8 since that is what can be read natively in java. //If your key begins with -----BEGIN RSA PRIVATE KEY-----, the it is ssleay and you need to convert it using the openssl command below //openssl pkcs8 -topk8 -inform pem -in ssleay-private-key.key -outform pem -nocrypt -out pkcs8-private-key.pem final private static String RSA_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" + -
thedumbtechguy revised this gist
Sep 4, 2014 . 2 changed files with 18 additions and 18 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,3 @@ ---Java This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ <?php function encrypt($string) { $key = "-----BEGIN PUBLIC KEY----- MIGfM.... gUn.... 7eMl.... yXd.... -----END PUBLIC KEY-----"; openssl_public_encrypt($string, $crypted, $key); return base64_encode($crypted); } -
thedumbtechguy renamed this gist
Sep 4, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
thedumbtechguy created this gist
Sep 4, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ <?php function encrypt($string) { $key = "-----BEGIN PUBLIC KEY----- MIGfM.... gUn.... 7eMl.... yXd.... -----END PUBLIC KEY-----"; openssl_public_encrypt($string, $crypted, $key); return base64_encode($crypted); } ---Java //I used this on Android //before you proceed, you need to ensure your private key is PKCS8 since that is what can be read natively in java. //If your key begins with -----BEGIN RSA PRIVATE KEY-----, you need to convert it using the openssl command below //openssl pkcs8 -topk8 -inform pem -in file.key -outform pem -nocrypt -out file.pem final private static String RSA_PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\n" + "MI...\n" + "2...\n" + "HPt...\n" + ... "v3...\n" + "-----END PRIVATE KEY-----"; public static byte[] decrypt(String key) throws Exception { String privKeyPEM = RSA_PRIVATE_KEY.replace("-----BEGIN PRIVATE KEY-----\n", ""); privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", ""); byte [] encoded = Base64.decode(privKeyPEM, Base64.DEFAULT); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey privKey = kf.generatePrivate(keySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] decodedStr = Base64.decode(key, Base64.DEFAULT); byte[] plainText = cipher.doFinal(decodedStr); return plainText; }