· 5 years ago · Dec 03, 2019, 06:56 AM
1package javaapplication21;
2import java.security.Key;
3import java.security.KeyPair;
4import java.security.KeyPairGenerator;
5import java.security.Security;
6import java.security.SecureRandom;
7import javax.crypto.Cipher;
8import java.security.InvalidKeyException;
9import java.security.NoSuchAlgorithmException;
10import java.security.spec.InvalidKeySpecException;
11import javax.crypto.BadPaddingException;
12import javax.crypto.IllegalBlockSizeException;
13import javax.crypto.NoSuchPaddingException;
14import javax.crypto.SecretKey;
15import javax.crypto.SecretKeyFactory;
16import javax.crypto.ShortBufferException;
17import javax.crypto.spec.PBEKeySpec;
18import javax.crypto.spec.SecretKeySpec;
19import javax.rmi.CORBA.Util;
20
21
22/**
23 *
24 * @author student
25 */
26public class JavaApplication21 {
27
28 public static byte[] hexStringToByteArray(String s) {
29 int len = s.length();
30 byte[] data = new byte[len / 2];
31 for (int i = 0; i < len; i += 2) {
32 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
33 + Character.digit(s.charAt(i+1), 16));
34 }
35 return data;
36}
37
38
39 public static void main(String[] args) throws ShortBufferException, IllegalBlockSizeException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, BadPaddingException, InvalidKeySpecException {
40 byte[] keyBytes = new byte[16];
41 // declare secure PRNG
42 SecureRandom myPRNG = new SecureRandom();
43 myPRNG.nextBytes(keyBytes);
44 SecretKeySpec myKey = new SecretKeySpec(keyBytes, "AES");
45 // instantiate AES object for ECB with no padding
46 Cipher myAES = Cipher.getInstance("AES/ECB/NoPadding");
47 // initialize AES objecy to encrypt mode
48 myAES.init(Cipher.ENCRYPT_MODE, myKey);
49 // initialize plaintext
50 byte[] plaintext = new byte[16]; // ii dai tu un text
51 plaintext = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d");
52 //initialize ciphertext
53 byte[] ciphertext = new byte[16];
54 // update cipher with the plaintext
55 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,
56 0);
57 // process remaining blocks of plaintext
58 cLength += myAES.doFinal(ciphertext, cLength);
59 // print plaintext and ciphertext
60 System.out.println("plaintext: " +
61 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
62 System.out.println("ciphertext: " +
63 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
64 // initialize AES for decryption
65 myAES.init(Cipher.DECRYPT_MODE, myKey);
66 // initialize a new array of bytes to place the decryption
67 byte[] dec_plaintext = new byte[16];
68 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,
69 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: " +
74 javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
75 // TODO code application logic here
76 char[] password = "short_password".toCharArray();
77 byte[] salt = new byte[16];
78 int iteration_count = 10000;
79 int key_size = 128;
80 // set salt values to random
81 myPRNG.nextBytes(salt);
82
83 // initialize key factory for HMAC-SHA1 derivation
84 SecretKeyFactory keyFactory =
85 SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
86 // set key specification
87 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,
88 key_size);
89 // generate the key
90 SecretKey myAESPBKey = new SecretKeySpec(
91 keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
92 // print the key
93 System.out.println("AES key: " +
94 javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
95 }
96
97}