您的位置:首页 > 编程语言 > Java开发

java中DES加密(DES/ECB/pkcs5padding)的代码分享

2017-12-27 15:38 579 查看
package com.oss.util;

import java.security.Key;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

/*** 

 * DES ECB PKCS5Padding 对称加密 解密 

 *  

 * 

 */  

public class DesECBUtil {  

    /** 

     * 加密数据 

     * @param encryptString   

     * @param encryptKey 

     * @return 

     * @throws Exception 

     */  

   public static String encryptDES(String encryptString, String encryptKey) throws Exception {  

         

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");  

        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(getKey(encryptKey), "DES"));  

        byte[] encryptedData = cipher.doFinal(encryptString.getBytes("UTF-8"));  

        return Base64.encodeBase64String(encryptedData);

    } 

  

    /** 

     * key  不足8位补位

     * @param string  

     */  

    public static byte[] getKey(String keyRule) {  

        Key key = null;  

        byte[] keyByte = keyRule.getBytes();  

        // 创建一个空的八位数组,默认情况下为0  

        byte[] byteTemp = new byte[8];  

        // 将用户指定的规则转换成八位数组  

        for (int i = 0; i < byteTemp.length && i < keyByte.length; i++) {  

            byteTemp[i] = keyByte[i];  

        }  

        key = new SecretKeySpec(byteTemp, "DES");  

        return key.getEncoded();  

    }  

      

    /*** 

     * 解密数据 

     * @param decryptString 

     * @param decryptKey 

     * @return 

     * @throws Exception 

     */  

  

    public static String decryptDES(String decryptString, String decryptKey) throws Exception {  

        

        byte[] sourceBytes = Base64.decodeBase64(decryptString);    

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");   

        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(getKey(decryptKey), "DES"));    

        byte[] decoded = cipher.doFinal(sourceBytes);    

        return new String(decoded, "UTF-8");  

        

    } 

   

    public static void main(String[] args) throws Exception {  

        String clearText = "加密的文本"; 

        String key = "123432";//密钥

        System.out.println("明文:"+clearText+"\n密钥:"+key);  

        String encryptText = encryptDES(clearText, key);  

        System.out.println("加密后:"+encryptText);  

        String decryptText = decryptDES(encryptText, key);  

        System.out.println("解密后:"+decryptText);  

    }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息