· 7 years ago · May 09, 2018, 09:28 AM
1package cryptogaphy;
2
3
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9
10import javax.crypto.Cipher;
11import javax.crypto.CipherInputStream;
12import javax.crypto.KeyGenerator;
13import javax.crypto.SecretKey;
14import javax.crypto.spec.IvParameterSpec;
15
16import java.security.PublicKey;
17import java.security.SecureRandom;
18import java.security.cert.Certificate;
19import java.security.KeyPair;
20import java.security.Key;
21import java.security.KeyStore;
22import java.security.KeyStoreException;
23import java.security.NoSuchAlgorithmException;
24import java.security.PrivateKey;
25import java.security.UnrecoverableKeyException;
26import java.security.cert.CertificateException;
27
28
29
30public class trythree {
31
32public static void main(String[] args) throws Exception {
33
34 try {
35
36 // Generate a symmetric key
37 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
38 SecureRandom secureRandom = new SecureRandom();
39 int keyBitSize = 128;
40 keyGenerator.init(keyBitSize, secureRandom);
41 SecretKey symmetricKey = keyGenerator.generateKey();
42 Key key = symmetricKey;
43
44 // Generate random IV
45 byte[] ivBytes = new byte[16];
46 IvParameterSpec iv = new IvParameterSpec(ivBytes);
47
48 System.out.println("Random Iv: " + iv);
49
50 // File Encryption
51 FileInputStream fis = new FileInputStream("C:\Users\Victoria\Desktop\plainData.txt");
52 FileOutputStream fos = new FileOutputStream("C:\Users\Victoria\Desktop\encryptedData.txt");
53
54 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
55
56 cipher.init(Cipher.ENCRYPT_MODE, key, iv, SecureRandom.getInstance("SHA1PRNG"));
57 CipherInputStream cis = new CipherInputStream(fis, cipher);
58 write(cis, fos);
59
60
61 // load Alice's keystore - input method should be changed
62 char[] aliceStorePass = "123456".toCharArray();
63 char[] aliceKeypass = "aliceKey".toCharArray();
64
65 FileInputStream aliceInput = new FileInputStream("C:\Users\Victoria\Desktop\aliceKeystore.jks");
66 KeyStore aliceKeystore = KeyStore.getInstance("JKS");
67 aliceKeystore.load(aliceInput, aliceStorePass);
68
69 String alias = "alice";
70 String alias2 = "bobcert";
71
72 Key aliceKey = aliceKeystore.getKey(alias, aliceKeypass);
73 PublicKey bobPublicKey = null;
74
75 // retrieve Alice's private key and Bob's public key from Alice's keystore
76 if (aliceKey instanceof PrivateKey) {
77 Certificate cert = aliceKeystore.getCertificate(alias2);
78 bobPublicKey = cert.getPublicKey();
79 new KeyPair(bobPublicKey, (PrivateKey) aliceKey);
80 System.out.println((PrivateKey) aliceKey);
81 }
82 System.out.println("Bob's public key: " + bobPublicKey);
83
84 // Encrypt the Symmetric Key with asymmetric RSA algorithm
85 byte[] encryptedkey = symmetricKey.getEncoded();
86 Cipher keyCipher = Cipher.getInstance("RSA");
87 keyCipher.init(Cipher.ENCRYPT_MODE, bobPublicKey );
88 keyCipher.doFinal(encryptedkey);
89
90 System.out.println("Asymmetric Secret Key: " + encryptedkey);
91 System.out.println("Symmetric Key: " + key);
92
93
94
95} catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | CertificateException e){
96System.out.println(e);
97}
98
99 }
100
101
102
103 private static void write(InputStream in, OutputStream out) throws IOException {
104 byte[] buf = new byte[64];
105 int numOfBytesRead;
106 while((numOfBytesRead = in.read(buf))!=-1)
107 {
108 out.write(buf, 0 ,numOfBytesRead);
109 }
110 out.close();
111 in.close();
112 System.out.println("-----ENCRYPTION COMPLETED!-----");
113 }
114}