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