· 10 years ago · Mar 30, 2016, 10:33 PM
1public static void main(String[] args) throws Exception {
2 String username = "bob@google.org";
3 String password = "Password1";
4 String secretID = "BlahBlahBlah";
5 String SALT2 = "deliciously salty";
6
7 // Get the Key
8 byte[] key = (SALT2 + username + password).getBytes();
9 System.out.println((SALT2 + username + password).getBytes().length);
10
11 // Need to pad key for AES
12 // TODO: Best way?
13
14 // Generate the secret key specs.
15 SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
16
17 // Instantiate the cipher
18 Cipher cipher = Cipher.getInstance("AES");
19 cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
20
21 byte[] encrypted = cipher.doFinal((secrectID).getBytes());
22 System.out.println("encrypted string: " + asHex(encrypted));
23
24 cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
25 byte[] original = cipher.doFinal(encrypted);
26 String originalString = new String(original);
27 System.out.println("Original string: " + originalString + "nOriginal string (Hex): " + asHex(original));
28}
29
30byte[] key = (SALT2 + username + password).getBytes("UTF-8");
31MessageDigest sha = MessageDigest.getInstance("SHA-1");
32key = sha.digest(key);
33key = Arrays.copyOf(key, 16); // use only first 128 bit
34
35SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
36
37PBEKeySpec pbeKeySpec;
38 PBEParameterSpec pbeParamSpec;
39 SecretKeyFactory keyFac;
40
41 // Salt
42 byte[] salt = {
43 (byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
44 (byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
45 };
46
47 // Iteration count
48 int count = 20;
49
50 // Create PBE parameter set
51 pbeParamSpec = new PBEParameterSpec(salt, count);
52
53 // Prompt user for encryption password.
54 // Collect user password as char array (using the
55 // "readPassword" method from above), and convert
56 // it into a SecretKey object, using a PBE key
57 // factory.
58 System.out.print("Enter encryption password: ");
59 System.out.flush();
60 pbeKeySpec = new PBEKeySpec(readPassword(System.in));
61 keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
62 SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
63
64 // Create PBE Cipher
65 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
66
67 // Initialize PBE Cipher with key and parameters
68 pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
69
70 // Our cleartext
71 byte[] cleartext = "This is another example".getBytes();
72
73 // Encrypt the cleartext
74 byte[] ciphertext = pbeCipher.doFinal(cleartext);
75
76import java.security.Key;
77import javax.crypto.Cipher;
78import javax.crypto.spec.SecretKeySpec;
79import sun.misc.*;
80import java.io.BufferedReader;
81import java.io.FileReader;
82
83public class AESFile
84{
85private static String algorithm = "AES";
86private static byte[] keyValue=new byte[] {'0','2','3','4','5','6','7','8','9','1','2','3','4','5','6','7'};// your key
87
88 // Performs Encryption
89 public static String encrypt(String plainText) throws Exception
90 {
91 Key key = generateKey();
92 Cipher chiper = Cipher.getInstance(algorithm);
93 chiper.init(Cipher.ENCRYPT_MODE, key);
94 byte[] encVal = chiper.doFinal(plainText.getBytes());
95 String encryptedValue = new BASE64Encoder().encode(encVal);
96 return encryptedValue;
97 }
98
99 // Performs decryption
100 public static String decrypt(String encryptedText) throws Exception
101 {
102 // generate key
103 Key key = generateKey();
104 Cipher chiper = Cipher.getInstance(algorithm);
105 chiper.init(Cipher.DECRYPT_MODE, key);
106 byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedText);
107 byte[] decValue = chiper.doFinal(decordedValue);
108 String decryptedValue = new String(decValue);
109 return decryptedValue;
110 }
111
112//generateKey() is used to generate a secret key for AES algorithm
113 private static Key generateKey() throws Exception
114 {
115 Key key = new SecretKeySpec(keyValue, algorithm);
116 return key;
117 }
118
119 // performs encryption & decryption
120 public static void main(String[] args) throws Exception
121 {
122 FileReader file = new FileReader("C://myprograms//plaintext.txt");
123 BufferedReader reader = new BufferedReader(file);
124 String text = "";
125 String line = reader.readLine();
126 while(line!= null)
127 {
128 text += line;
129 line = reader.readLine();
130 }
131 reader.close();
132 System.out.println(text);
133
134 String plainText = text;
135 String encryptedText = AESFile.encrypt(plainText);
136 String decryptedText = AESFile.decrypt(encryptedText);
137
138 System.out.println("Plain Text : " + plainText);
139 System.out.println("Encrypted Text : " + encryptedText);
140 System.out.println("Decrypted Text : " + decryptedText);
141 }
142}