· 6 years ago · Nov 21, 2019, 01:40 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 */
6
7
8package laborator7;
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.PBEKeySpec;
25import javax.crypto.spec.SecretKeySpec;
26import javax.rmi.CORBA.Util;
27
28public class Laborator7 {
29
30 /**
31 * @param args the command line arguments
32 */
33
34 public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
35 // TODO code application logic here
36
37
38 //IV vector
39 byte[] iv = new byte[16];
40 SecureRandom ivspec = new SecureRandom(iv);
41 ivspec.nextBytes(iv);
42
43 //Simetric "AES"
44 byte[] keyBytes = new byte[16];
45 // declare secure PRNG
46 SecureRandom myPRNG = new SecureRandom();
47 // seed the key
48 myPRNG.nextBytes(keyBytes);
49 // build the key from random key bytes
50 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
51 // instantiate AES object for ECB with no padding
52 Cipher myAES = Cipher.getInstance("AES/ECB/NoPadding");
53 // initialize AES objecy to encrypt mode
54 myAES.init(Cipher.ENCRYPT_MODE, myKey);
55 // initialize plaintext
56 byte[] plaintext = new byte[32];
57
58 //initialize ciphertext
59 byte[] ciphertext = new byte[myAES.getOutputSize(plaintext.length)];
60 // update cipher with the plaintext
61 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
62 // process remaining blocks of plaintext
63 cLength += myAES.doFinal(ciphertext, cLength);
64 // print plaintext and ciphertext
65 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
66 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
67 // initialize AES for decryption
68
69
70
71
72 myAES.init(Cipher.DECRYPT_MODE, myKey);
73 // initialize a new array of bytes to place the decryption
74 byte[] dec_plaintext = new byte[32];
75 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,0);
76 // process remaining blocks of ciphertext
77 cLength += myAES.doFinal(dec_plaintext, cLength);
78 // print the new plaintext (hopefully identical to the initial one)
79 System.out.println("decrypted: " +
80 javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
81 // get a Cipher instance for RSA with PKCS1 padding
82 Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
83 // get an instance for the Key Generator
84 KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
85 // generate an 1024 bit key
86 KeyPair myRSAKeyPair= myRSAKeyGen.generateKeyPair();
87 // store the public and private key individually
88 Key pbKey = myRSAKeyPair.getPublic();
89 Key pvKey = myRSAKeyPair.getPrivate();
90
91
92 // init cipher for encryption
93 myRSA.init(Cipher.ENCRYPT_MODE, pbKey, ivspec);
94 // encrypt, as expected we encrypt a symmetric key with RSA rather than a file or some longer stream which should be encrypted with AES
95 ciphertext = myRSA.doFinal(keyBytes);
96 // init cipher for decryption
97 myRSA.init(Cipher.DECRYPT_MODE, pvKey);
98 // decrypt
99 plaintext = myRSA.doFinal(ciphertext);
100 System.out.println("plaintext: " + javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
101 System.out.println("ciphertext: " + javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
102 System.out.println("keybytes: " + javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
103
104
105 // System.out.println("IV vector : " + javax.xml.bind.DatatypeConverter.printHexBinary(iv));
106
107 char[] password = "short_password".toCharArray();
108 byte[] salt = new byte[16];
109 int iteration_count = 10000;
110
111 int key_size = 128;
112 // set salt values to random
113 myPRNG.nextBytes(salt);
114
115 // initialize key factory for HMAC-SHA1 derivation
116 SecretKeyFactory keyFactory =
117 SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
118 // set key specification
119 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,key_size);
120 // generate the key
121 SecretKey myAESPBKey = new SecretKeySpec(
122 keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
123 // print the key
124 System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
125 System.out.println("IV vector : " + javax.xml.bind.DatatypeConverter.printHexBinary(iv));
126
127
128 }
129
130}