3DES加密
程序员文章站
2022-06-15 17:55:11
...
DES加密算法是一种标准算法,应该可以通过不同程序语言实现,我在异构系统里,可能需要用不同的语言加密解密,整理不同语言的DES算法还是有必要的,先前我发过一个Java的3DES算法代码,我再发一遍
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class DES {
public static int _DES = 1;
public static int _DESede = 2;
public static int _Blowfish = 3;
private Cipher p_Cipher;
private SecretKey p_Key;
private String p_Algorithm;
private void selectAlgorithm(int al) {
switch (al) {
default:
case 1:
this.p_Algorithm = “DES”;
break;
case 2:
this.p_Algorithm = “DESede”;
break;
case 3:
this.p_Algorithm = “Blowfish”;
break;
}
}
public DES(int algorithm) throws Exception {
this.selectAlgorithm(algorithm);
Security.addProvider(new com.sun.crypto.provider.SunJCE());
this.p_Cipher = Cipher.getInstance(this.p_Algorithm);
}
public byte[] getKey() {
return this.checkKey().getEncoded();
}
private SecretKey checkKey() {
try {
if (this.p_Key == null) {
KeyGenerator keygen = KeyGenerator.getInstance(this.p_Algorithm);
/*
SecureRandom sr = new SecureRandom(key.getBytes());
keygen.init(168, sr);*/
this.p_Key = keygen.generateKey();
}
}
catch (Exception nsae) {}
return this.p_Key;
}
public void setKey(byte[] enckey) {
this.p_Key = new SecretKeySpec(enckey, this.p_Algorithm);
}
public byte[] encode(byte[] data) throws Exception {
this.p_Cipher.init(Cipher.ENCRYPT_MODE, this.checkKey());
return this.p_Cipher.doFinal(data);
}
public byte[] decode(byte[] encdata, byte[] enckey) throws Exception {
this.setKey(enckey);
this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key);
return this.p_Cipher.doFinal(encdata);
}
public String byte2hex(byte[] b) {
String hs = “”;
String stmp = “”;
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0xFF);
if (stmp.length() == 1) {
hs += “0″ + stmp;
}
else {
hs += stmp;
}
}
return hs.toUpperCase();
}
public byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = “” + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
public static void main(String[] args) throws Exception {
String info = “要加密的字串”;
System.out.println(“region string:” + info);
byte[] key; //密钥文件(byte)
DES des = new DES(DES._DESede); // 声明DES
key = des.getKey(); //获取随机生成的密钥
System.out.println(“encrypted key(byte):” + new String(key));
String hexkey = des.byte2hex(key); //生成十六进制密钥
System.out.println(“encrypted key(hex):” + hexkey);
byte[] enc = des.encode(info.getBytes()); //生成加密文件(byte)
System.out.println(“encrypted string(byte):” + new String(enc));
String hexenc = des.byte2hex(enc); //生成十六进制加密文件
System.out.println(“encrypted string(hex):” + hexenc);
byte[] dec = des.decode(enc, des.hex2byte(hexkey)); //解密文件,其中转换十六进制密钥为byte
System.out.println(“decrypted string:” + new String(dec)); //生成解密文件字符串, 与info相同
}
}
最近我在看PHP的时候,研究了一下PHP对于3DES的加密代码
<?php
function fmt3DESEncode($s,$base64key){
$key = base64_decode($base64key);
$td = mcrypt_module_open(MCRYPT_3DES, ”, MCRYPT_MODE_ECB, ”);
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
mcrypt_generic_init($td, $key,$iv);
$encrypted_data = mcrypt_generic($td, $s);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encrypted_data;
}
function PaddingPKCS7($data) {
$block_size = mcrypt_get_block_size(‘tripledes’, ‘ecb’);
$padding_char = $block_size – (strlen($data) % $block_size);
$data .= str_repeat(chr($padding_char),$padding_char);
return $data;
}
function fmt3DESDecode($s,$base64key) {
$key = base64_decode($base64key);
$td = mcrypt_module_open(MCRYPT_3DES, ”, MCRYPT_MODE_ECB, ”);
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
mcrypt_generic_init($td, $key,$iv);
$p_t = mdecrypt_generic($td, $s);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $p_t;
}
function unPaddingPKCS7($data) {
$pad = ord($data{strlen($data)-1});
if ($pad > strlen($data)) {
return false;
}
if (strspn($data, chr($pad), strlen($data) – $pad) != $pad) {
return false;
}
$srcdata = substr($data, 0, -1 * $pad);
return $srcdata;
}
$S = “laoer”;
$str = BASE64_Encode(fmt3DESEncode(PaddingPKCS7($S),”XXXXXXXXX-BASE64-密钥”));
echo $str;
echo”<br/>”;
$str = fmt3DESDecode(base64_decode($str), “XXXXXXXXX-BASE64-密钥”);
echo unPaddingPKCS7($str);
?>
有几点注意,在Java里3DES默认ENCRYPT_MODE是ECB,并采用PKCS7补码,关于着两个概念,我不是专业人士,也解释不清楚,需要研究加密解密了,所以在PHP里也进行了相应的处理,还有就是Java代码里密钥和加密结果是用16进制编码了,PHP代码里用了Base64,大家用的时候相应改一下就可以了。
现在可以测试一下,JAVA和PHP的加密解密结果是否一致!
补:.NET下3DES的算法也没有问题,我不做这方面,但以前同事做过,都测试通过
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class DES {
public static int _DES = 1;
public static int _DESede = 2;
public static int _Blowfish = 3;
private Cipher p_Cipher;
private SecretKey p_Key;
private String p_Algorithm;
private void selectAlgorithm(int al) {
switch (al) {
default:
case 1:
this.p_Algorithm = “DES”;
break;
case 2:
this.p_Algorithm = “DESede”;
break;
case 3:
this.p_Algorithm = “Blowfish”;
break;
}
}
public DES(int algorithm) throws Exception {
this.selectAlgorithm(algorithm);
Security.addProvider(new com.sun.crypto.provider.SunJCE());
this.p_Cipher = Cipher.getInstance(this.p_Algorithm);
}
public byte[] getKey() {
return this.checkKey().getEncoded();
}
private SecretKey checkKey() {
try {
if (this.p_Key == null) {
KeyGenerator keygen = KeyGenerator.getInstance(this.p_Algorithm);
/*
SecureRandom sr = new SecureRandom(key.getBytes());
keygen.init(168, sr);*/
this.p_Key = keygen.generateKey();
}
}
catch (Exception nsae) {}
return this.p_Key;
}
public void setKey(byte[] enckey) {
this.p_Key = new SecretKeySpec(enckey, this.p_Algorithm);
}
public byte[] encode(byte[] data) throws Exception {
this.p_Cipher.init(Cipher.ENCRYPT_MODE, this.checkKey());
return this.p_Cipher.doFinal(data);
}
public byte[] decode(byte[] encdata, byte[] enckey) throws Exception {
this.setKey(enckey);
this.p_Cipher.init(Cipher.DECRYPT_MODE, this.p_Key);
return this.p_Cipher.doFinal(encdata);
}
public String byte2hex(byte[] b) {
String hs = “”;
String stmp = “”;
for (int i = 0; i < b.length; i++) {
stmp = Integer.toHexString(b[i] & 0xFF);
if (stmp.length() == 1) {
hs += “0″ + stmp;
}
else {
hs += stmp;
}
}
return hs.toUpperCase();
}
public byte[] hex2byte(String hex) throws IllegalArgumentException {
if (hex.length() % 2 != 0) {
throw new IllegalArgumentException();
}
char[] arr = hex.toCharArray();
byte[] b = new byte[hex.length() / 2];
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) {
String swap = “” + arr[i++] + arr[i];
int byteint = Integer.parseInt(swap, 16) & 0xFF;
b[j] = new Integer(byteint).byteValue();
}
return b;
}
public static void main(String[] args) throws Exception {
String info = “要加密的字串”;
System.out.println(“region string:” + info);
byte[] key; //密钥文件(byte)
DES des = new DES(DES._DESede); // 声明DES
key = des.getKey(); //获取随机生成的密钥
System.out.println(“encrypted key(byte):” + new String(key));
String hexkey = des.byte2hex(key); //生成十六进制密钥
System.out.println(“encrypted key(hex):” + hexkey);
byte[] enc = des.encode(info.getBytes()); //生成加密文件(byte)
System.out.println(“encrypted string(byte):” + new String(enc));
String hexenc = des.byte2hex(enc); //生成十六进制加密文件
System.out.println(“encrypted string(hex):” + hexenc);
byte[] dec = des.decode(enc, des.hex2byte(hexkey)); //解密文件,其中转换十六进制密钥为byte
System.out.println(“decrypted string:” + new String(dec)); //生成解密文件字符串, 与info相同
}
}
最近我在看PHP的时候,研究了一下PHP对于3DES的加密代码
<?php
function fmt3DESEncode($s,$base64key){
$key = base64_decode($base64key);
$td = mcrypt_module_open(MCRYPT_3DES, ”, MCRYPT_MODE_ECB, ”);
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
mcrypt_generic_init($td, $key,$iv);
$encrypted_data = mcrypt_generic($td, $s);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encrypted_data;
}
function PaddingPKCS7($data) {
$block_size = mcrypt_get_block_size(‘tripledes’, ‘ecb’);
$padding_char = $block_size – (strlen($data) % $block_size);
$data .= str_repeat(chr($padding_char),$padding_char);
return $data;
}
function fmt3DESDecode($s,$base64key) {
$key = base64_decode($base64key);
$td = mcrypt_module_open(MCRYPT_3DES, ”, MCRYPT_MODE_ECB, ”);
$iv_size = mcrypt_enc_get_iv_size($td);
$iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
mcrypt_generic_init($td, $key,$iv);
$p_t = mdecrypt_generic($td, $s);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $p_t;
}
function unPaddingPKCS7($data) {
$pad = ord($data{strlen($data)-1});
if ($pad > strlen($data)) {
return false;
}
if (strspn($data, chr($pad), strlen($data) – $pad) != $pad) {
return false;
}
$srcdata = substr($data, 0, -1 * $pad);
return $srcdata;
}
$S = “laoer”;
$str = BASE64_Encode(fmt3DESEncode(PaddingPKCS7($S),”XXXXXXXXX-BASE64-密钥”));
echo $str;
echo”<br/>”;
$str = fmt3DESDecode(base64_decode($str), “XXXXXXXXX-BASE64-密钥”);
echo unPaddingPKCS7($str);
?>
有几点注意,在Java里3DES默认ENCRYPT_MODE是ECB,并采用PKCS7补码,关于着两个概念,我不是专业人士,也解释不清楚,需要研究加密解密了,所以在PHP里也进行了相应的处理,还有就是Java代码里密钥和加密结果是用16进制编码了,PHP代码里用了Base64,大家用的时候相应改一下就可以了。
现在可以测试一下,JAVA和PHP的加密解密结果是否一致!
补:.NET下3DES的算法也没有问题,我不做这方面,但以前同事做过,都测试通过