[b]Java和nodejs中des加解密数据互操作,直接上代码(仅供参考):[/b]
[b]结果:[/b]
[b]nodejs 输出结果[/b]
des-ecb cb22e0c49a73e0e0cb22e0c49a73e0e008bb5db6b37c06d7
des-cbc 388d44f8b0f709c0915e14abc8eb782604ae07d96110ab0d
des-ede3 0a5f769c1a6eb5710a5f769c1a6eb5713bba29a037c699da
des-ede3-cbc 99988858eabe3e95ace8349b9e19dda66abb82b44b5f8f62
[b]java输出结果[/b]
encrypt_des_ecb: cb22e0c49a73e0e0cb22e0c49a73e0e008bb5db6b37c06d7
encrypt_des_cbc: 388d44f8b0f709c0915e14abc8eb782604ae07d96110ab0d
encrypt_3des_ecb: 0a5f769c1a6eb5710a5f769c1a6eb5713bba29a037c699da
encrypt_3des_cbc: 99988858eabe3e95ace8349b9e19dda66abb82b44b5f8f62
(转):[url]http://mygo.iteye.com/blog/2018882[/url]
var assert = require('assert');
var crypto = require('crypto');
function test_des(param) {
var key = new Buffer(param.key);
var iv = new Buffer(param.iv ? param.iv : 0)
var plaintext = param.plaintext
var alg = param.alg
var autoPad = param.autoPad
//encrypt
var cipher = crypto.createCipheriv(alg, key, iv);
cipher.setAutoPadding(autoPad) //default true
var ciph = cipher.update(plaintext, 'utf8', 'hex');
ciph += cipher.final('hex');
console.log(alg, ciph)
//decrypt
var decipher = crypto.createDecipheriv(alg, key, iv);
cipher.setAutoPadding(autoPad)
var txt = decipher.update(ciph, 'hex', 'utf8');
txt += decipher.final('utf8');
assert.equal(txt, plaintext, 'fail');
}
test_des({
alg: 'des-ecb',
autoPad: true,
key: '01234567',
plaintext: '1234567812345678',
iv: null
})
test_des({
alg: 'des-cbc',
autoPad: true,
key: '01234567',
plaintext: '1234567812345678',
iv: '12345678'
})
test_des({
alg: 'des-ede3', //3des-ecb
autoPad: true,
key: '0123456789abcd0123456789',
plaintext: '1234567812345678',
iv: null
})
test_des({
alg: 'des-ede3-cbc', //3des-cbc
autoPad: true,
key: '0123456789abcd0123456789',
plaintext: '1234567812345678',
iv: '12345678'
})
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import org.junit.Test;
@Test
public void encrypt_3des_ecb() throws Exception {
byte[] key = "0123456789abcd0123456789".getBytes();
byte[] plainText = "1234567812345678".getBytes();
// KeySpec myKeySpec = new DESedeKeySpec(key);
// SecretKeyFactory mySecretKeyFactory = SecretKeyFactory.getInstance("DESede");
// SecretKey secretKey = mySecretKeyFactory.generateSecret(myKeySpec);
SecretKey secretKey = new SecretKeySpec(key, "DESede");
//encrypt
Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(plainText);
System.out.println("encrypt_3des_ecb: " + Hex.encodeHexString(encryptedData));
//decrypt
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptPlainText = cipher.doFinal(encryptedData);
org.junit.Assert.assertArrayEquals(decryptPlainText, plainText);
}
@Test
public void encrypt_3des_cbc() throws Exception {
byte[] key = "0123456789abcd0123456789".getBytes();
byte[] plainText = "1234567812345678".getBytes();
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
SecretKey secretKey = new SecretKeySpec(key, "DESede");
//encrypt
Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptedData = cipher.doFinal(plainText);
System.out.println("encrypt_3des_cbc: " + Hex.encodeHexString(encryptedData));
//decrypt
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decryptPlainText = cipher.doFinal(encryptedData);
org.junit.Assert.assertArrayEquals(decryptPlainText, plainText);
}
@Test
public void encrypt_des_ecb() throws Exception {
byte[] key = "01234567".getBytes();
byte[] plainText = "1234567812345678".getBytes();
SecretKey secretKey = new SecretKeySpec(key, "DES");
//encrypt
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedData = cipher.doFinal(plainText);
System.out.println("encrypt_des_ecb: " + Hex.encodeHexString(encryptedData));
//decrypt
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptPlainText = cipher.doFinal(encryptedData);
org.junit.Assert.assertArrayEquals(decryptPlainText, plainText);
}
@Test
public void encrypt_des_cbc() throws Exception {
byte[] key = "01234567".getBytes();
byte[] plainText = "1234567812345678".getBytes();
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
SecretKey secretKey = new SecretKeySpec(key, "DES");
//encrypt
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
byte[] encryptedData = cipher.doFinal(plainText);
System.out.println("encrypt_des_cbc: " + Hex.encodeHexString(encryptedData));
//decrypt
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] decryptPlainText = cipher.doFinal(encryptedData);
org.junit.Assert.assertArrayEquals(decryptPlainText, plainText);
}
[b]结果:[/b]
[b]nodejs 输出结果[/b]
des-ecb cb22e0c49a73e0e0cb22e0c49a73e0e008bb5db6b37c06d7
des-cbc 388d44f8b0f709c0915e14abc8eb782604ae07d96110ab0d
des-ede3 0a5f769c1a6eb5710a5f769c1a6eb5713bba29a037c699da
des-ede3-cbc 99988858eabe3e95ace8349b9e19dda66abb82b44b5f8f62
[b]java输出结果[/b]
encrypt_des_ecb: cb22e0c49a73e0e0cb22e0c49a73e0e008bb5db6b37c06d7
encrypt_des_cbc: 388d44f8b0f709c0915e14abc8eb782604ae07d96110ab0d
encrypt_3des_ecb: 0a5f769c1a6eb5710a5f769c1a6eb5713bba29a037c699da
encrypt_3des_cbc: 99988858eabe3e95ace8349b9e19dda66abb82b44b5f8f62
(转):[url]http://mygo.iteye.com/blog/2018882[/url]