使用Cipher.doFinal()进行RSA解密不会恢复原始的纯文本

时间:2018-10-18 07:04:28

标签: android encryption rsa

从原始资源文件生成公钥/私钥对,并使用它们在Android应用中进行加密/解密非常简单。 但是,以下代码在VirtualBox中与Android-x86-v4.4.4模拟器一起运行时,无法正确恢复纯文本。任何人都可以指出此代码出了什么问题(它不会产生任何错误或生成任何异常): (更改为Cipher.getInstance(“ RSA / NONE / NoPadding”)也没有帮助)

    PublicKey mPublicKey = null;
    PrivateKey mPrivateKey = null;

    String mPlainText = "The quick brown fox jumped over the lazy dog" ;
    byte[] mEncryptText = null;
    byte[] mDecryptText = null;


    try {
        InputStream mIS = getResources().openRawResource(R.raw.test1_public_key);
        DataInputStream dis = new DataInputStream(mIS);
        byte [] keyBytes = new byte [(int) mIS.available()];
        dis.readFully(keyBytes);
        dis.close();
        mIS.close();
        X509EncodedKeySpec mX509KeySpec = new X509EncodedKeySpec(keyBytes);
        mPublicKey = (KeyFactory.getInstance("RSA")).generatePublic(mX509KeySpec);
        Toast.makeText(this, "Publickey generated", Toast.LENGTH_LONG).show();
    }
    catch(Exception e){
        Log.e("onButtondecrypt", "exception", e);
        Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
    }

    try {
        InputStream mIS = getResources().openRawResource(R.raw.test1_private_key);
        DataInputStream dis = new DataInputStream(mIS);
        byte [] keyBytes = new byte [(int) mIS.available()];
        dis.readFully(keyBytes);
        dis.close();
        mIS.close();
        PKCS8EncodedKeySpec mPKCS8keySpec = new PKCS8EncodedKeySpec(keyBytes);
        mPrivateKey = (KeyFactory.getInstance("RSA")).generatePrivate(mPKCS8keySpec);
        Toast.makeText(this, "PRIVATE key generated", Toast.LENGTH_LONG).show();
    }
    catch(Exception e){
        Log.e("onButtondecrypt", "exception", e);
        Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
    }

    Toast.makeText(this, mPlainText, Toast.LENGTH_LONG).show();

    Toast.makeText(this, "Encrypting with Publickey ...", Toast.LENGTH_LONG).show();
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, mPublicKey);
        mEncryptText = cipher.doFinal(mPlainText.getBytes());
        Toast.makeText(this, mEncryptText.toString(), Toast.LENGTH_LONG).show();
    }
    catch(Exception e){
        Log.e("onButtondecrypt", "exception", e);
        Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
    }

    Toast.makeText(this, "Decrypting with PRIVATE key ...", Toast.LENGTH_LONG).show();
    try {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, mPrivateKey);
        mDecryptText = cipher.doFinal(mEncryptText);
        Toast.makeText(this, mDecryptText.toString(), Toast.LENGTH_LONG).show();
    }
    catch(Exception e){
        Log.e("onButtondecrypt", "exception", e);
        Log.e("onButtondecrypt", "exception: " + Log.getStackTraceString(e));
    }

感谢所有人。

2 个答案:

答案 0 :(得分:0)

将字符串转换为byte []的方式是错误的,反之亦然。正确的方法是

byte[] st = txt.getBytes("UTF-8");

String s = new String(bytes);

因此,加密/解密正确,只有吐司显示不正确。

答案 1 :(得分:-1)

public static String encrypt(String value, String key) {

    String encrypted = null;
    try {
        byte[] value_bytes = value.getBytes("UTF-8");
        byte[] key_bytes = getKeyBytes(key);
        encrypted = Base64.encodeToString(encrypt(value_bytes, key_bytes, key_bytes), 0);
    } catch (Exception ex) {
        ExceptionRecord.catchException(ex);

    }
    return encrypted;
}

public static byte[] encrypt(byte[] paramArrayOfByte1, byte[] paramArrayOfByte2, byte[] paramArrayOfByte3)
{
    byte[] byteencrypt = new byte[0];
    try {
        // setup AES cipher in CBC mode with PKCS #5 padding
        Cipher localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // encrypt
        localCipher.init(1, new SecretKeySpec(paramArrayOfByte2, "AES"), new IvParameterSpec(paramArrayOfByte3));
        byteencrypt = localCipher.doFinal(paramArrayOfByte1);
    }catch (Exception ex)
    {
        ExceptionRecord.catchException(ex);
    }
    return byteencrypt;
}

/**
 *
 * @param value data to decrypt
 * @param key a secret key used for encryption
 * @return String result after decryption
 * @throws KeyException
 * @throws GeneralSecurityException
 * @throws GeneralSecurityException
 * @throws InvalidAlgorithmParameterException
 * @throws IllegalBlockSizeException
 * @throws BadPaddingException
 * @throws IOException
 */
public static String decrypt(String value, String key) {
    String decrypted = null;
    try {
        byte[] value_bytes = Base64.decode(value, 0);
        byte[] key_bytes = getKeyBytes(key);
        decrypted = new String(decrypt(value_bytes, key_bytes, key_bytes), "UTF-8");
    } catch (Exception ex) {
        ExceptionRecord.catchException(ex);

    }
    return decrypted;
}

public static byte[] decrypt(byte[] ArrayOfByte1, byte[] ArrayOfByte2, byte[] ArrayOfByte3)
{
    byte[] bytedecrypt = new byte[0];
    try {
        // setup AES cipher in CBC mode with PKCS #5 padding
        Cipher localCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // decrypt
        localCipher.init(2, new SecretKeySpec(ArrayOfByte2, "AES"), new IvParameterSpec(ArrayOfByte3));
        bytedecrypt = localCipher.doFinal(ArrayOfByte1);
    }catch (Exception ex)
    {
        ExceptionRecord.catchException(ex);
    }
    return bytedecrypt;
}

private static byte[] getKeyBytes(String paramString)
{
    byte[] keybytes = new byte[0];
    try {
        byte[] arrayOfByte1 = new byte[16];
        byte[] arrayOfByte2 = paramString.getBytes("UTF-8");
        System.arraycopy(arrayOfByte2, 0, arrayOfByte1, 0, Math.min(arrayOfByte2.length, arrayOfByte1.length));
        keybytes = arrayOfByte1;
    }
    catch (Exception ex)
    {
        ExceptionRecord.catchException(ex);
    }
    return  keybytes;
}