ideawu / Objective-C-RSA

Doing RSA encryption and decryption with Objective-C on iOS
http://www.ideawu.com/blog/post/132.html
Other
1.15k stars 280 forks source link

Android sample #44

Closed MujasSam closed 6 years ago

MujasSam commented 6 years ago

How to decrypt files/text in Android client?

ideawu commented 6 years ago

Sorry, I can't help, this project only works for Objective-C.

MujasSam commented 6 years ago

package com.test;

/**

import android.text.TextUtils; import android.util.Base64; import android.util.Log;

import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.security.InvalidKeyException; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;

import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException;

public class RSAEncryption {

public static final String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";
//public static final String PROVIDER = "BC";
public static final int BASE64_FLAG = Base64.DEFAULT;
private static Cipher cipher;

/**
 * Create and return the PublicKey object from the public key bytes
 */
private static PublicKey getPublicKeyNew(String certificateString) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {

    KeyFactory keyFactory;
    if (!TextUtils.isEmpty(certificateString)) {
        certificateString = certificateString
                .replace("-----BEGIN PUBLIC KEY-----\n", "")
                .replace("-----END PUBLIC KEY-----", "")
                .replaceAll("\\s", "").trim();
        Log.e("Public Key ", certificateString);
        byte[] certificateData = Base64.decode(certificateString, Base64.DEFAULT);
        keyFactory = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(certificateData);
        return keyFactory.generatePublic(keySpec);
    }

    return null;
}

/**
 * New Encryption
 */

public void encryptFile(byte[] input, File output)
        throws IOException, GeneralSecurityException {
    this.cipher = Cipher.getInstance(RSA_ALGORITHM);
    String pemString = SharedPrefSingleton.getInstance().getPublicKey();
    PublicKey publicKey = getPublicKeyNew(pemString);
    this.cipher.init(Cipher.ENCRYPT_MODE, publicKey);

    writeToFile(output, this.cipher.doFinal(input));
}

/*public void decryptFile(File output, byte[] input)
        throws IOException, GeneralSecurityException {
    try {
        this.cipher = Cipher.getInstance(RSA_ALGORITHM);
        this.cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
    } catch (Exception e) {
        e.printStackTrace();
    }
    writeToFile(output, this.cipher.doFinal(input));
}*/

private void writeToFile(File output, byte[] toWrite)
        throws IllegalBlockSizeException, BadPaddingException, IOException {
    FileOutputStream fos = new FileOutputStream(output);
    fos.write(toWrite);
    fos.flush();
    fos.close();
}

/*------------------------------------------------------------
            Text Encription and Decryption
-------------------------------------------------------------*/

/***
 *
 *
 * @param msg
 * @return
 * @throws NoSuchAlgorithmException
 * @throws NoSuchPaddingException
 * @throws UnsupportedEncodingException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws InvalidKeyException
 * @throws NoSuchProviderException
 */
public static String encryptText(String msg)
        throws NoSuchAlgorithmException, NoSuchPaddingException,
        UnsupportedEncodingException, IllegalBlockSizeException,
        BadPaddingException, InvalidKeyException, NoSuchProviderException {
    cipher = Cipher.getInstance(RSA_ALGORITHM);
    String pemString = SharedPrefSingleton.getInstance().getPublicKey();
    try {
        cipher.init(Cipher.ENCRYPT_MODE, getPublicKeyNew(pemString));
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    byte[] encrypted = cipher.doFinal(msg.getBytes());
    return Base64.encodeToString(encrypted,Base64.DEFAULT);
}

/*public String decryptText(String msg) throws InvalidKeyException, UnsupportedEncodingException,
        IllegalBlockSizeException, BadPaddingException {
    try {
        this.cipher = Cipher.getInstance(RSA_ALGORITHM);
        this.cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new String(cipher.doFinal(Base64.decode(msg, Base64.DEFAULT)));
}*/

}

MujasSam commented 6 years ago

I Have solved the issue for Android and published the code in git in reply.

Best Regards,

Mujasam BN | Technical Lead - Mobility | Cell: +91 9986790097

*DreamOrbit *A Deloitte Technology Fast 50 Company http://dreamorbit.com/deloitte-recognized-dreamorbit-among-top-500-emerging-technology-company-in-india/ Upcoming Holiday : none | Next Planned Leave : none

On Tue, Dec 5, 2017 at 9:52 AM, ideawu notifications@github.com wrote:

Sorry, I can't help, this project only works for Objective-C.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ideawu/Objective-C-RSA/issues/44#issuecomment-349191219, or mute the thread https://github.com/notifications/unsubscribe-auth/ANPVJCXzzy0Y54nmZrXlu7JTOPegii1wks5s9MUHgaJpZM4Q0IbO .