· 6 years ago · Aug 15, 2019, 03:34 AM
1import hdpenc.cipher.utilities.Tools;
2import org.apache.commons.codec.binary.Hex;
3import org.bouncycastle.jce.provider.BouncyCastleProvider;
4
5import javax.crypto.Cipher;
6import javax.crypto.SecretKey;
7import javax.crypto.spec.IvParameterSpec;
8import javax.crypto.spec.SecretKeySpec;
9import javax.xml.bind.DatatypeConverter;
10import java.io.UnsupportedEncodingException;
11import java.security.Security;
12
13//import java.util.Base64;
14
15public class Main {
16
17 private static final int keyLength = 32;
18 //private static final SecureRandom random = new SecureRandom();
19
20 private static SecretKey key;
21 private static byte[] iv;
22
23 public static void main(String [] args) throws Exception {
24 Security.addProvider(new BouncyCastleProvider());
25 Main test= new Main();
26 String plain = "jaja";
27
28 /*System.out.println(byte2HexStr(plain.getBytes()));
29 byte[] encrypted =encrypt(plain);
30 String str= byte2HexStr(encrypted);
31 System.out.println(str);
32 byte[] decrypted = decrypt(Tools.hexStr2Byte(str));
33 System.out.println(byte2HexStr(decrypted));*/
34 byte[] cipher =test.encrypt(plain);
35 String str = Tools.byte2HexStr(cipher);
36 System.out.println(str);
37 byte[] recover =decrypt(Tools.hexStr2Byte(str));
38 System.out.println(new String(Hex.decodeHex(test.toHexString(recover).toCharArray()),"UTF-8"));
39
40
41
42 }
43
44 private static void printByteArr(byte[] arr) {
45 System.out.print("[");
46 for (int i = 0; i < arr.length; i++) {
47 System.out.printf(i == 0 ? "%d" : ",%d", (arr[i] & 0xFF));
48 }
49 System.out.println("]");
50 }
51 public String toHexString(byte[] array) {
52 return DatatypeConverter.printHexBinary(array);
53 }
54
55 public static byte [] encrypt(String plaintext) throws Exception {
56 key = generateKey();
57
58 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
59 String ivStr = "0123456789abcdef";
60 iv = ivStr.getBytes("US-ASCII");
61 cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
62
63
64 // printByteArr(iv);
65 return cipher.doFinal(plaintext.getBytes());
66 }
67
68
69 /*private static String decrypt(byte [] ciphertext) throws Exception {
70 printByteArr(ciphertext);
71 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
72 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
73 return new String(cipher.doFinal(ciphertext));
74 }*/
75 public static byte[] decrypt ( byte[] cipherText)
76 throws Exception
77 {
78 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
79
80 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
81
82 return cipher.doFinal(cipherText);
83 }
84
85 private static SecretKey generateKey() throws Exception {
86 byte[] keyBytes = new byte[keyLength];
87 //random.nextBytes(keyBytes);
88 SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
89 return keySpec;
90 }
91
92 private static IvParameterSpec generateIV(Cipher cipher) throws Exception {
93 byte [] ivBytes = new byte[cipher.getBlockSize()];
94 //random.nextBytes(ivBytes);
95 return new IvParameterSpec(ivBytes);
96 }
97 public static byte[] keyAdjust(byte[] key,int keyLength){
98 int i=0;
99 String strKey = null;
100
101 if(key.length == keyLength) return key;
102
103 try {
104 strKey = new String(key, "UTF-8");
105 } catch (UnsupportedEncodingException e) {
106 e.printStackTrace();
107 }
108
109 if(key.length < keyLength){
110 while(strKey.length() < keyLength){
111 strKey += strKey.charAt(i++);
112 }
113 }else{
114 strKey = strKey.substring(0,keyLength);
115 }
116 return strKey.getBytes();
117 }
118
119
120 public byte[] ivAdjust(byte[] iv){
121 int i=0;
122 String strIV = null;
123
124 if(iv.length == 16) return iv;
125
126 try {
127 strIV = new String(iv, "UTF-8");
128 } catch (UnsupportedEncodingException e) {
129 e.printStackTrace();
130 }
131
132 if(iv.length < 16){
133 while(strIV.length() < 16){
134 strIV += strIV.charAt(i++);
135 }
136 }else{
137 strIV = strIV.substring(0,16);
138 }
139 return strIV.getBytes();
140 }
141
142
143 public static int keySizeReview(int keySize){
144 return 32;
145 }
146}