鍍金池/ 問(wèn)答/HTML5  PHP/ php 3des/cbc/PKCS5Padding 加解密

php 3des/cbc/PKCS5Padding 加解密

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;

class Untitled {



public static void main(String[] args) throws Exception {
    
    //key 生成1 方法1

// KeyGenerator keyGen = KeyGenerator.getInstance("DESede");//密鑰生成器
// keyGen.init(168); //可指定密鑰長(zhǎng)度為112或168,默認(rèn)為168
// SecretKey secretKey = keyGen.generateKey();//生成密鑰
// byte[] key = secretKey.getEncoded();//密鑰字節(jié)數(shù)組 }
// SecretKey secretKey1 = new SecretKeySpec(key, "DESede");//恢復(fù)密鑰

    
    //key生成2
    String keyString = "8daluqp9xm2kw6zs1htawqee";

    SecretKey secretKey2 = new SecretKeySpec(keyString.getBytes(), "DESede");
            
    //加密
    String data = "hello";
    System.out.println(data.getBytes());
    Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");//Cipher完成加密或解密工作類(lèi)
    cipher.init(Cipher.ENCRYPT_MODE, secretKey2);//對(duì)Cipher初始化,解密模式
    byte[] cipherByte = cipher.doFinal(data.getBytes());//加密data
    System.out.println(cipherByte);
    IvParameterSpec iv = new IvParameterSpec(cipher.getIV());
    //解密
    cipher.init(Cipher.DECRYPT_MODE,secretKey2,iv);
    byte[] cipherByte2 = cipher.doFinal(cipherByte);
    System.out.println(new String(cipherByte2));

}
}

====================

php 版本的 3des/cbc/PKCS5Padding 該如何實(shí)現(xiàn)?

回答
編輯回答
淺淺

phpseclib 這個(gè)類(lèi)庫(kù)里面好像帶這個(gè)支持

2017年3月6日 13:19