· 7 years ago · Apr 26, 2018, 07:32 AM
1
2package javaapplication57;
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;
10import java.security.InvalidKeyException;
11import java.security.NoSuchAlgorithmException;
12import java.security.spec.InvalidKeySpecException;
13import javax.crypto.BadPaddingException;
14import javax.crypto.IllegalBlockSizeException;
15import javax.crypto.NoSuchPaddingException;
16import javax.crypto.SecretKey;
17import javax.crypto.SecretKeyFactory;
18import javax.crypto.ShortBufferException;
19import javax.crypto.spec.IvParameterSpec;
20import javax.crypto.spec.PBEKeySpec;
21import javax.crypto.spec.SecretKeySpec;
22import javax.rmi.CORBA.Util;
23/**
24 *
25 * @author student
26 */
27public class JavaApplication57 {
28
29 /**
30 * @param args the command line arguments
31 */
32 public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, InvalidAlgorithmParameterException {
33 // TODO code application logic here
34
35 IvParameterSpec iv;
36 SecureRandom secureRandom = new SecureRandom();
37 byte[] ivspec = new byte[16];
38 secureRandom.nextBytes(ivspec);
39 iv= new IvParameterSpec(ivspec);
40 byte[] keyBytes = new byte[16];
41 // declare secure PRNG
42 SecureRandom myPRNG = new SecureRandom();
43 // seed the key
44 myPRNG.nextBytes(keyBytes);
45 // build the key from random key bytes
46 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
47 // instantiate AES object for ECB with no padding
48 Cipher myAES = Cipher.getInstance("AES/CBC/NoPadding");
49// initialize AES objecy to encrypt mode
50 myAES.init(Cipher.ENCRYPT_MODE, myKey);
51// initialize plaintext
52 byte[] plaintext = new byte[16];
53//initialize ciphertext
54 byte[] ciphertext = new byte[16];
55// update cipher with the plaintext
56 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
57// process remaining blocks of plaintext
58 cLength += myAES.doFinal(ciphertext, cLength);
59// print plaintext and ciphertext
60 System.out.println("plaintext: " +javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
61 System.out.println("ciphertext: " +javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
62// initialize AES for decryption
63 byte[] random= new byte[16];
64 myPRNG.nextBytes(random);
65 IvParameterSpec iv1 = new IvParameterSpec(random);
66 myAES.init(Cipher.DECRYPT_MODE, myKey, iv1);
67// initialize a new array of bytes to place the decryption
68 byte[] dec_plaintext = new byte[16];
69 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,0);
70// process remaining blocks of ciphertext
71 cLength += myAES.doFinal(dec_plaintext, cLength);
72// print the new plaintext (hopefully identical to the initial one)
73 System.out.println("decrypted: " +javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
74 // get a Cipher instance for RSA with PKCS1 padding
75 Cipher myRSA = Cipher.getInstance("RSA");
76// get an instance for the Key Generator
77 KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
78// generate an 1024 bit key
79 myRSAKeyGen.initialize(1024, myPRNG);
80 KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
81// store the public and private key individually
82 Key pbKey = myRSAKeyPair.getPublic();
83 Key pvKey = myRSAKeyPair.getPrivate();
84// init cipher for encryption
85 myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
86
87 ciphertext = myRSA.doFinal(keyBytes);
88// init cipher for decryption
89 myRSA.init(Cipher.DECRYPT_MODE, pvKey);
90// decrypt
91 plaintext = myRSA.doFinal(ciphertext);
92 System.out.println("plaintext: " +javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
93 System.out.println("ciphertext: " +javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
94 System.out.println("keybytes: " +javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
95
96 char[] password = "short_password".toCharArray();
97 byte[] salt = new byte[16];
98 int iteration_count = 10000;
99
100 int key_size = 128;
101// set salt values to random
102 myPRNG.nextBytes(salt);
103// initialize key factory for HMAC-SHA1 derivation
104 SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
105// set key specification
106 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,key_size);
107// generate the key
108 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
109// print the key
110 System.out.println("AES key: " +javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
111 }
112
113}