美文网首页
DES加密、解密

DES加密、解密

作者: 简书_捡书 | 来源:发表于2017-12-13 16:12 被阅读0次

加密、解密操作

/** 
 * 
 * <p> 
 * 采用JDK内置类进行真正的加密操作 
 * @param byteS 
 * @param password 
 * @return byte[]    
 */  
private static byte[] encryptByte(byte[] byteS, byte password[]) {  
    byte[] byteFina = null;  
    try {// 初始化加密/解密工具  
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");  
        DESKeySpec desKeySpec = new DESKeySpec(password);  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);  
        IvParameterSpec iv = new IvParameterSpec(keys);  
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);  
        byteFina = cipher.doFinal(byteS);  
    } catch (Exception e) {  
        throw new RuntimeException(e);  
    }  
    return byteFina;  
}  

/** 
 * 
 * <p> 
 * 采用JDK对应的内置类进行解密操作 
 * @param byteS 
 * @param password 
 * @return byte[]     
 */  
private static byte[] decryptByte(byte[] byteS, byte password[]) {  
    byte[] byteFina = null;  
    try {// 初始化加密/解密工具  
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");  
        DESKeySpec desKeySpec = new DESKeySpec(password);  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");  
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);  
        IvParameterSpec iv = new IvParameterSpec(keys);  
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);  
        byteFina = cipher.doFinal(byteS);  
    } catch (Exception e) {  
        throw new RuntimeException(e);  
    }  
    return byteFina;  
}  

在实际加密或解密的过程后又将结果用base64包起来。

/** 
 * 
 * <p> 
 * Des加密strMing,然后base64转换 
 * @param strMing 
 * @param md5key 
 * @return String   
 */  
public static String encryptStr(String strMing, byte md5key[]) {  
    byte[] byteMi = null;  
    byte[] byteMing = null;  
    String strMi = "";  
    try {  
        byteMing = strMing.getBytes("utf-8");  
        byteMi = encryptByte(byteMing, md5key);  
        BASE64Encoder base64Encoder = new BASE64Encoder();  
        strMi = base64Encoder.encode(byteMi);  
    } catch (Exception e) {  
        throw new RuntimeException(e);  
    } finally {  
        byteMing = null;  
        byteMi = null;  
    }  
    return strMi;  
}  

/** 
 * 
 * <p> 
 * Base64转换strMi,然后进行des解密 
 * @param strMi 
 * @param md5key 
 * @return String    
 */  
public static String decryptStr(String strMi, byte md5key[]) {  
    byte[] byteMing = null;  
    String strMing = "";  
    try {  
        BASE64Decoder decoder = new BASE64Decoder();  
        byteMing = decoder.decodeBuffer(strMi);  
        byteMing = decryptByte(byteMing, md5key);  
        strMing = new String(byteMing);  
    } catch (Exception e) {  
        throw new RuntimeException(e);  
    } finally {  
        byteMing = null;  
    }  
    return strMing;  
}  

同时可以将字符集编码通过参数的形式进行传输。 保证客户端与服务器之间字符集相同

相关文章

网友评论

      本文标题:DES加密、解密

      本文链接:https://www.haomeiwen.com/subject/nkqzixtx.html