0
回答
Des3
注册华为云得mate10,2.9折抢先购!>>>   
package com.pawf.controller.base;


import java.security.Key;  


import javax.crypto.Cipher;  
import javax.crypto.SecretKeyFactory;  
import javax.crypto.spec.DESedeKeySpec;  
import javax.crypto.spec.IvParameterSpec;  


import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
  
/** 
 * 3DES加密工具类 
 *  
 * @author liufeng  
 * @date 2012-10-11 
 */  
public class Des3 {  
    // 密钥  
    private static String secretKey = "pawifi1234567890123456789";  
    // 向量  
    private static String iv = "01234567";  
    // 加解密统一使用的编码方式  
    private static String encoding = "utf-8";  
  
    /** 
     * 3DES加密 
     *  
     * @param plainText 普通文本 
     * @return 
     * @throws Exception  
     */  
    public static String encode(String plainText,String key) throws Exception {  
        Key deskey = null;
        if(key==null){
        key = secretKey;
        }
        DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.ENCRYPT_MODE, deskey, ips);  
        byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
        return Base64.encode(encryptData);  
    }  
  
    /** 
     * 3DES解密 
     *  
     * @param encryptText 加密文本 
     * @return 
     * @throws Exception 
     */  
    public static String decode(String encryptText,String key) throws Exception {  
        Key deskey = null;  
        if(key==null){
        key = secretKey;
        }
        DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());  
        SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
        deskey = keyfactory.generateSecret(spec);  
        Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");  
        IvParameterSpec ips = new IvParameterSpec(iv.getBytes());  
        cipher.init(Cipher.DECRYPT_MODE, deskey, ips);  
  
        byte[] decryptData = cipher.doFinal(Base64.decode(encryptText));  
  
        return new String(decryptData, encoding);  
    }  
    
    /** 
     * 3DES加密 
     *  
     * @param plainText 普通文本 
     * @return 
     * @throws Exception  
     */  
    public static String encodeEcb(String plainText,String key) throws Exception {  
    Key deskey = null;
    if(key==null){
    key = secretKey;
    }
    DESedeKeySpec spec = new DESedeKeySpec(key.getBytes());  
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede");  
    deskey = keyfactory.generateSecret(spec);  
   
    Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, deskey);  
    byte[] encryptData = cipher.doFinal(plainText.getBytes(encoding));  
    return bytesToHexString(encryptData);
    }  
  


/*
* Convert byte[] to hex
* string.这里我们可以将byte转换成int,然后利用Integer.toHexString(int)来转换成16进制字符串。

* @param src byte[] data

* @return hex string
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}

public static void main(String[] args) {
try {
//105d7b9773a312d704f1f4a5     生产秘钥
System.out.println(Des3.decode("PYN+MX8EpkTTlV8e71Aq9JKHaqqc1Hp4", "105d7b9773a312d704f1f4a5"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


<无标签>
举报
北极之北
发帖于1年前 0回/105阅
顶部