· 6 years ago · Apr 18, 2019, 05:22 AM
1package javaapplication47;
2
3import java.security.InvalidAlgorithmParameterException;
4import java.security.Key;
5import java.security.KeyPair;
6import java.security.KeyPairGenerator;
7import java.security.Security;
8import java.security.SecureRandom;
9import javax.crypto.Cipher;
10
11import java.security.InvalidKeyException;
12import java.security.NoSuchAlgorithmException;
13import java.security.spec.InvalidKeySpecException;
14import javax.crypto.BadPaddingException;
15import javax.crypto.IllegalBlockSizeException;
16import javax.crypto.NoSuchPaddingException;
17import javax.crypto.SecretKey;
18import javax.crypto.SecretKeyFactory;
19import javax.crypto.ShortBufferException;
20import javax.crypto.spec.PBEKeySpec;
21import javax.crypto.spec.SecretKeySpec;
22import javax.rmi.CORBA.Util;
23
24
25public class JavaApplication47 {
26
27 /**
28 * @param args the command line arguments
29 */
30 public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException, NoSuchPaddingException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException {
31 byte[] keyBytes = new byte[16];
32 // declare secure PRNG
33 SecureRandom myPRNGCBC = new SecureRandom();
34 // seed the key
35 myPRNGCBC.nextBytes(keyBytes);
36 // build the key from random key bytes
37 SecretKeySpec myKeyCBC = new SecretKeySpec(keyBytes, "AES");
38 // instantiate AES object for ECB with no padding
39 Cipher myAESCBC = Cipher.getInstance("AES/CBC/NoPadding");
40 // initialize AES objecy to encrypt mode
41 myAESCBC.init(Cipher.ENCRYPT_MODE, myKeyCBC);
42 // initialize plaintext 0
43 byte[] plaintext = new byte[32];
44 plaintext[0] = 'Z';
45 plaintext[1] = 'S';
46 plaintext[2] = 'O';
47 plaintext[3] = 'L';
48 plaintext[4] = 'T';
49 plaintext[5] = 6;
50 plaintext[6] = 9;
51
52 //initialize ciphertext
53 byte[] ciphertextCBC = new byte[32];
54 byte[] ciphertextCFB = new byte[32];
55 byte[] ciphertextOFB = new byte[32];
56
57
58
59
60 SecureRandom myPRNGOFB = new SecureRandom();
61 // seed the key
62 myPRNGOFB.nextBytes(keyBytes);
63 // build the key from random key bytes
64 SecretKeySpec myKeyOFB = new SecretKeySpec(keyBytes, "AES");
65 // instantiate AES object for ECB with no padding
66 Cipher myAESOFB = Cipher.getInstance("AES/OFB/NoPadding");
67 // initialize AES objecy to encrypt mode
68 myAESOFB.init(Cipher.ENCRYPT_MODE, myKeyOFB);
69 // initialize plaintext 0
70
71
72 SecureRandom myPRNGCFB = new SecureRandom();
73 // seed the key
74 myPRNGCFB.nextBytes(keyBytes);
75 // build the key from random key bytes
76 SecretKeySpec myKeyCFB = new SecretKeySpec(keyBytes, "AES");
77 // instantiate AES object for ECB with no padding
78 Cipher myAESCFB = Cipher.getInstance("AES/OFB/NoPadding");
79 // initialize AES objecy to encrypt mode
80 myAESCFB.init(Cipher.ENCRYPT_MODE, myKeyCFB);
81 // initialize plaintext 0
82
83
84 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
85
86 // initialize AES for decryption
87 int cLengthCBC = myAESCBC.update(plaintext, 0, plaintext.length, ciphertextCBC, 0);
88 myAESCBC.init(Cipher.DECRYPT_MODE, myKeyCBC, myAESCBC.getParameters());
89 // initialize a new array of bytes to place the decryption
90 byte[] dec_plaintextCBC = new byte[32];
91 cLengthCBC = myAESCBC.update(ciphertextCBC, 0, ciphertextCBC.length, dec_plaintextCBC, 0);
92 // process remaining blocks of ciphertext
93 cLengthCBC += myAESCBC.doFinal(dec_plaintextCBC, cLengthCBC);
94 // print the new plaintext (hopefully identical to the initial one)
95 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertextCBC));
96 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintextCBC));
97
98
99
100 int cLengthOFB = myAESOFB.update(plaintext, 0, plaintext.length, ciphertextOFB, 0);
101 myAESOFB.init(Cipher.DECRYPT_MODE, myKeyOFB, myAESOFB.getParameters());
102 // initialize a new array of bytes to place the decryption
103 byte[] dec_plaintextOFB = new byte[32];
104 cLengthOFB = myAESOFB.update(ciphertextOFB, 0, ciphertextOFB.length, dec_plaintextOFB, 0);
105 // process remaining blocks of ciphertext
106 cLengthOFB += myAESOFB.doFinal(dec_plaintextOFB, cLengthOFB);
107 // print the new plaintext (hopefully identical to the initial one)
108 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertextOFB));
109 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintextOFB));
110
111
112 int cLengthCFB = myAESCFB.update(plaintext, 0, plaintext.length, ciphertextCFB, 0);
113 myAESCFB.init(Cipher.DECRYPT_MODE, myKeyCFB, myAESCFB.getParameters());
114 // initialize a new array of bytes to place the decryption
115 byte[] dec_plaintextCFB = new byte[32];
116 cLengthCFB = myAESCFB.update(ciphertextCFB, 0, ciphertextCFB.length, dec_plaintextCFB, 0);
117 // process remaining blocks of ciphertext
118 cLengthCFB += myAESCFB.doFinal(dec_plaintextCFB, cLengthCFB);
119 // print the new plaintext (hopefully identical to the initial one)
120 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertextCFB));
121 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintextCFB));
122
123
124 char[] password = "short_password".toCharArray();
125 byte[] salt = new byte[16];
126 int iteration_count = 10000;
127
128
129 int key_size = 128;
130 // set salt values to random
131 SecureRandom myPRNG = new SecureRandom();
132 myPRNG.nextBytes(salt);
133
134 // initialize key factory for HMAC-SHA1 derivation
135 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
136 // set key specification
137 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count, key_size);
138 // generate the key
139 SecretKey myAESPBKey = new SecretKeySpec( keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
140 // print the key
141 System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
142
143 Cipher myAES = Cipher.getInstance("AES/OFB/NoPadding");
144 // initialize AES objecy to encrypt mode
145 myAES.init(Cipher.ENCRYPT_MODE, myAESPBKey);
146 myAES.init(Cipher.DECRYPT_MODE, myAESPBKey,myAES.getParameters());
147
148
149
150 }
151
152}