Skip to content

Commit 37cb330

Browse files
Create Decrypt.Java
1 parent 076db8b commit 37cb330

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

AES Descyption/Decrypt.Java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.DecryptionUTIL.CodeUTIL();
2+
3+
import java.security.InvalidAlgorithmParameterException;
4+
import java.security.InvalidKeyException;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.security.SecureRandom;
7+
import java.security.spec.InvalidKeySpecException;
8+
import java.util.Base64;
9+
10+
import javax.crypto.BadPaddingException;
11+
import javax.crypto.Cipher;
12+
import javax.crypto.IllegalBlockSizeException;
13+
import javax.crypto.NoSuchPaddingException;
14+
import javax.crypto.SecretKey;
15+
import javax.crypto.spec.IvParameterSpec;
16+
import javax.crypto.spec.SecretKeySpec;
17+
18+
19+
20+
public class CSBUtil {
21+
22+
public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, BadPaddingException, IllegalBlockSizeException {
23+
24+
String cipherText = "TmD/I7qMnzJFNa/eTT2CdiNao6TPtznoVh4P+Kgp07oqY7yzXPfgRx7ApKAGwl5VSANvo+5E71Gki13hwgRKAcDBCnhtz1w7ShRVt7WtF5PXBVOx6NPZvuwVrZAZF7lQWwIJCk9jYEgxaZLRFEdBC/km//jmlmwkrsV7i+SHnqNhvY+A8BUlx+/fvae+wNCHWkf1YbbG/TXwj9rRiZhklm0Wuw0UgknleFo4tcCNxrm0o14Wspw+tSfGwR8OXzRKfU3eF+YuBP+p+eYI5FYT2oj8B5g0PUwBp3PinBdUFp9zxJ7s5+GeYDUXxf/u98uecIln5VIYXkJt3HWZwz6l6oRsXf54KQYPvMYMoKH88FzxWEnGs6eDWSCLDbs89Rk9tt/mwnihmzjFUqkPDSJ2rQ==";
25+
SecretKey key = getKeyFromPassword("f4f35fe3ed13641d07cf7f21913b4682");
26+
IvParameterSpec ivParameterSpec = generateIv();
27+
String algorithm = "AES/CBC/PKCS5Padding";
28+
29+
String plainText = decrypt(algorithm, cipherText, key, ivParameterSpec);
30+
System.out.println("Ciphertext : " + cipherText + "\nPlain Text :" + plainText.substring(16,plainText.length()));
31+
32+
}
33+
34+
public static IvParameterSpec generateIv() {
35+
byte[] iv = new byte[16];
36+
new SecureRandom().nextBytes(iv);
37+
return new IvParameterSpec(iv);
38+
}
39+
40+
public static SecretKey getKeyFromPassword(String password)
41+
throws NoSuchAlgorithmException, InvalidKeySpecException {
42+
43+
SecretKey secret1 = new SecretKeySpec("samplekey".getBytes(), "AES");
44+
return secret1;
45+
}
46+
47+
public static String decrypt(String algorithm, String cipherText, SecretKey key,
48+
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
49+
InvalidAlgorithmParameterException, InvalidKeyException,
50+
BadPaddingException, IllegalBlockSizeException {
51+
52+
Cipher cipher = Cipher.getInstance(algorithm);
53+
cipher.init(Cipher.DECRYPT_MODE, key, iv);
54+
byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
55+
return new String(plainText);
56+
}
57+
}

0 commit comments

Comments
 (0)