· 7 years ago · Dec 04, 2018, 07:22 AM
1import javax.crypto.*;
2import java.security.*;
3
4public class Symmetric {
5 private static Cipher cipher;
6 private static KeyGenerator keyGen;
7 private static SecretKey secretKey;
8
9 /**
10 * Symmetric Constructor
11 * @author Matt Moore
12 * @throws NoSuchAlgorithmException
13 * @throws NoSuchPaddingException
14 */
15 public Symmetric() throws NoSuchAlgorithmException, NoSuchPaddingException {
16 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); //transformation
17 keyGen = KeyGenerator.getInstance("AES"); //must match alg used in trasnformation
18 secretKey = keyGen.generateKey(); //secret key
19 }
20
21 public static void main(String args[])
22 {
23 try {
24
25 Asymmetric a = new Asymmetric();
26 PublicKey pkTest = a.getPublic();
27
28 Symmetric s = new Symmetric();
29 System.out.println(sessionKey(pkTest));
30
31
32
33 } catch (InvalidKeyException e) {
34 // TODO Auto-generated catch block
35 e.printStackTrace();
36 } catch (IllegalBlockSizeException e) {
37 // TODO Auto-generated catch block
38 e.printStackTrace();
39 } catch (BadPaddingException e) {
40 // TODO Auto-generated catch block
41 e.printStackTrace();
42 } catch (NoSuchAlgorithmException e) {
43 // TODO Auto-generated catch block
44 e.printStackTrace();
45 } catch (NoSuchPaddingException e) {
46 // TODO Auto-generated catch block
47 e.printStackTrace();
48 }
49 }
50
51
52
53 /**
54 * Encrypts the provided string (the given message)
55 * @author Matt Moore
56 * @param msg
57 * @return byte[], cipherText - encrypted string (as an array of bytes)
58 * @throws InvalidKeyException
59 * @throws IllegalBlockSizeException
60 * @throws BadPaddingException
61 */
62 public byte[] encrypt(String msg) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
63 //initialize cipher object
64 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
65 //convert string msg to array of bytes
66 byte[] plainText = msg.getBytes();
67 //call update repeatedly for a long msg
68 return cipher.doFinal(plainText);
69 }
70
71 /**
72 * @author Matt Moore
73 * @param pubKey
74 * @return byte array - an encrypted secret key, using the given public key
75 * @throws InvalidKeyException
76 * @throws BadPaddingException
77 * @throws IllegalBlockSizeException
78 * @throws NoSuchAlgorithmException
79 * @throws NoSuchPaddingException
80 */
81 public static byte[] sessionKey(PublicKey pubKey) throws
82 InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchPaddingException
83 {
84 // Instantiate and Initialize RSA cipher
85 Cipher cipher = Cipher.getInstance("RSA");
86 cipher.init(Cipher.ENCRYPT_MODE, pubKey);
87
88 //Encrypt & Return
89 return cipher.doFinal(secretKey.getEncoded());
90 }
91
92 /**
93 * Decrypts the provided byte array (the cipherText)
94 * @author Matt Moore
95 * @param cipherText
96 * @return String - the plainText, cipherText decrypted
97 * @throws InvalidKeyException
98 * @throws IllegalBlockSizeException
99 * @throws BadPaddingException
100 */
101 public String decrypt(byte[] cipherText) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
102 //initialize cipher object
103 cipher.init(Cipher.DECRYPT_MODE, secretKey); //same key, SYMMETRIC
104 byte[] plainText = cipher.doFinal(cipherText);
105 return new String(plainText);
106 }
107
108}