ArrayIndexOutOfBoundsException:RSA块的数据太多

Bah*_*maz 6 java android rsa

我的Android应用程序有一些问题.我正在尝试与RSA加密/解密相关的应用程序.这是我的问题:

我可以清楚地加密短句,但是当我尝试将此消息解密为原始文本时,我会给出错误("RSA块的数据太多").而且如果我想加密一个长句子我有同样的错误.我有一些搜索这个问题,并在这个网站找到了一些解决方案:

网站1

网站2

网站3

但我什么都不懂,这些解决方案都很复杂.我怎么能解决这个问题呢,有人能给我一个更简单的解决方案吗?谢谢.

EDİT:这些是我用于此项目的代码块.

public String RSAEncrypt(String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {

    publicKey = getPublicKey();
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] cipherData = cipher.doFinal(plain.getBytes());
    return Base64.encodeToString(cipherData, Base64.DEFAULT);
}

public String RSADecrypt(byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {

    privateKey = getPrivateKey();
    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, privateKey);      
    byte[] cipherData = cipher.doFinal(encryptedBytes);
    return Base64.encodeToString(cipherData, Base64.DEFAULT);
}
Run Code Online (Sandbox Code Playgroud)

eri*_*son 21

RSA只能加密比密钥对的模数短几个字节的消息.额外的字节用于填充,确切的数字取决于您使用的填充方案.

RSA用于密钥传输,而不是数据加密.如果您有长消息,请使用随机密钥使用AES对其进行加密.然后使用RSA使用消息接收者的公钥加密AES密钥.你应该使用Cipherwrap()unwrap()方法.

这就是PGP,S/MIME,TLS(粗略)以及任何其他正确设计的RSA加密方案的工作原理.