MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / AESExample

Class AESExample

Java/AES.java:17–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15import javax.crypto.IllegalBlockSizeException;
16import javax.crypto.NoSuchPaddingException;
17public class AESExample
18{
19 /* Private variable declaration */
20 private static final String SECRET_KEY = "123456789";
21 private static final String SALTVALUE = "abcdefg";
22
23 /* Encryption Method */
24 public static String encrypt(String strToEncrypt)
25 {
26 try
27 {
28 /* Declare a byte array. */
29 byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
30 IvParameterSpec ivspec = new IvParameterSpec(iv);
31 /* Create factory for secret keys. */
32 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
33 /* PBEKeySpec class implements KeySpec interface. */
34 KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256);
35 SecretKey tmp = factory.generateSecret(spec);
36 SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
37 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
38 cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivspec);
39 /* Retruns encrypted value. */
40 return Base64.getEncoder()
41.encodeToString(cipher.doFinal(strToEncrypt.getBytes(StandardCharsets.UTF_8)));
42 }
43 catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e)
44 {
45 System.out.println("Error occured during encryption: " + e.toString());
46 }
47 return null;
48 }
49
50 /* Decryption Method */
51 public static String decrypt(String strToDecrypt)
52 {
53 try
54 {
55 /* Declare a byte array. */
56 byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
57 IvParameterSpec ivspec = new IvParameterSpec(iv);
58 /* Create factory for secret keys. */
59 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
60 /* PBEKeySpec class implements KeySpec interface. */
61 KeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), SALTVALUE.getBytes(), 65536, 256);
62 SecretKey tmp = factory.generateSecret(spec);
63 SecretKeySpec secretKey = new SecretKeySpec(tmp.getEncoded(), "AES");
64 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
65 cipher.init(Cipher.DECRYPT_MODE, secretKey, ivspec);
66 /* Retruns decrypted value. */
67 return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
68 }
69 catch (InvalidAlgorithmParameterException | InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | BadPaddingException | IllegalBlockSizeException | NoSuchPaddingException e)
70 {
71 System.out.println("Error occured during decryption: " + e.toString());
72 }
73 return null;
74 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected