nodejs和java中的des/3des加密解密

Java和nodejs中des加解密数据互操作,直接上代码(仅供参考):
JS代码如下:

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'  
})  

Java代码

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);  
    }

使用NoPadding填充, 注意待加密的数据长度必须为8字节整数倍。
上面nodejs中使用的是createDecipheriv, API中还有一个crypto.createCipher(algorithm, password)从password生成key和iv。

原文链接:,转发请注明来源!

发表评论

要发表评论,您必须先登录