· 5 years ago · Dec 03, 2019, 08:42 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 symasymcrypto;
7
8/**
9 *
10 * @author student
11 */
12
13import java.security.Key;
14import java.security.KeyPair;
15import java.security.KeyPairGenerator;
16import java.security.Security;
17import java.security.SecureRandom;
18import javax.crypto.Cipher;
19import java.security.InvalidKeyException;
20import java.security.NoSuchAlgorithmException;
21import javax.crypto.BadPaddingException;
22import javax.crypto.IllegalBlockSizeException;
23import javax.crypto.NoSuchPaddingException;
24import javax.crypto.SecretKey;
25import javax.crypto.SecretKeyFactory;
26import javax.crypto.ShortBufferException;
27import javax.crypto.spec.IvParameterSpec;
28import javax.crypto.spec.PBEKeySpec;
29import javax.crypto.spec.SecretKeySpec;
30import javax.rmi.CORBA.Util;
31
32
33public class SymAsymCrypto {
34
35 /**
36 * @param args the command line arguments
37 */
38 public static void main(String[] args) throws Exception {
39 // TODO code application logic here
40
41 byte[] keyBytes = new byte[16];
42 // declare secure PRNG
43 SecureRandom myPRNG = new SecureRandom();
44 // seed the key
45 myPRNG.nextBytes(keyBytes);
46 // build the key from random key bytes
47 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
48 IvParameterSpec IV = new IvParameterSpec(keyBytes);
49 // instantiate AES object for ECB with no padding
50 Cipher myAES = Cipher.getInstance("AES/CBC/NoPadding");
51 // initialize AES objecy to encrypt mode
52 myAES.init(Cipher.ENCRYPT_MODE, myKey, IV);
53 // initialize plaintext
54 byte[] plaintext = new byte[32];
55 //initialize ciphertext
56 byte[] ciphertext = new byte[32];
57 // update cipher with the plaintext
58 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
59 // process remaining blocks of plaintext
60 cLength += myAES.doFinal(ciphertext, cLength);
61 // print plaintext and ciphertext
62 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
63 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
64 // initialize AES for decryption
65 myAES.init(Cipher.DECRYPT_MODE, myKey, IV);
66 // initialize a new array of bytes to place the decryption
67 byte[] dec_plaintext = new byte[32];
68 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,0);
69 // process remaining blocks of ciphertext
70 cLength += myAES.doFinal(dec_plaintext, cLength);
71 // print the new plaintext (hopefully identical to the initial one)
72 System.out.println("decrypted: " + javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
73
74 // get a Cipher instance for RSA with PKCS1 padding
75 Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
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 // encrypt, as expected we encrypt a symmetric key with RSA rather than
87 //a file or some longer stream which should be encrypted with AES
88 ciphertext = myRSA.doFinal(keyBytes);
89 // init cipher for decryption
90 myRSA.init(Cipher.DECRYPT_MODE, pvKey);
91 // decrypt
92 plaintext = myRSA.doFinal(ciphertext);
93 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
94 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
95 System.out.println("keybytes: " + javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
96
97
98 char[] password = "short_password".toCharArray();
99 byte[] salt = new byte[16];
100 int iteration_count = 10000;
101 int key_size = 128;
102 // set salt values to random
103 myPRNG.nextBytes(salt);
104
105 // initialize key factory for HMAC-SHA1 derivation
106 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
107 // set key specification
108 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,key_size);
109 // generate the key
110 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
111 // print the key
112 System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
113 }
114
115}