too much data for RSA block
2012-12-13 08:28
405 查看
http://objectmix.com/java/76634-too-much-data-rsa-block.html
Hallo,
i have a problem with my RSA implementation in Java... if i have a
string which is no longer then 64 Signs is there any problem. But as
soon as a string is longer than 64 Signs i get an Exception about
this:
´-----------------------------
| java.lang.ArrayIndexOutOfBoundsException: too much data for RSA
block
| at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(JCERSACipher.java:271)
| at javax.crypto.Cipher.doFinal(DashoA6275)
| at allesineinem.main(allesineinem.java:38)
Exception in thread "main"
`-----------------------------
Here is my code for encrypt and decrypt a String with RSA and
bouncycastleProvider... I hope someone can help me and say where is
the problem
----------------------------------------------------
import java.security.*;
import javax.crypto.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* @author Yildirim
*
* Folgendes auswählen, um die Schablone für den erstellten
Typenkommentar zu ändern:
* Fenster>Benutzervorgaben>Java>Codegenerierung>Code und
Kommentare
*/
public class allesineinem
{
public static void main(String[] args) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator RSAkeyPairGen =
KeyPairGenerator.getInstance("RSA");
SecureRandom sunSha1prngSecRand =
SecureRandom.getInstance("SHA1PRNG","SUN");
sunSha1prngSecRand.setSeed(System.currentTimeMillis());
RSAkeyPairGen.initialize(512,sunSha1prngSecRand);
KeyPair rsaKeyPair = RSAkeyPairGen.generateKeyPair();
//System.out.println(RSAkeyPairGen);
PrivateKey rsaPrivKey = rsaKeyPair.getPrivate();
PublicKey rsaPubKey = rsaKeyPair.getPublic();
Cipher cipher = Cipher.getInstance("RSA","BC");
cipher.init(Cipher.ENCRYPT_MODE,rsaPubKey);
String text = "Hallo wie gehs? Danke.. mir gehts gut..was treibst
du denn so? na";
//String text = "0123456789012345678901234567890123456789012345678901234567890123";
byte[] buf1 = text.getBytes();
byte[] buf = cipher.doFinal(buf1);
for (int i=0;i<buf.length;i++)
{
//System.out.print(buf[i]+" ");
}
System.out.println();
//System.out.print(toHumanReadableString(buf));
String string1 = toHumanReadableString(buf);
System.out.println();
//System.out.print(string1+" ");
System.out.println();
byte[] hallo1 = string1.getBytes();
for (int i=0;i<buf.length;i++)
{
//System.out.print(hallo1[i]+" ");
}
System.out.println();
//System.out.println(string1.getBytes());
cipher.init(Cipher.DECRYPT_MODE,rsaPrivKey);
//System.out.println(buf);
byte[] buf2 = cipher.doFinal(buf);
System.out.println(toHumanReadableString(buf2));
}
public static String toHumanReadableString(byte[] bytes)
{
return((new String(bytes)));
}
}
-----------------------------------------------
thanks
Ümit Yildirim
PS: sorry because of my bad english
Reply With Quote
08-20-2004 11:38 AM
#2
MatheGeniee@gmx.de (?mit Yildirim) wrote in message news:<4a2b6107.0408170147.77789ddb@posting.google.com>...
> Hallo,
>
> i have a problem with my RSA implementation in Java... if i have a
> string which is no longer then 64 Signs is there any problem. But as
> soon as a string is longer than 64 Signs i get an Exception about
> this:
BouncyCastle RSA gives an exception if you try to encrypt a block
which is longer than the key size. In your case the key size is 512
== 64 bytes. I think the only work around is to break your text into
64 byte blocks.
> RSAkeyPairGen.initialize(512,sunSha1prngSecRand);
Also 512 is pretty short for an RSA key, better to use 1024 or 2048.
And of course, the usual advice is to use RSA only for sending a
symmetric key, which is then used for encrypting the rest of the data.
-- Russ
Hallo,
i have a problem with my RSA implementation in Java... if i have a
string which is no longer then 64 Signs is there any problem. But as
soon as a string is longer than 64 Signs i get an Exception about
this:
´-----------------------------
| java.lang.ArrayIndexOutOfBoundsException: too much data for RSA
block
| at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(JCERSACipher.java:271)
| at javax.crypto.Cipher.doFinal(DashoA6275)
| at allesineinem.main(allesineinem.java:38)
Exception in thread "main"
`-----------------------------
Here is my code for encrypt and decrypt a String with RSA and
bouncycastleProvider... I hope someone can help me and say where is
the problem
----------------------------------------------------
import java.security.*;
import javax.crypto.*;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
/**
* @author Yildirim
*
* Folgendes auswählen, um die Schablone für den erstellten
Typenkommentar zu ändern:
* Fenster>Benutzervorgaben>Java>Codegenerierung>Code und
Kommentare
*/
public class allesineinem
{
public static void main(String[] args) throws Exception
{
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator RSAkeyPairGen =
KeyPairGenerator.getInstance("RSA");
SecureRandom sunSha1prngSecRand =
SecureRandom.getInstance("SHA1PRNG","SUN");
sunSha1prngSecRand.setSeed(System.currentTimeMillis());
RSAkeyPairGen.initialize(512,sunSha1prngSecRand);
KeyPair rsaKeyPair = RSAkeyPairGen.generateKeyPair();
//System.out.println(RSAkeyPairGen);
PrivateKey rsaPrivKey = rsaKeyPair.getPrivate();
PublicKey rsaPubKey = rsaKeyPair.getPublic();
Cipher cipher = Cipher.getInstance("RSA","BC");
cipher.init(Cipher.ENCRYPT_MODE,rsaPubKey);
String text = "Hallo wie gehs? Danke.. mir gehts gut..was treibst
du denn so? na";
//String text = "0123456789012345678901234567890123456789012345678901234567890123";
byte[] buf1 = text.getBytes();
byte[] buf = cipher.doFinal(buf1);
for (int i=0;i<buf.length;i++)
{
//System.out.print(buf[i]+" ");
}
System.out.println();
//System.out.print(toHumanReadableString(buf));
String string1 = toHumanReadableString(buf);
System.out.println();
//System.out.print(string1+" ");
System.out.println();
byte[] hallo1 = string1.getBytes();
for (int i=0;i<buf.length;i++)
{
//System.out.print(hallo1[i]+" ");
}
System.out.println();
//System.out.println(string1.getBytes());
cipher.init(Cipher.DECRYPT_MODE,rsaPrivKey);
//System.out.println(buf);
byte[] buf2 = cipher.doFinal(buf);
System.out.println(toHumanReadableString(buf2));
}
public static String toHumanReadableString(byte[] bytes)
{
return((new String(bytes)));
}
}
-----------------------------------------------
thanks
Ümit Yildirim
PS: sorry because of my bad english
Reply With Quote
08-20-2004 11:38 AM
#2
Re: too much data for RSA block
MatheGeniee@gmx.de (?mit Yildirim) wrote in message news:<4a2b6107.0408170147.77789ddb@posting.google.com>...> Hallo,
>
> i have a problem with my RSA implementation in Java... if i have a
> string which is no longer then 64 Signs is there any problem. But as
> soon as a string is longer than 64 Signs i get an Exception about
> this:
BouncyCastle RSA gives an exception if you try to encrypt a block
which is longer than the key size. In your case the key size is 512
== 64 bytes. I think the only work around is to break your text into
64 byte blocks.
> RSAkeyPairGen.initialize(512,sunSha1prngSecRand);
Also 512 is pretty short for an RSA key, better to use 1024 or 2048.
And of course, the usual advice is to use RSA only for sending a
symmetric key, which is then used for encrypting the rest of the data.
-- Russ
相关文章推荐
- Too much data for RSA block fail. What is PKCS#7?
- too much data for RSA block .关于RSA算法密钥长度/密文长度/明文长度
- too much data for RSA block .关于RSA算法密钥长度/密文长度/明文长度
- RSA 解密的时候报错 而且有乱码:java.lang.ArrayIndexOutOfBoundsException: too much data for RSA block
- MongoDB:too much data for sort() with no index error异常
- mongodb too much data for sort() with no index
- mongodb 查询时没有索引报错(too much data for sort() with no index)
- Mongodb查询提示com.MongoDB.MongoException: too much data for sort() with no index
- PHP执行insert语句报错“Data too long for column”解决办法
- Data Access Application Block for .NET
- Data Access Application Block for .NET v2
- 成功解决data too long for column 'name' at row 1
- (汉字问题1)mysql中data too long for column问题的解决方法
- 在程序中写了一段代码向数据库中插入数据,出现了data too long for column 'name' at row 1的问题。
- Data Access Application Block for .NET
- GCC编译“Value too large for defined data type”错误解决办法
- ### Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'articleTit
- Linux mount Windows共享后编译出现“Value too large for defined data type”的问题 (zz)
- 成功解决data too long for column 'name'
- mysql建表时指定编码,避免Data too long for column.....的异常