|
|
| View previous topic :: View next topic |
| Author |
Message |
dan hamilton Level 1

Joined: 04 Apr 2006 Posts: 1
|
Posted: Tue Apr 04, 2006 6:21 am Post subject: RSA: Bad padding exception |
|
|
Hi guys, probably missing something very straight forward on this, but please forgive me, I'm very naive!
Have a client server application where the client identifies its self with an RSA encrypted username & password.
Unfortunately I'm getting a "bad padding exception: data must start with zero" when i try to decrypt with the private key on the server side.
I'm fairly sure the key is correct as I have tested encrypting with public key then decrypting with private key on the client side with no problems at all. Just seems when I transfer it over the connection it messses it up somehow?!
Using PrintWriter & BufferedReader on the sockets if thats of importance.
EncodeBASE64 & DecodeBASE64 encode byte[] to 64base and vice versa respectively.
Any ideas guys??
Client CODE
//Read server request for username
String serverMessage = in.readLine();
System.out.println(serverMessage);
// retrieve username from file
String userName = "d";
byte [] plainText = userName.getBytes("UTF8");
// retrieve public key from file
System.out.print("Getting Public RSA Key from file: ");
FileInputStream keyfis = new FileInputStream("Public Encoded Key");
byte[] encKey = new byte[keyfis.available()];
keyfis.read(encKey);
keyfis.close();
System.out.println("OK");
//rebuild key
System.out.print("Regenerating Key: ");
KeyFactory factory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(encKey);
Key myKey = factory.generatePublic(keySpec);
System.out.println("OK");
// encrypt username
System.out.print("Encrypting Username: ");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, myKey);
byte[] cipherText = cipher.doFinal(plainText);
System.out.println("OK");
String encryptedText = encodeBASE64(cipherText);
out.println(encryptedText);
Server Code
// Retrieve private key from file
FileInputStream keyfis = new FileInputStream("Private Encoded Key");
byte[] encKey = new byte[keyfis.available()];
keyfis.read(encKey);
keyfis.close();
// Rebuild the key
KeyFactory factory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encKey);
Key myKey = factory.generatePrivate(keySpec);
System.out.println("Got Key From File!");
// Request, read & format user name.
os.println("RequestUserName");
System.out.println("Username requested");
String cipherString = is.readLine();
System.out.println(cipherString);
System.out.println("\nBase 64 version"+ cipherString);
byte [] cipheredMessage = decodeBASE64(cipherString);
System.out.println("\nByte version" + cipheredMessage);
// decipher username
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, myKey);
byte[] newPlainText = cipher.doFinal(cipheredMessage);
System.out.println("Decrypted Username:" + new String(newPlainText, "UTF8") );
many thanks! |
|
| Back to top |
|
 |
joearunkumar Level 1

Joined: 02 Feb 2011 Posts: 1 Location: India
|
Posted: Wed Feb 02, 2011 1:58 am Post subject: Re: RSA: Bad padding exception |
|
|
I wanted to encrypt and decrypt using RSA. i got the value of m and e from both the private and public key. when i recreate the public and private key using the value of m and e, i get a padding exception saying that my message is longer than modulus. pls help me solve this.
My code.
import java.math.BigInteger;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.*;
import sun.misc.BASE64Encoder;
public class eg1 {
public static void main (String[] args) throws NoSuchAlgorithmException,InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException,BadPaddingException, NoSuchPaddingException
{
try {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(512, random);
KeyPair pair = keyGen.generateKeyPair();
//PrivateKey priv = pair.getPrivate();
//PublicKey pub = pair.getPublic();
BASE64Encoder b64 = new BASE64Encoder();
String str=b64.encode(pub);
KeyFactory fact = KeyFactory.getInstance("RSA");
RSAPublicKeySpec pub = fact.getKeySpec(pair.getPublic(), RSAPublicKeySpec.class);
RSAPrivateKeySpec priv = fact.getKeySpec(pair.getPrivate(), RSAPrivateKeySpec.class);
String str1 = "" + pub.getModulus();
String str2 = "" + priv.getPrivateExponent();
String str3 = "" + pub.getPublicExponent();
System.out.println("n: " + str1);
System.out.println("d: " + str2);
System.out.println("e: " + str3);
BigInteger n = new BigInteger(str1);
BigInteger d = new BigInteger(str2);
BigInteger e = new BigInteger(str3);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(n, e);
//KeyFactory fact = KeyFactory.getInstance("RSA");
PublicKey pubKey = fact.generatePublic(keySpec);
RSAPrivateKeySpec keySp = new RSAPrivateKeySpec(d, e);
PrivateKey privKey = fact.generatePrivate(keySp);
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
rsaCipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cleartext = null;
cleartext = "Hai".getBytes();
System.out.println("The original text is: " + cleartext.toString());
byte[] ciphertext = null;
ciphertext = rsaCipher.doFinal(cleartext);
System.out.println("The encrypted text is: " + ciphertext.toString());
rsaCipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] cleartext1 = rsaCipher.doFinal(ciphertext);
String theString = new String(cleartext1);
System.out.println(theString);
} catch (InvalidKeySpecException ex) {
Logger.getLogger(eg1.class.getName()).log(Level.SEVERE, null, ex);
}
}
} |
|
| Back to top |
|
 |
|
|