ArrayIndexOutOfBoundsException:RSA块的数据太多 [英] ArrayIndexOutOfBoundsException : too much data for RSA block

查看:228
本文介绍了ArrayIndexOutOfBoundsException:RSA块的数据太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some problem with my android application. I am trying to an app related with RSA encryption/decryption.this is my problem:

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

I can encrypt short sentences clearly, but when i try to decrypt this message to orginal text I give an error ("too much data for RSA block"). And also if I want to encrypt a long sentences i have same error.I had some search for this problem, and found some solution in this sites:

第1页

Site 2

站点3

但是我什么都不懂,这些解决方案都很复杂。我怎么能解决这个问题,任何人都可以给我一个更简单的解决方案?谢谢。

But i dont understand anything, these solutions are so complicated.How can i fixed this problem, Can anyone give me a more simple solution? Thank you.

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

EDİT: These are the code blocks that i use for this project.

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);
}


推荐答案

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

RSA can only encrypt messages that are several bytes shorter than the modulus of the key pair. The extra bytes are for padding, and the exact number depends on the padding scheme you are using.

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

RSA is for key transport, not data encryption. If you have a long message, encrypt it with AES, using a random key. Then encrypt the AES key with RSA, using the public key of the message recipient. You should be using the Cipher class's wrap() and unwrap() methods.

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

This is how PGP, S/MIME, TLS (roughly), and any other correctly designed RSA encryption schemes work.

这篇关于ArrayIndexOutOfBoundsException:RSA块的数据太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆