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