Reputation: 538
Hello I have to encrypt and decrypt a String. This is the method that I am using
/** Utility method to Encrpyt a plain text string using blowfish algorithm. This method is synchronised between threads.
* Base64 encoding is used to encode and decode byte array.
* <p>NOTE: use the same key for Encryption and Decryption</p>
*
* @param plainText Plain text String
* @param key Secret key ( If null default will be used)
* @return String URL safe encrypted String
* @throws Exception
*/
public synchronized static String blowfishEncryption(String plainText, String key) throws Exception {
if(DEBUG) {
logger.log(Level.INFO,"blowfishEncryption() method ===== passed normal text: { "+plainText+" passed key: "+key+" }");
}
if(key==null) {
logger.log(Level.INFO,"passed key is null using default key");
key=BLOWFISH_SECRET;
}
ByteArrayOutputStream os= new ByteArrayOutputStream(1024);
byte[] keyByte = hexToBytes(key);
SecretKeySpec skeySpec = new SecretKeySpec(keyByte, "Blowfish");
Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] stringByte=plainText.getBytes("US-ASCII");
byte[] econtent=ecipher.doFinal(stringByte);
String out= new String(Base64.encodeBase64(econtent), "US-ASCII");
return out;
}
/** Utility method for Blowfish Decryption. This method is synchronised between threads.
* <p>NOTE: use the same key for Encryption and Decryption</p>
*
* @param cipherContent Cipher Text Byte array to be decrypted
* @param key Key used for Decryption. NOTE: use same key for encryption and decryption
* @return String Plain text String
* @throws Exception
*/
public synchronized static String blowfishDecryption(String cipherText, String key) throws Exception {
// String ciphertext is base 64 endoded string This method returns plain text string
if(DEBUG) {
logger.log(Level.INFO,"blowfishEncryption() method ===== passed key: "+key+" }");
}
if(key==null) {
logger.log(Level.INFO,"passed key is null using default key");
key=BLOWFISH_SECRET;
}
byte[] myKeyByte = hexToBytes(key);
SecretKeySpec skeySpec = new SecretKeySpec(myKeyByte, "Blowfish");
Cipher ecipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
ecipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] cipherContent=cipherText.getBytes("US-ASCII");
byte[] dContent=ecipher.doFinal(cipherContent);
String out=new String(Base64.encodeBase64(dContent), "US-ASCII");
return out;
}
but I am getting Bad padding exception What is wrong. Also I want the result to be in String. I will pass this string to my server and server will decrypt using same method that is used here.
Blowfish encryption
Encrypt value=J7mbZ4mal7R9ckRBodqqyti70XD3+Bci
[java] Blow fish decryption====================
[java] Encrypt value=J7mbZ4mal7R9ckRBodqqyti70XD3+Bci
Upvotes: 0
Views: 2620
Reputation: 8395
You are Base64-encoding the encrypted data in the blowfishEncryption
method, but instead of Base64-decoding it back into array of bytes in the blowfishEncryption
you are using
byte[] cipherContent=cipherText.getBytes("US-ASCII");
Replace that line with
byte[] cipherContent=Base64.decodeBase64(cipherText);
// fix the method name if needed, as it is not clear what Base64 class you are using
Upvotes: 1