Java的Android的错误"对于RSA块QUOT太多的数据; [英] Java Android Error "too much data for RSA block"
问题描述
一个在我的Android项目(RSA加密/解密)的错误。
加密传递OK,但是当我试图解密加密的文本,yhere是一个错误:为RSA块的数据太多
如何解决这个问题呢?
code:
公共字符串解密(字符串文本)抛出异常
{
尝试{
Log.i(Crypto.java:Decrypt,文本);
RSAPrivateKey privateKey =(RSAPrivateKey)kp.getPrivate();
密码加密= Cipher.getInstance(RSA / ECB / NoPadding);
cipher.init(Cipher.DECRYPT_MODE,privateKey);
字节[] =的CipherData cipher.doFinal(text.getBytes()); //< ----错误:RSA块数据太多
字节[] = decryptedBytes cipher.doFinal(的CipherData);
字符串解密=新的String(decryptedBytes); Log.i(解密,解密); 返回解密;
}赶上(例外五){
的System.out.println(e.getMessage());
}
返回null;
}
您的问题是,你需要连接code /解code密文(只文本
在code)如果您想使用文本重新presentation运送它(字符串
你的情况)。
试着查找基地64编码在这个网站,应该有很多关于它的信息。恩code解密之前加密和德code之后。你也应该指定您的明文特定的字符编码。
最后,你应该可能与对称密码进行加密,并使用RSA加密对称密钥。否则,你可能会耗尽的RSA计算之内的空间,因为公共密钥无法加密比模量(密钥大小)更大的数据。
A have an error in my Android project (RSA encryption/decryption). The encryption passes OK, but when I trying to decrypt encrypted text, yhere are an error: "too much data for RSA block".
How to solve this problem?
code:
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;
}
Your issue is that you need to encode/decode the ciphertext (just text
in your code) if you want to transport it using a textual representation (String
in your case).
Try and look up base 64 encoding on this site, there should be a lot of information about it. Encode after encryption and decode before decryption. You should also specify a specific character encoding for your plaintext.
Finally, you should probably encrypt with a symmetric cipher, and encrypt the symmetric key using RSA. Otherwise you may run out of space within the RSA calculation, because a public key cannot encrypt data larger than its modulus (key size).
这篇关于Java的Android的错误&QUOT;对于RSA块QUOT太多的数据;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!