· 7 years ago · Mar 14, 2018, 09:42 AM
1package crypto;
2import java.security.Key;
3import java.security.KeyPair;
4import java.security.KeyPairGenerator;
5import java.security.SecureRandom;
6import java.security.interfaces.RSAPrivateCrtKey;
7import java.security.interfaces.RSAPrivateKey;
8import java.security.interfaces.RSAPublicKey;
9import java.security.spec.RSAKeyGenParameterSpec;
10import java.util.ArrayList;
11import java.util.Formatter;
12
13import javax.crypto.Cipher;
14import javax.crypto.KeyGenerator;
15import javax.crypto.SecretKey;
16import javax.crypto.SecretKeyFactory;
17import javax.crypto.spec.GCMParameterSpec;
18import javax.crypto.spec.PBEKeySpec;
19import javax.crypto.spec.SecretKeySpec;
20
21import java.awt.List;
22import java.io.BufferedReader;
23import java.io.FileNotFoundException;
24import java.io.FileReader;
25import java.io.IOException;
26import java.math.BigInteger;
27public class asdasd {
28
29 /**
30 * @param args
31 * @throws Exception
32 */
33 public static void main(String[] args) throws Exception {
34
35 String pass = "password";
36 String salty = "0E0A4A729188F12F52C85F692DDAD154";
37 String hexIV = "f8f3abeec32dc69e75d4451093f9bcdb";
38 String encryptedText = "7158C91A594DF409A07819A15F7D663520F07C784C4B2C0E4AFA5CB5FF59";
39 byte [] salt = hexStringToByteArray(salty);
40 //byte [] pswd = hexStringToByteArray(pass);
41 byte [] iv = hexStringToByteArray(hexIV);
42 byte [] cipher = hexStringToByteArray(encryptedText);
43 SecretKey trying = deriveAESkeyfromPbkdf2(pass.toCharArray(),salt,1000,256);
44 byte[] trophy = decryptWithAESGCM(trying, iv, 128, salt, cipher);
45 String tphy = convertBytesToHexString(trophy);
46 System.out.println(salt);
47 System.out.println(tphy);
48 }
49 private static String convertBytesToHexString(byte[] pbkdf2) {
50 Formatter formatter = new Formatter();
51 for (byte b : pbkdf2)
52 {
53 formatter.format("%02x", b);
54 }
55 String result = formatter.toString();
56 formatter.close();
57 return result;
58 }
59 public static byte[] hexStringToByteArray(String s) {
60 int len = s.length();
61 byte[] data = new byte[len / 2];
62 for (int i = 0; i < len; i += 2) {
63 data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
64 + Character.digit(s.charAt(i+1), 16));
65 }
66 return data;
67 }
68
69 public static SecretKey deriveAESkeyfromPbkdf2(char[] passwd, byte[] salt, int iteration, int keyLen) throws Exception
70 {
71 //init PBKDF2
72 SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
73 PBEKeySpec keySpec = new PBEKeySpec(passwd, salt, iteration, keyLen);
74
75 // derive secret key
76 SecretKey s = keyFac.generateSecret(keySpec);
77 SecretKey key = new SecretKeySpec(s.getEncoded(),"AES");
78
79 return key;
80 }
81
82 public static byte[] decryptWithAESGCM(SecretKey key, byte[] iv, int tagLen, byte[] aad, byte[] ciphertext) throws Exception
83 {
84 Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
85 GCMParameterSpec spec = new GCMParameterSpec(tagLen, iv);
86 cipher.init(Cipher.DECRYPT_MODE, key, spec);
87
88 cipher.updateAAD(aad);
89
90 byte[] plainText = cipher.doFinal(ciphertext);
91
92 return plainText;
93 }
94}