· 7 years ago · Aug 27, 2018, 11:24 AM
1AES encryption in Java
2package net.sf.andhsli.hotspotlogin;
3
4import java.security.SecureRandom;
5
6import javax.crypto.Cipher;
7import javax.crypto.KeyGenerator;
8import javax.crypto.SecretKey;
9import javax.crypto.spec.SecretKeySpec;
10
11/**
12 * Usage:
13 * <pre>
14 * String crypto = SimpleCrypto.encrypt(masterpassword, cleartext)
15 * ...
16 * String cleartext = SimpleCrypto.decrypt(masterpassword, crypto)
17 * </pre>
18 * @author ferenc.hechler
19 */
20public class SimpleCrypto {
21
22 public static String encrypt(String seed, String cleartext) throws Exception {
23 byte[] rawKey = getRawKey(seed.getBytes());
24 byte[] result = encrypt(rawKey, cleartext.getBytes());
25 return toHex(result);
26 }
27
28 public static String decrypt(String seed, String encrypted) throws Exception {
29 byte[] rawKey = getRawKey(seed.getBytes());
30 byte[] enc = toByte(encrypted);
31 byte[] result = decrypt(rawKey, enc);
32 return new String(result);
33 }
34
35 private static byte[] getRawKey(byte[] seed) throws Exception {
36 KeyGenerator kgen = KeyGenerator.getInstance("AES");
37 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
38 sr.setSeed(seed);
39 kgen.init(128, sr); // 192 and 256 bits may not be available
40 SecretKey skey = kgen.generateKey();
41 byte[] raw = skey.getEncoded();
42 return raw;
43 }
44
45
46 private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
47 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
48 Cipher cipher = Cipher.getInstance("AES");
49 cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
50 byte[] encrypted = cipher.doFinal(clear);
51 return encrypted;
52 }
53
54 private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
55 SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
56 Cipher cipher = Cipher.getInstance("AES");
57 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
58 byte[] decrypted = cipher.doFinal(encrypted);
59 return decrypted;
60 }
61
62 public static String toHex(String txt) {
63 return toHex(txt.getBytes());
64 }
65 public static String fromHex(String hex) {
66 return new String(toByte(hex));
67 }
68
69 public static byte[] toByte(String hexString) {
70 int len = hexString.length()/2;
71 byte[] result = new byte[len];
72 for (int i = 0; i < len; i++)
73 result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
74 return result;
75 }
76
77 public static String toHex(byte[] buf) {
78 if (buf == null)
79 return "";
80 StringBuffer result = new StringBuffer(2*buf.length);
81 for (int i = 0; i < buf.length; i++) {
82 appendHex(result, buf[i]);
83 }
84 return result.toString();
85 }
86 private final static String HEX = "0123456789ABCDEF";
87 private static void appendHex(StringBuffer sb, byte b) {
88 sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
89 }
90
91}
92
93public static void main(String[] args) {
94 try {
95 String plaintext = "Hello world", key = "test";
96 String ciphertext = encrypt(key, plaintext);
97 String plaintext2 = decrypt(key, ciphertext);
98 System.out.println("Encrypting '" + plaintext +
99 "' yields: (" + ciphertext.length() + ") " + ciphertext);
100 System.out.println("Decrypting it yields: " + plaintext2);
101 }
102 catch (Exception ex) {
103 ex.printStackTrace();
104 }
105}