· 9 years ago · Jan 17, 2017, 04:18 AM
1byte[] a = encryptFIN128AES("pls");
2String b = decryptFIN128AES(a);
3Log.e("AES_Test", "b = " + b);
4
5/**
6 * Encrypts a string with AES (128 bit key)
7 * @param fin
8 * @return the AES encrypted string
9 */
10 private byte[] encryptFIN128AES(String fin) {
11
12 SecretKeySpec sks = null;
13
14 try {
15 sks = new SecretKeySpec(generateKey(PASSPHRASE, SALT.getBytes(StandardCharsets.UTF_8)).getEncoded(), "AES");
16 } catch (Exception e) {
17 Log.e("encryptFIN128AES", "AES key generation error");
18 }
19
20 // Encode the original data with AES
21 byte[] encodedBytes = null;
22 try {
23 Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
24 c.init(Cipher.ENCRYPT_MODE, sks);
25 encodedBytes = c.doFinal(fin.getBytes(StandardCharsets.UTF_8));
26 } catch (Exception e) {
27 Log.e("encryptFIN128AES", "AES encryption error");
28 }
29
30 return encodedBytes;
31
32 }
33
34
35 /**
36 * Decrypts a string with AES (128 bit key)
37 * @param encodedBytes
38 * @return the decrypted String
39 */
40 private String decryptFIN128AES(byte[] encodedBytes) {
41
42 SecretKeySpec sks = null;
43
44 try {
45 sks = new SecretKeySpec(generateKey(PASSPHRASE, SALT.getBytes(StandardCharsets.UTF_8)).getEncoded(), "AES");
46 } catch (Exception e) {
47 Log.e("decryptFIN128AES", "AES key generation error");
48 }
49
50 // Decode the encoded data with AES
51 byte[] decodedBytes = null;
52 try {
53 Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
54 c.init(Cipher.DECRYPT_MODE, sks);
55 decodedBytes = c.doFinal(encodedBytes);
56 } catch (Exception e) {
57 Log.e("decryptFIN128AES", "AES decryption error");
58 }
59
60 //return Base64.encodeToString(decodedBytes, Base64.DEFAULT);
61 return new String(decodedBytes, StandardCharsets.UTF_8);
62 }
63
64
65/**
66 * Build private key from a passpharase/PIN (incl. key derivation (Uses PBKDF2))
67 * @param passphraseOrPin
68 * @param salt
69 * @return The generated SecretKey (Used for AES-encryption, key size specified in outputKeyLength)
70 */
71 public static SecretKey generateKey(char[] passphraseOrPin, byte[] salt)
72 throws NoSuchAlgorithmException, InvalidKeySpecException {
73 // Number of PBKDF2 hardening rounds to use. Larger values increase
74 // computation time. You should select a value that causes computation
75 // to take >100ms.
76 final int iterations = 1000;
77
78 // Generate a 256-bit key
79 final int outputKeyLength = 128;
80
81 SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
82 KeySpec keySpec = new PBEKeySpec(passphraseOrPin, salt, iterations, outputKeyLength);
83 SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
84 return secretKey;
85 }
86
87E/decryptFIN128AES: AES decryption error
88E/AndroidRuntime: FATAL EXCEPTION: Thread-176
89 Process: testapp.ttyi.nfcapp, PID: 2920
90 java.lang.NullPointerException: Attempt to get length of null array
91 at java.lang.String.<init>(String.java:371)
92 at testapp.ttyi.nfcapp.DisplayQRActivity.decryptFIN128AES(DisplayQRActivity.java:254)
93 at testapp.ttyi.nfcapp.DisplayQRActivity.access$100(DisplayQRActivity.java:29)
94 at testapp.ttyi.nfcapp.DisplayQRActivity$1.run(DisplayQRActivity.java:77)
95 at java.lang.Thread.run(Thread.java:818)