Plz refer below code here I m trying to Decrypt a file which is
Encrypted_ in
DOT NET_ using following Algo,mode and Padding Scheme
ALGORITHM : AES
MODE : ECB
PADDING SCHEME : PKCS7Padding
Now I have to decrypt it in java using third party provider class
BouncyCastleProvider for PKCS7Padding but I m not getting success and getting....Exception like
org.bouncycastle.crypto.InvalidCipherTextException: pad block corrupted
The PassPhrase I m using is also same which is used @ the time of Encryption.
I m in need of Urgent solution of this.Is there any other mechanism available then also I m ready to use it.
Ur reply would be appreciated........!!
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class Forum {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
String passPhrase = "pAsSPhraSe";
String encFileName = "EncData.xls.enc";;
String decFileName = "DecData.xls";;
FileInputStream encFileIn = null;
FileOutputStream decFileOut = null;
File f = null;
byte[] message;
try {
f = new File(encFileName);
encFileIn = new FileInputStream(f);
decFileOut = new FileOutputStream(decFileName);
message = new byte[encFileIn.available()]; //Read the encrypted file in from disk
encFileIn.read(message);
byte[] input = message;
byte[] keyBytes = passwordToKey (passPhrase);
SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC");
System.out.println(new String(input));
cipher.init(Cipher.DECRYPT_MODE, key);
//Decryption pass
int ctLength = cipher.update(input, 0, input.length, input, 0);
byte[] plainText = new byte[cipher.getOutputSize(ctLength)];
int ptLength = cipher.update(input, 0, ctLength, plainText, 0);
ptLength += cipher.doFinal(plainText, ptLength);
System.out.println(new String(plainText));
System.out.println(ptLength);
decFileOut.write(plainText);
} catch (Exception e) {
System.out.println(e);
}
}
/** From a password, generates a 128-bit key.
* The method is very simple (simply generating a MD5 hash)
* and is not advisable to use it; use some another
* method such as that defined in "Password-Based Encryption" schemes.
*/
public static byte[] passwordToKey (String password) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance ("MD5");
} catch (NoSuchAlgorithmException ex) {
return new byte[0];
}
byte[] passwordBytes = null;
try {
passwordBytes = password.getBytes ("ISO-8859-1");
} catch (UnsupportedEncodingException ex) {
passwordBytes = new byte[0];
}
return md.digest(passwordBytes);
}
}