· 8 years ago · Nov 28, 2017, 08:32 AM
1import java.security.Key;
2import java.security.KeyPair;
3import java.security.KeyPairGenerator;
4import java.security.Security;
5import java.security.SecureRandom;
6import javax.crypto.Cipher;
7import java.security.InvalidKeyException;
8import java.security.NoSuchAlgorithmException;
9import java.security.spec.InvalidKeySpecException;
10
11import javax.crypto.BadPaddingException;
12import javax.crypto.IllegalBlockSizeException;
13import javax.crypto.NoSuchPaddingException;
14import javax.crypto.SecretKey;
15import javax.crypto.SecretKeyFactory;
16import javax.crypto.ShortBufferException;
17import javax.crypto.spec.PBEKeySpec;
18import javax.crypto.spec.SecretKeySpec;
19import javax.rmi.CORBA.Util;
20
21public class CryptoLab {
22
23 public static long[] benchAlgo(String prefix, int keysize, byte[] input) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException
24 {
25 System.out.println("\n" + prefix);
26
27 long start, end;
28 long[] times = new long[2];
29 byte[] keyBytes = new byte[keysize];
30 // declare securePRNG
31 SecureRandom myPRNG = new SecureRandom();
32 // seed the key
33 myPRNG.nextBytes(keyBytes);
34 // build the key from random key bytes
35 SecretKeySpec myKey = new SecretKeySpec(keyBytes, prefix);
36 // instantiate AES object for ECB with no padding
37 Cipher myAES = Cipher.getInstance(prefix + "/ECB/NoPadding");
38 // initialize AES objecy to encrypt mode
39 myAES.init(Cipher.ENCRYPT_MODE, myKey);
40 // initialize plaintext
41 byte[] plaintext = input;
42 System.out.println("plaintext:\t\t" +
43 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
44 // initialize ciphertext
45 byte[] ciphertext = new byte[input.length];
46
47 start = System.nanoTime();
48 // update cipher with the plaintext
49 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext, 0);
50 while(cLength < plaintext.length)
51 // process remaining blocks of plaintext
52 cLength += myAES.doFinal(ciphertext, cLength);
53 // print plaintext and ciphertext
54 end = System.nanoTime();
55 System.out.println("ciphertext:\t\t" +
56 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext) +
57 " in " + (end-start) + "ns");
58 times[0] = end-start;
59 // initialize AES for decryption
60 myAES.init(Cipher.DECRYPT_MODE, myKey);
61 // initialize a new array of bytes to place the decryption
62 byte[] dec_plaintext = new byte[input.length];
63 start = System.nanoTime();
64 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext, 0);
65 // process remaining blocks of ciphertext
66 cLength += myAES.doFinal(dec_plaintext, cLength);
67 end = System.nanoTime();
68 // print the new plaintext (hopefully identical to the initial one)
69 System.out.println("decrypted:\t\t" +
70 javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext) +
71 " in " + (end-start) + "ns");
72 times[1] = end-start;
73 return times;
74 }
75
76 public static void hashes() throws NoSuchAlgorithmException, InvalidKeySpecException
77 {
78 char[] password = "short_password".toCharArray();
79 byte[] salt = new byte[16];
80 int iteration_count = 10000;
81
82 int key_size = 128;
83 // set salt values to random
84 SecureRandom myPRNG = new SecureRandom();
85 myPRNG.nextBytes(salt);
86 // initialize key factory for HMAC - SHA1 derivation
87 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
88 // set key specification
89 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,
90 key_size);
91 // generate the key
92 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
93 // print the key
94 System.out.println("AES\tkey:\t" +
95 javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
96 }
97
98 public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException
99 {
100 hashes();
101 }
102}