· 5 years ago · Nov 18, 2019, 11:10 AM
1package encriptionusingpbkgkey;
2
3import java.security.Key;
4import java.security.KeyPair;
5import java.security.KeyPairGenerator;
6import java.security.Security;
7import java.security.SecureRandom;
8import javax.crypto.Cipher;
9import javax.crypto.SecretKeyFactory;
10import javax.crypto.SecretKey;
11
12import java.security.InvalidKeyException;
13import java.security.NoSuchAlgorithmException;
14import java.security.spec.InvalidKeySpecException;
15import javax.crypto.BadPaddingException;
16import javax.crypto.IllegalBlockSizeException;
17import javax.crypto.NoSuchPaddingException;
18import javax.crypto.ShortBufferException;
19import javax.crypto.interfaces.PBEKey;
20import javax.crypto.spec.PBEKeySpec;
21import javax.crypto.spec.SecretKeySpec;
22import javax.rmi.CORBA.Util;
23
24
25public class EncriptionUsingPBKGKey {
26
27 public static void CBC_enc() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException{
28 byte[] keyBytes = new byte[16];
29 // declare secure PRNG
30 SecureRandom myPRNG = new SecureRandom();
31 // seed the key
32 myPRNG.nextBytes(keyBytes);
33 // build the key from random key bytes
34 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
35 // instantiate AES object for ECB with no padding
36 Cipher myAES = Cipher.getInstance("AES/CBC/NoPadding");
37 // initialize AES objecy to encrypt mode
38 myAES.init(Cipher.ENCRYPT_MODE, myKey);
39 // initialize plaintext
40 byte[] plaintext = new byte[16];
41 //initialize ciphertext
42 byte[] ciphertext = new byte[16];
43 // update cipher with the plaintext
44 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
45 // process remaining blocks of plaintext
46 cLength += myAES.doFinal(ciphertext, cLength);
47 // print plaintext and ciphertext
48 System.out.println("plaintext: " +
49 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
50 System.out.println("ciphertext: " +
51 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
52 }
53
54 public static void encriptFromPBKG () throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException {
55 byte[] salt = new byte[16];
56 char[] password = "short_password".toCharArray();
57 int iterationCount = 10000;
58 int keySize = 128;
59 SecureRandom myPRNG = new SecureRandom();
60 myPRNG.nextBytes(salt);
61
62 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
63 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iterationCount, keySize);
64 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
65 System.out.println("AES key: " + javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
66
67 Cipher myAES = Cipher.getInstance("AES/OFB/NoPadding");
68 // initialize AES objecy to encrypt mode
69 myAES.init(Cipher.ENCRYPT_MODE, myAESPBKey);
70 // initialize plaintext
71 byte[] plaintext = new byte[16];
72 //initialize ciphertext
73 byte[] ciphertext = new byte[16];
74 // update cipher with the plaintext
75 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
76 // process remaining blocks of plaintext
77 cLength += myAES.doFinal(ciphertext, cLength);
78 // print plaintext and ciphertext
79 System.out.println("OFB encryption:\nplaintext: " +
80 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
81 System.out.println("ciphertext: " +
82 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
83
84 myAES = Cipher.getInstance("AES/CFB/NoPadding");
85 // initialize AES objecy to encrypt mode
86 myAES.init(Cipher.ENCRYPT_MODE, myAESPBKey);
87 // initialize plaintext
88 plaintext = new byte[16];
89 //initialize ciphertext
90 ciphertext = new byte[16];
91 // update cipher with the plaintext
92 cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
93 // process remaining blocks of plaintext
94 cLength += myAES.doFinal(ciphertext, cLength);
95 // print plaintext and ciphertext
96 System.out.println("\nCFB encryption:\nplaintext: " +
97 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
98 System.out.println("ciphertext: " +
99 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
100 }
101
102 public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchPaddingException, ShortBufferException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException{
103 System.out.println("CBC encription: ");
104 CBC_enc();
105 System.out.println();
106 encriptFromPBKG();
107 }
108
109}