· 6 years ago · Sep 20, 2019, 07:20 AM
1import sun.misc.BASE64Decoder;
2import sun.misc.BASE64Encoder;
3
4import javax.crypto.Cipher;
5import javax.crypto.NoSuchPaddingException;
6import javax.crypto.spec.IvParameterSpec;
7import javax.crypto.spec.SecretKeySpec;
8import java.math.BigInteger;
9import java.security.InvalidAlgorithmParameterException;
10import java.security.InvalidKeyException;
11import java.security.MessageDigest;
12import java.security.NoSuchAlgorithmException;
13
14public class Main {
15 private static String md5(final String input) throws NoSuchAlgorithmException {
16 final MessageDigest md = MessageDigest.getInstance("MD5");
17 final byte[] messageDigest = md.digest(input.getBytes());
18 final BigInteger number = new BigInteger(1, messageDigest);
19 return String.format("%032x", number);
20 }
21
22 private Cipher initCipher(final int mode, final String initialVectorString, final String secretKey)
23 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
24 final SecretKeySpec skeySpec = new SecretKeySpec(md5(secretKey).getBytes(), "AES");
25 final IvParameterSpec initialVector = new IvParameterSpec(initialVectorString.getBytes());
26 final Cipher cipher = Cipher.getInstance("AES/CFB8/NoPadding");
27 cipher.init(mode, skeySpec, initialVector);
28 return cipher;
29 }
30
31 public String encrypt(final String dataToEncrypt, final String initialVector, final String secretKey) {
32 String encryptedData = null;
33 try {
34 // Initialize the cipher
35 final Cipher cipher = initCipher(Cipher.ENCRYPT_MODE, initialVector, secretKey);
36 // Encrypt the data
37 final byte[] encryptedByteArray = cipher.doFinal(dataToEncrypt.getBytes());
38 // Encode using Base64
39 encryptedData = (new BASE64Encoder()).encode(encryptedByteArray);
40 } catch (Exception e) {
41 System.err.println("Problem encrypting the data");
42 e.printStackTrace();
43 }
44 return encryptedData;
45 }
46
47 public String decrypt(final String encryptedData, final String initialVector, final String secretKey) {
48 String decryptedData = null;
49 try {
50 // Initialize the cipher
51 final Cipher cipher = initCipher(Cipher.DECRYPT_MODE, initialVector, secretKey);
52 // Decode using Base64
53 final byte[] encryptedByteArray = (new BASE64Decoder()).decodeBuffer(encryptedData);
54 // Decrypt the data
55 final byte[] decryptedByteArray = cipher.doFinal(encryptedByteArray);
56 decryptedData = new String(decryptedByteArray, "UTF8");
57 } catch (Exception e) {
58 System.err.println("Problem decrypting the data");
59 e.printStackTrace();
60 }
61 return decryptedData;
62 }
63
64 public static void main(final String[] args) {
65 final String iv = "0123456789123456"; // This has to be 16 characters
66 final String secretKey = "Replace this by your secret key";
67 final Main crypto = new Main();
68
69 final String encryptedData = crypto.encrypt("This is a test message.", iv, secretKey);
70 System.out.println(encryptedData);
71
72 final String decryptedData = crypto.decrypt(encryptedData, iv, secretKey);
73 System.out.println(decryptedData);
74 }
75}