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
-
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