ROCKY
ROCKY

Reputation: 73

How to decrypt file in java which is encrypted in .net

I have a file which is encrypted in .Net. I have to decrypt that file in java. I have key and IV in text file. This file in encrypted using AES, CBC and PKCS7.

I was trying to do that using following code. Can any one please help me ?

File file = new File("myFile.txt");
String key = readFile(new File("AESKey.bin"));
String iv = readFile(new File("AESIV.bin"));
final byte[] secretKey = key.getBytes();
final byte[] initVector = iv.getBytes();
InputStream cipherInputStream = null;
final StringBuilder output = new StringBuilder();
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey, "AES"), new
IvParameterSpec(initVector, 0, cipher.getBlockSize()));
cipherInputStream = new CipherInputStream(new FileInputStream(file), cipher);
final byte[] buffer = new byte[8192];
int read = cipherInputStream.read(buffer);
final String charsetName = "UTF-8";
while (read > -1) {
    output.append(new String(buffer, 0, read, charsetName));
read = cipherInputStream.read(buffer);
}
System.out.println(output);*

It is giving exception -

Exception in thread "main" java.security.NoSuchAlgorithmException: Cannot find any provider

supporting AES/CBC/PKCS7Padding.

Can any one help me please ?

Upvotes: 3

Views: 411

Answers (1)

Marko Topolnik
Marko Topolnik

Reputation: 200138

I suggest you try to instantiate the cipher

AES/CBC/PKCS5Padding

which is

a) supported on the JDK;

b) identical in effect to PKSCS7 padding.

Upvotes: 2

Related Questions