01…
02 // LTPA 3DES 密钥
03 String ltpa3DESKey = "7dH4i81YepbVe+gF9XVUzE4C1Ca5g6A4Q69OFobJV9g=";
04 // LTPA 密钥密码
05 String ltpaPassword = "Passw0rd";
06 try {
07 // 第一步,获得加密key
08 byte[] secretKey = getSecretKey(ltpa3DESKey, ltpaPassword);
09 // 第二步,使用加密key解密ltpa Cookie
10 String ltpaPlaintext = new String(decryptLtpaToken(tokenCipher,
11 secretKey));
12 displayTokenData(ltpaPlaintext);
13 } catch (Exception e) {
14 System.out.println("Caught inner: " + e);
15 }
16…
17 //获得安全Key
18 private static byte[] getSecretKey(String ltpa3DESKey, String password)
19 throws Exception {
20 // 使用SHA获得key密码的hash值
21 MessageDigest md = MessageDigest.getInstance("SHA");
22 md.update(password.getBytes());
23 byte[] hash3DES = new byte[24];
24 System.arraycopy(md.digest(), 0, hash3DES, 0, 20);
25 // 使用0替换后4个字节
26 Arrays.fill(hash3DES, 20, 24, (byte) 0);
27 // BASE64解码 ltpa3DESKey
28 byte[] decode3DES = Base64.decodeBase64(ltpa3DESKey.getBytes());
29 // 使用key密码hash值解密已Base64解码的ltpa3DESKey
30 return decrypt(decode3DES, hash3DES);
31 }
32 //解密LtpaToken
33 public static byte[] decryptLtpaToken(String encryptedLtpaToken, byte[] key)
34 throws Exception {
35 // Base64解码LTPAToken
36 final byte[] ltpaByteArray = Base64.decodeBase64(encryptedLtpaToken
37 .getBytes());
38 // 使用key解密已Base64解码的LTPAToken
39 return decrypt(ltpaByteArray, key);
40 }
41 // DESede/ECB/PKC5Padding解方法
42 public static byte[] decrypt(byte[] ciphertext, byte[] key)
43 throws Exception {
44 final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
45 final KeySpec keySpec = new DESedeKeySpec(key);
46 final Key secretKey = SecretKeyFactory.getInstance("TripleDES")
47 .generateSecret(keySpec);
48 cipher.init(Cipher.DECRYPT_MODE, secretKey);
49 return cipher.doFinal(ciphertext);
50 }
51
…