· 6 years ago · Apr 14, 2019, 12:40 PM
1import java.io.BufferedReader;
2import java.io.IOException;
3import java.io.InputStreamReader;
4import java.security.InvalidAlgorithmParameterException;
5import java.security.Key;
6import java.security.KeyPair;
7import java.security.KeyPairGenerator;
8import java.security.Security;
9import java.security.SecureRandom;
10import javax.crypto.Cipher;
11import java.security.InvalidKeyException;
12import java.security.NoSuchAlgorithmException;
13import java.security.spec.InvalidKeySpecException;
14import javax.crypto.BadPaddingException;
15import javax.crypto.IllegalBlockSizeException;
16import javax.crypto.NoSuchPaddingException;
17import javax.crypto.SecretKey;
18import javax.crypto.SecretKeyFactory;
19import javax.crypto.ShortBufferException;
20import javax.crypto.spec.IvParameterSpec;
21import javax.crypto.spec.PBEKeySpec;
22import javax.crypto.spec.SecretKeySpec;
23import javax.rmi.CORBA.Util;
24
25public class SILabSapt8
26{
27 public static void main(String[] args)
28 throws IOException, InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException
29 {
30 boolean done = false;
31 String userPassword = null;
32 BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in));
33 do
34 {
35 System.out.print("Your password : ");
36 userPassword = inputReader.readLine();
37 if(userPassword.length() < 16 || userPassword.length() > 16)
38 {
39 System.out.println("Parola trebuie sa aiba 16 caractere");
40 done = false;
41 }else
42 done = true;
43 }while(!done);
44 inputReader.close();
45 doEncription(userPassword);
46 }
47 private static void doEncription(String uPass)
48 throws InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeySpecException
49 {
50 byte[] keyBytes = new byte[16];
51 SecureRandom myPRNG = new SecureRandom();
52 myPRNG.nextBytes(keyBytes);
53 SecretKeySpec myKey = new SecretKeySpec(keyBytes,"AES");
54 Cipher myAES = Cipher.getInstance("AES/CBC/NoPadding");
55 // Initialization vector
56 byte[] iv = new byte[16];
57 IvParameterSpec ivp = new IvParameterSpec(iv);
58 // initialize AES objecy to encrypt mode
59 myAES.init(Cipher.ENCRYPT_MODE, myKey,ivp);
60 // initialize plaintext
61 byte[] plaintext = uPass.getBytes();
62 //initialize ciphertext
63 byte[] ciphertext = new byte[16];
64 // update cipher with the plaintext
65 int cLength = myAES.update(plaintext, 0, plaintext.length, ciphertext,0);
66 // process remaining blocks of plaintext
67 cLength += myAES.doFinal(ciphertext, cLength);
68 // print plaintext and ciphertext
69 System.out.println("plaintext: " +
70 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
71 System.out.println("ciphertext: " +
72 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
73 // initialize AES for decryption
74 myAES.init(Cipher.DECRYPT_MODE, myKey, ivp);
75 // myAES.init(Cipher.DECRYPT_MODE, myKey);
76 // initialize a new array of bytes to place the decryption
77 byte[] dec_plaintext = new byte[16];
78 cLength = myAES.update(ciphertext, 0, ciphertext.length, dec_plaintext,0);
79 // process remaining blocks of ciphertext
80 cLength += myAES.doFinal(dec_plaintext, cLength);
81 // print the new plaintext (hopefully identical to the initial one)
82 System.out.println("decrypted: " +
83 javax.xml.bind.DatatypeConverter.printHexBinary(dec_plaintext));
84
85 // get a Cipher instance for RSA with PKCS1 padding
86 Cipher myRSA = Cipher.getInstance("RSA/ECB/PKCS1Padding");
87 // get an instance for the Key Generator
88 KeyPairGenerator myRSAKeyGen = KeyPairGenerator.getInstance("RSA");
89 // generate an 1024 bit key
90 myRSAKeyGen.initialize(1024, myPRNG);
91 KeyPair myRSAKeyPair = myRSAKeyGen.generateKeyPair();
92 // store the public and private key individually
93 Key pbKey = myRSAKeyPair.getPublic();
94 Key pvKey = myRSAKeyPair.getPrivate();
95 // init cipher for encryption
96 myRSA.init(Cipher.ENCRYPT_MODE, pbKey, myPRNG);
97 // encrypt, as expected we encrypt a symmetric key with RSA rather than
98 ciphertext = myRSA.doFinal(keyBytes);
99 // init cipher for decryption
100 myRSA.init(Cipher.DECRYPT_MODE, pvKey);
101 // decrypt
102 plaintext = myRSA.doFinal(ciphertext);
103 System.out.println("plaintext: " +
104 javax.xml.bind.DatatypeConverter.printHexBinary(plaintext));
105 System.out.println("ciphertext: " +
106 javax.xml.bind.DatatypeConverter.printHexBinary(ciphertext));
107 System.out.println("keybytes: " +
108 javax.xml.bind.DatatypeConverter.printHexBinary(keyBytes));
109
110
111 char[] password = uPass.toCharArray();
112 byte[] salt = new byte[16];
113 int iteration_count = 10000;
114 int key_size = 128;
115 // set salt values to random
116 myPRNG.nextBytes(salt);
117 // initialize key factory for HMAC-SHA1 derivation
118 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
119 // set key specification
120 PBEKeySpec pbekSpec = new PBEKeySpec(password, salt, iteration_count,key_size);
121 // generate the key
122 SecretKey myAESPBKey = new SecretKeySpec(keyFactory.generateSecret(pbekSpec).getEncoded(), "AES");
123 // print the key
124 System.out.println("AES key: " +javax.xml.bind.DatatypeConverter.printHexBinary(myAESPBKey.getEncoded()));
125 }
126}