Skip to content

Instantly share code, notes, and snippets.

@thedumbtechguy
Last active December 1, 2018 21:10
Show Gist options
  • Save thedumbtechguy/c17e6c3b2098a9c5d04d to your computer and use it in GitHub Desktop.
Save thedumbtechguy/c17e6c3b2098a9c5d04d to your computer and use it in GitHub Desktop.

Revisions

  1. thedumbtechguy revised this gist Sep 4, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions Decrypt.java
    Original 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-----, you need to convert it using the openssl command below
    //openssl pkcs8 -topk8 -inform pem -in file.key -outform pem -nocrypt -out file.pem
    //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" +
  2. thedumbtechguy revised this gist Sep 4, 2014. 2 changed files with 18 additions and 18 deletions.
    18 changes: 0 additions & 18 deletions Encrypt Decrypt.php → Decrypt.java
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,3 @@
    <?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
    18 changes: 18 additions & 0 deletions Encrypt.php
    Original 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);
    }

  3. thedumbtechguy renamed this gist Sep 4, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. thedumbtechguy created this gist Sep 4, 2014.
    56 changes: 56 additions & 0 deletions Encrypt Decrypt
    Original 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;
    }