Good Afternoon All!
I have put some code together that will take a string, break it into 16 byte pieces, encrypt it using RSA encryption, an then combine all of the pieces.
The problem is that when I try to decrypt it I get the exception "javax.crypto.BadPaddingException: unknown block type"
(I have to use RSA, I am interacting with other code that uses it, I don't get to decide.)
Can anyone help?
Thanks,
Doug
Here is the code:
public byte[] encryptdecrypt(String text, Key key, int cipherMode)
{
byte[] c = null;
//byte[] result = new byte[0];
byte[] result = null;
InputStream inputReader = null;
try
{
logger.debug("encryptdecrypt try block");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
String textLine = null;
byte[] buf = cipherMode == Cipher.ENCRYPT_MODE? new byte[16] : new byte[128];
//byte[] buf = new byte[16];
int bufl;
// init the Cipher object for Encryption...
logger.debug("Start Cipher init");
cipher.init(cipherMode, key);
logger.debug("Cipher inint done, now init inputstream");
inputReader = new ByteArrayInputStream( text.getBytes() );
logger.debug("inputreader init, start while block");
while ( (bufl = inputReader.read(buf)) != -1)
{
byte[] encText = null;
if (cipherMode == Cipher.ENCRYPT_MODE)
{
encText = encrypt(copyBytes(buf,bufl),(PublicKey)key);
logger.debug("Cipher mode is Encrypt");
}
else
{
logger.debug("buf = " + new String(buf));
encText = decrypt(copyBytes(buf,bufl),(PrivateKey)key);
logger.debug("Cipher mode is decrypt encText is" + new String(encText));
}
logger.debug("Call Copy");
result = concat(result, encText);
}
}
catch (Exception e)
{
logger.debug(e.toString());
//throw e;
}
return result;
}
public byte[] concat (byte[] a, byte[] b)
{
byte[] c = null;
logger.debug("concat call");
if(a == null)
{
logger.debug("concat if statement, a is null");
c = new byte[b.length];
logger.debug("concat c is created as b length");
System.arraycopy (b, 0, c, 0, b.length);
logger.debug("concat null array copy is done");
}
else
{
logger.debug("concat else statement, a is NOT null");
c = new byte[a.length + b.length];
System.arraycopy (a, 0, c, 0, a.length);
System.arraycopy (b, 0, c, a.length, b.length);
}
logger.debug("new byte array created");
return c;
}
public static byte[] copyBytes(byte[] arr, int length)
{
logger.debug("Start copyBytes call");
byte[] newArr = null;
if (arr.length == length)
{
logger.debug("inside if");
newArr = arr;
}
else
{
logger.debug("inside else");
newArr = new byte[length];
for (int i = 0; i < length; i++)
{
logger.debug("in for loop");
newArr[i] = (byte) arr;
}
}
return newArr;
}
public static byte[] encrypt(byte[] text, PublicKey key) throws Exception
{
byte[] cipherText = null;
try
{
//
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
// out.println("\nProvider is: " + cipher.getProvider().getInfo());
// out.println("\nStart encryption with public key");
// encrypt the plaintext using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text);
logger.debug("RSA encryption of "+ text.length+"-byte input is "+ cipherText.length+" bytes long.");
}
catch (Exception e)
{
logger.debug(e.toString());
//throw e;
}
return cipherText;
}
public static byte[] decrypt(byte[] text, PrivateKey key) throws Exception
{
logger.debug("start decrypt call");
logger.debug("Test is" + text.length+" bytes long.");
byte[] dectyptedText = null;
try
{
// decrypt the text using the private key
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", "BC");
// out.println("Start decryption");
cipher.init(Cipher.DECRYPT_MODE, key);
logger.debug("Rerady to decrypt");
dectyptedText = cipher.doFinal(text);
logger.debug("decryption done");
}
catch (Exception e)
{
logger.debug(e.toString());
//throw e;;
}
return dectyptedText;
}