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