· 7 years ago · Jun 27, 2018, 02:22 AM
1Private Function Decrypt(cipherText As String) As String
2dim _encryptionkey as string = "kmjfds(#1231SDSA()#rt32geswfkjFJDSKFJDSFd"
3 Dim cipherBytes As Byte() = Convert.FromBase64String(cipherText)
4 Using encryptor As Aes = Aes.Create()
5 Dim pdb As New Rfc2898DeriveBytes(_EncryptionKey, New Byte() {&H49, &H76, &H61, &H6E, &H20, &H4D, &H65, &H64, &H76, &H65, &H64, &H65, _
6 &H76})
7 encryptor.Key = pdb.GetBytes(32)
8 encryptor.IV = pdb.GetBytes(16)
9 Using ms As New MemoryStream()
10 Using cs As New CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write)
11 cs.Write(cipherBytes, 0, cipherBytes.Length)
12 cs.Close()
13 End Using
14 cipherText = Encoding.Unicode.GetString(ms.ToArray())
15 End Using
16 End Using
17 Return cipherText
18 End Function
19
20String myData = "kgxCSfBSw5BRxmjgc4qYhwN12dxG0dyf=";
21 byte[] salt = new byte[] {0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
22 String pw = "kmjfds(#1231SDSA()#rt32geswfkjFJDSKFJDSFd";
23
24 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
25 PBEKeySpec pbeKeySpec = new PBEKeySpec(pw.toCharArray(), salt, 1000, 384);
26 Key secretKey = factory.generateSecret(pbeKeySpec);
27 byte[] key = new byte[32];
28 byte[] iv = new byte[16];
29 System.arraycopy(secretKey.getEncoded(), 0, key, 0, 32);
30 System.arraycopy(secretKey.getEncoded(), 32, iv, 0, 16);
31
32
33 SecretKeySpec secretSpec = new SecretKeySpec(key, "AES");
34 AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
35 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
36 Cipher cipher1 = Cipher.getInstance("AES/CBC/PKCS5Padding");
37
38 try {
39 cipher.init(Cipher.DECRYPT_MODE,secretSpec,ivSpec);
40 cipher1.init(Cipher.ENCRYPT_MODE,secretSpec,ivSpec);
41 } catch (InvalidKeyException e) {
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 } catch (InvalidAlgorithmParameterException e) {
45 // TODO Auto-generated catch block
46 e.printStackTrace();
47 }
48
49 //byte[] decordedValue;
50 //decordedValue = new BASE64Decoder().decodeBuffer(myData);
51 //decordedValue = myData.getBytes("ISO-8859-1");
52 //byte[] decValue = cipher.doFinal(myData.getBytes());
53 //Base64.getMimeEncoder().encodeToString(cipher.doFinal(myData.getBytes()));
54 //String decryptedValue = new String(decValue);
55 byte[] decodedValue = new Base64().decode(myData.getBytes());
56
57
58
59 String clearText = "ljfva09876FK";
60
61
62 //String encodedValue = new Base64().encodeAsString(clearText.getBytes("UTF-16"));
63
64
65
66 byte[] cipherBytes = cipher1.doFinal(clearText.getBytes("UTF-16LE"));
67 //String cipherText = new String(cipherBytes, "UTF8");
68 String encoded = Base64.encodeBase64String(cipherBytes);
69 System.out.println(encoded);
70
71
72 byte[] decValue = cipher.doFinal(decodedValue);
73
74 System.out.println(new String(decValue, StandardCharsets.UTF_16LE));
75
76PBEKeySpec pbeKeySpec = new PBEKeySpec(pw.toCharArray(), salt, 1000, 384);
77
78new String(decValue, StandardCharsets.UTF_16LE)
79
80String myData = "kgxCSfBSw5BRxmjgc4qYhwN12dxG0=";
81 byte[] salt = new byte[] {0x49, 0x76, 0x61, 0x6E, 0x20, 0x4D, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76};
82 String pw = "kmjfds(#1231SDSA()#rt32geswfkjFJDSKFJDSFd";
83
84 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
85 PBEKeySpec pbeKeySpec = new PBEKeySpec(pw.toCharArray(), salt, 1000, 384);
86 Key secretKey = factory.generateSecret(pbeKeySpec);
87 byte[] key = new byte[32];
88 byte[] iv = new byte[16];
89 System.arraycopy(secretKey.getEncoded(), 0, key, 0, 32);
90 System.arraycopy(secretKey.getEncoded(), 32, iv, 0, 16);
91
92
93 SecretKeySpec secretSpec = new SecretKeySpec(key, "AES");
94 AlgorithmParameterSpec ivSpec = new IvParameterSpec(iv);
95 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
96 try {
97 cipher.init(Cipher.DECRYPT_MODE,secretSpec,ivSpec);
98 } catch (InvalidKeyException e) {
99 // TODO Auto-generated catch block
100 e.printStackTrace();
101 } catch (InvalidAlgorithmParameterException e) {
102 // TODO Auto-generated catch block
103 e.printStackTrace();
104 }
105
106 //byte[] decordedValue;
107 //decordedValue = new BASE64Decoder().decodeBuffer(myData);
108 //decordedValue = myData.getBytes("ISO-8859-1");
109 //byte[] decValue = cipher.doFinal(myData.getBytes());
110 //Base64.getMimeEncoder().encodeToString(cipher.doFinal(myData.getBytes()));
111 //String decryptedValue = new String(decValue);
112 byte[] decodedValue = new Base64().decode(myData.getBytes());
113
114
115 byte[] decValue = cipher.doFinal(decodedValue);
116
117 System.out.println(new String(decValue, StandardCharsets.UTF_16LE));