Java Android错误“RSA块的数据太多”

A在我的Android项目中有错误(RSA加密/解密)。 加密通过OK,但是当我尝试解密加密文本时,有一个错误: “RSA块的数据太多”

如何解决这个问题呢?

码:

public String Decrypt(String text) throws Exception { try{ Log.i("Crypto.java:Decrypt", text); RSAPrivateKey privateKey = (RSAPrivateKey)kp.getPrivate(); Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] cipherData = cipher.doFinal(text.getBytes());// <----ERROR: too much data for RSA block byte[] decryptedBytes = cipher.doFinal(cipherData); String decrypted = new String(decryptedBytes); Log.i("Decrypted", decrypted); return decrypted; }catch(Exception e){ System.out.println(e.getMessage()); } return null; } 

您的问题是,如果要使用文本表示(在您的情况下为String )传输密文,则需要对密文进行编码/解码(只需在代码中添加文本)。

尝试在这个站点上查找base 64编码,应该有很多关于它的信息。 在解密之前加密和解码后编码。 您还应该为明文指定特定的字符编码。

最后,您应该使用对称密码进行加密,并使用RSA加密对称密钥。 否则,您可能会在RSA计算中耗尽空间,因为公钥无法加密大于其模数(密钥大小)的数据。