wangting
2024-12-26 fa261e8c1220b31af54e8167e4de9c3320b1af27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.vci.client.logon.base;
 
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
 
import sun.misc.BASE64Decoder;
 
public class EncryptData {
 
    byte[] encryptKey;
    DESedeKeySpec spec;
    SecretKeyFactory keyFactory;
    SecretKey theKey;
    Cipher cipher;
    IvParameterSpec IvParameters;
 
    /**
     * 
     * @param encKey 双方协定的共享密钥字符串,如:"www.liming.portal.com"
     */
    public EncryptData(String encKey) {
        try {
            this.encryptKey = encKey.getBytes();
            this.spec = new DESedeKeySpec(this.encryptKey);
            this.keyFactory = SecretKeyFactory.getInstance("DESede");
            this.theKey = this.keyFactory.generateSecret(this.spec);
            this.cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
            this.IvParameters = new IvParameterSpec(new byte[] { 12, 34, 56,
                    78, 90, 87, 65, 43 });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    //
    /**
     * 
     * @param sPwd,需解密的加密字符串,由UID和时间戳构成
     * @return
     */
    public String decrypt(String sPwd) {
        String decrypted_password = null;
        try {
            byte[] password = new BASE64Decoder().decodeBuffer(sPwd);
            this.cipher.init(2, this.theKey, this.IvParameters);
            byte[] decryptedPassword = password;
            byte[] decrypted_pwd = this.cipher.doFinal(decryptedPassword);
            decrypted_password = new String(decrypted_pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decrypted_password;
    }
}