· 7 years ago · Nov 13, 2018, 07:44 AM
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package javaapplication18;
7
8import java.security.Key;
9import java.security.KeyPair;
10import java.security.KeyPairGenerator;
11import java.security.Security;
12import java.security.SecureRandom;
13import javax.crypto.Cipher;
14
15import java.security.InvalidKeyException;
16import java.security.NoSuchAlgorithmException;
17import javax.crypto.BadPaddingException;
18import javax.crypto.IllegalBlockSizeException;
19import javax.crypto.NoSuchPaddingException;
20import javax.crypto.SecretKey;
21import javax.crypto.SecretKeyFactory;
22import javax.crypto.ShortBufferException;
23import javax.crypto.spec.IvParameterSpec;
24import javax.crypto.spec.PBEKeySpec;
25import javax.crypto.spec.SecretKeySpec;
26import javax.rmi.CORBA.Util;
27
28/**
29 *
30 * @author student
31 */
32public class JavaApplication18 {
33
34 /**
35 * @param args the command line arguments
36 */
37 public static SecretKey small_to_big_password(String short_pass) {
38
39 char[] password = short_pass.toCharArray();
40 byte[] salt = new byte[16];
41 int iteration_count = 10000;
42 int iterations = 100;
43 int key_size = 128;
44 long startTime = System.nanoTime();
45
46 for (int i = 0; i < iterations; i++) {
47 SecureRandom myPRNG = new SecureRandom();
48
49 SecretKey myAESPBKey = new SecretKeySpec(salt, "AES");
50 // set salt values to random
51 myPRNG.nextBytes(salt);
52 try {
53 // initialize key factory for HMAC-SHA1 derivation
54 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
55 // set key specification
56 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count, key_size);
57 // generate the key
58 myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
59 } catch (Exception e) {
60 System.out.println("A error occured in small to big password");
61 System.out.println(e);
62 }
63 }
64
65 long endTime = System.nanoTime();
66 long duration = (endTime - startTime) / (iterations * 1000000);
67
68 System.out.println("Time to calculate AESkey from small key: " + duration + " milliseconds");
69
70 SecureRandom myPRNG = new SecureRandom();
71
72 SecretKey myAESPBKey = new SecretKeySpec(salt, "AES");
73 // set salt values to random
74 myPRNG.nextBytes(salt);
75 try {
76 // initialize key factory for HMAC-SHA1 derivation
77 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
78 // set key specification
79 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count, key_size);
80 // generate the key
81 myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
82 } catch (Exception e) {
83 System.out.println("A error occured in small to big password");
84 System.out.println(e);
85 }
86 // return the key
87 return myAESPBKey;
88
89 }
90
91 public static void first_example() {
92 try {
93 byte[] keyBytes = new byte[16];
94 // declare secure PRNG
95 SecureRandom myPRNG = new SecureRandom();
96 // seed the key
97 myPRNG.nextBytes(keyBytes);
98 // build the key from random key bytes
99 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
100 // instantiate AES object for ECB with no padding
101 Cipher myAES = Cipher.getInstance("AES/ECB/NoPadding");
102 // initialize AES objecy to encrypt mode
103 myAES.init(Cipher.ENCRYPT_MODE, myKey);
104 // initialize plaintext
105 byte[] plaintext = new byte[16];
106 //initialize ciphertext
107 byte[] ciphertext = new byte[16];
108
109 // update cipher with the plaintext
110 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
111 // process remaining blocks of plaintext
112 cLength += myAES.doFinal(ciphertext, cLength);
113
114 // print plaintext and ciphertext
115 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
116 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
117 // initialize AES for decryption
118 myAES.init(Cipher.DECRYPT_MODE, myKey);
119 // initialize a new array of bytes to place the decryption
120 byte[] dec_plaintext = new byte[16];
121 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext, 0);
122 // process remaining blocks of ciphertext
123 cLength += myAES.doFinal(dec_plaintext, cLength);
124 // print the new plaintext (hopefully identical to the initial one)
125 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
126 } catch (Exception e) {
127 System.out.println("A error occured");
128 System.out.println(e);
129 }
130 }
131
132 public static void main(String[] args) {
133 String password = "short_password";
134// first_example();
135 System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(small_to_big_password(password).getEncoded()));
136 SecretKey myAESPBKey = small_to_big_password(password);
137 try {
138 Cipher myAES = Cipher.getInstance("AES/ECB/NoPadding");
139 // initialize AES objecy to encrypt mode
140 myAES.init(Cipher.ENCRYPT_MODE, myAESPBKey);
141
142 Cipher OFB = Cipher.getInstance("AES/OFB/NoPadding");
143 // initialize AES objecy to encrypt mode
144 OFB.init(Cipher.ENCRYPT_MODE, myAESPBKey);
145
146 Cipher CFB = Cipher.getInstance("AES/CFB/NoPadding");
147 // initialize AES objecy to encrypt mode
148 CFB.init(Cipher.ENCRYPT_MODE, myAESPBKey);
149 byte[] plaintext = new byte[16];
150 //initialize ciphertext
151 byte[] ciphertext = new byte[16];
152
153 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
154 cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
155 // process remaining blocks of plaintext
156 cLength += myAES.doFinal(ciphertext, cLength);
157
158 System.out.println("ciphertext after ECB: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
159// update cipher with the plaintext
160 cLength = OFB.update(plaintext, 0, plaintext.length, ciphertext, 0);
161 // process remaining blocks of plaintext
162 cLength += OFB.doFinal(ciphertext, cLength);
163
164 System.out.println("ciphertext after OFB: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
165 // update cipher with the plaintext
166 cLength = CFB.update(plaintext, 0, plaintext.length, ciphertext, 0);
167 // process remaining blocks of plaintext
168 cLength += CFB.doFinal(ciphertext, cLength);
169
170 System.out.println("ciphertext after CFB: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
171
172 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
173
174
175 /* ENCRYPT AND DECRYPT WITH IV */
176 System.out.println("IV");
177 // initialize AES for decryption
178 SecureRandom randomSecureRandom = SecureRandom.getInstance("SHA1PRNG");
179 byte[] iv = new byte[CFB.getBlockSize()];
180 randomSecureRandom.nextBytes(iv);
181 IvParameterSpec ivParams = new IvParameterSpec(iv);
182
183 CFB = Cipher.getInstance("AES/CFB/NoPadding");
184 // initialize AES objecy to encrypt mode
185 CFB.init(Cipher.ENCRYPT_MODE, myAESPBKey, ivParams);
186 // update cipher with the plaintext
187 cLength = CFB.update(plaintext, 0, plaintext.length, ciphertext, 0);
188 // process remaining blocks of plaintext
189 cLength += CFB.doFinal(ciphertext, cLength);
190
191 System.out.println("ciphertext after CFB: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
192
193 CFB.init(Cipher.DECRYPT_MODE, myAESPBKey, ivParams);
194 // initialize a new array of bytes to place the decryption
195 byte[] dec_plaintext = new byte[16];
196 cLength = CFB.update(ciphertext, 0, ciphertext.length, dec_plaintext, 0);
197 // process remaining blocks of ciphertext
198 cLength += CFB.doFinal(dec_plaintext, cLength);
199 // print the new plaintext (hopefully identical to the initial one)
200 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
201 } catch (Exception e) {
202 System.out.println("A error occured in main");
203 System.out.println(e);
204 }
205 }
206
207}