DES CBC java加密 php解密

java DES-CBC 加解密代码:

package cn.sh.ideal.util;

import java.io.IOException;
import java.net.URLEncoder;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Date;
import java.util.ResourceBundle;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

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

import sun.misc.BASE64Decoder;

public class DesUtil {

    private static byte[] IV = { 0x12, 0x34, 0x56, 0x78, (byte) 0x90,
            (byte) 0xAB, (byte) 0xCD, (byte) 0xEF };

    /**
     * 加密
     * @param data 待加密的明文
     * @param key 加密密钥
     * @return 密文
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec ks = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        SecretKey sk = skf.generateSecret(ks);
        Cipher cip = Cipher.getInstance("DES/CBC/PKCS5Padding");// Cipher.getInstance("DES");
        IvParameterSpec ivSpec = new IvParameterSpec(IV);
        cip.init(Cipher.ENCRYPT_MODE, sk, ivSpec);// IV的方式
        // cip.init(Cipher.ENCRYPT_MODE, sk, sr);//没有传递IV
        return new String(new Base64().encode(cip.doFinal(data
                .getBytes("UTF-8"))));

    }

    /**
     * 解密
     * @param data 待解密的密文
     * @param key 解密密钥
     * @return 明文
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null) {
            return null;
        }

        byte[] buf = new Base64().decode(data.getBytes("UTF-8"));

        SecureRandom sr = new SecureRandom();

        DESKeySpec dks = new DESKeySpec(key.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey securekey = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        IvParameterSpec ivSpec = new IvParameterSpec(IV);
        cipher.init(Cipher.DECRYPT_MODE, securekey, ivSpec);

        // cipher.init(Cipher.DECRYPT_MODE, securekey, sr);//没有传递IV

        return new String(cipher.doFinal(buf));
    }

    public static void main(String[] args) throws Exception {
        String desKey = "1A426B316FB648";
        String params = "1383838438";
        System.out.println("原文:"+params);
        System.out.println("密码:"+desKey);
        String encryptStr = DesUtil.encrypt(params,desKey).replace("+","A_a");
        System.out.println("加密后:"+encryptStr);
        String decryptStr = DesUtil.decrypt(encryptStr.replace("A_a", "+"), desKey);
        System.out.println("解密后:"+decryptStr);
        

    }

}

 

php DES-CBC加解密代码(需要开启openssl php扩展):

$iv = "\x12\x34\x56\x78\x90\xAB\xCD\xEF\.";
$str = "13838384388";
$en_str = openssl_encrypt($str, 'des-cbc', "1A426B316FB648",0,$iv);
echo $en_str."<br/>";
$de_str = openssl_decrypt($en_str, 'des-cbc', "1A426B316FB648",0,$iv);
echo $de_str;

 

附 php AES加解密代码:

class AESMcrypt{


    /** 
     * 设置默认的加密key 32位
     * @var str 
     * 为了保密省略后半部分
     */ 
    private static $defaultKey = "1A426B316FB648";

    /** 
     * 设置默认加密向量
     * @var str
     * 为了保密省略后半部分
     */
     
    //在.net/java中的格式为
    //$iv='{ 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF}';
    
    private $iv = "\x12\x34\x56\x78\x90\xAB\xCD\xEF\.";
     
    /** 
     * 设置加密算法 
     * @var str 
     */ 
    private $cipher; 
     
    /** 
     * 设置加密模式 
     * @var str 
     */ 
    private $mode; 
     
    public function __construct($cipher = MCRYPT_RIJNDAEL_128, $mode = MCRYPT_MODE_CBC){ 
        $this->cipher = $cipher; 
        $this->mode = $mode; 
    } 
     
    /** 
     * 对内容加密,注意此加密方法中先对内容使用padding pkcs7,然后再加密。 
     * @param str $content    需要加密的内容 
     * @return str 加密后的密文 
     */ 
    public function encrypt($content){ 
        if(empty($content)){
            return null; 
        }
        
        $srcdata = $this->addPkcs7Padding($content);
        return mcrypt_encrypt($this->cipher, $this->getSecretKey(), $srcdata, $this->mode, $this->iv);
    }
    
    /**
     * pkcs7补码
     *
     * @param string $string  明文
     *
     * @return String
     */ 
    function addPkcs7Padding($string) {
        $blocksize = mcrypt_get_block_size($this->cipher, $this->mode);
        $len = strlen($string); //取得字符串长度
        $pad = $blocksize - ($len % $blocksize); //取得补码的长度
        $string .= str_repeat(chr($pad), $pad); //用ASCII码为补码长度的字符, 补足最后一段
        return $string;
    }

    /** 
     * 对内容解密,注意此加密方法中先对内容解密。再对解密的内容使用padding pkcs7去除特殊字符。 
     * @param String $content    需要解密的内容 
     * @return String 解密后的内容 
     */ 
    public function decrypt($content){ 
        if(empty($content)){ 
            return null; 
        } 

        $content = mcrypt_decrypt($this->cipher, $this->getSecretKey(), $content, $this->mode, $this->iv); 
        //$block = mcrypt_get_block_size($this->cipher, $this->mode); 
        $pad = ord($content[($len = strlen($content)) - 1]); 
        return substr($content, 0, strlen($content) - $pad); 
    }
    
    public function getSecretKey()
    {
        return self::$defaultKey;
    }
}

 

posted @ 2018-12-29 10:29  加菲猫and大白  阅读(846)  评论(0编辑  收藏  举报