1. 程式人生 > >node+java AES 相同加密解密

node+java AES 相同加密解密

node

var crypto = require('crypto')

// 加密
function encryption (data, key) {
  var iv = ''
  var clearEncoding = 'utf8'
  var cipherEncoding = 'base64'
  var cipherChunks = []
  var cipher = crypto.createCipheriv('aes-128-ecb', key, iv)
  cipher.setAutoPadding(true)

  cipherChunks.push(cipher.update(data, clearEncoding, cipherEncoding))
  cipherChunks.push(cipher.final(cipherEncoding))

  return cipherChunks.join('')
}
// 解密
function decryption (data, key) {
  var iv = ''
  var clearEncoding = 'utf8'
  var cipherEncoding = 'base64'
  var cipherChunks = []
  var decipher = crypto.createCipheriv('aes-128-ecb', key, iv)
  decipher.setAutoPadding(true)

  cipherChunks.push(decipher.update(data, cipherEncoding, clearEncoding))
  cipherChunks.push(decipher.final(clearEncoding))

  return cipherChunks.join('')
}
console.log(encryption('admin', '13223wrwe4345678'))
console.log(encryption('admin', '13223wrwe4345678'))

java

package com.create.color.common.util;



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


import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;

/**
 * 編碼工具類 實現aes加密、解密
 */
@SuppressWarnings("restriction")
public class AESUtils {

    /**
     * 金鑰長度必須是16
     */
    private static final String defaultKey = "13223wrwe4345678";

    private static final String testContent = "admin";

    /**
     * 演算法
     */
    private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding";

    public static void main(String[] args) throws Exception {
        System.out.println("加密前:" + testContent);

        System.out.println("加密金鑰和解密金鑰:" + defaultKey);

        String encrypt = encrypt(testContent, defaultKey);
        System.out.println("加密後:" + encrypt);

        String decrypt = decrypt("SXza4p72q8T/aoHXS5Yr0Q==", defaultKey);
        System.out.println("解密後:" + decrypt);
    }

    /**
     * aes解密
     *
     * @param encrypt
     *            內容
     * @return
     * @throws Exception
     */
    public static String decryptByDefaultKey(String encrypt) throws Exception {
        return decrypt(encrypt, defaultKey);
    }

    /**
     * aes加密
     *
     * @param content
     * @return
     * @throws Exception
     */
    public static String encryptByDefaultKey(String content) throws Exception {
        return encrypt(content, defaultKey);
    }

    /**
     * base 64 encode
     *
     * @param bytes
     *            待編碼的byte[]
     * @return 編碼後的base 64 code
     */
    private static String base64Encode(byte[] bytes) {
        return Base64.encodeBase64String(bytes);
    }

    /**
     * base 64 decode
     *
     * @param base64Code
     *            待解碼的base 64 code
     * @return 解碼後的byte[]
     * @throws Exception
     */
    private static byte[] base64Decode(String base64Code) throws Exception {
        return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
    }

    /**
     * AES加密
     *
     * @param content
     *            待加密的內容
     * @param encryptKey
     *            加密金鑰
     * @return 加密後的byte[]
     * @throws Exception
     */
    private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), "AES"));

        return cipher.doFinal(content.getBytes("utf-8"));
    }

    /**
     * AES加密為base 64 code
     *
     * @param content
     *            待加密的內容
     * @param encryptKey
     *            加密金鑰
     * @return 加密後的base 64 code
     * @throws Exception
     */
    public static String encrypt(String content, String encryptKey) throws Exception {
        return base64Encode(aesEncryptToBytes(content, encryptKey));
    }

    /**
     * AES解密
     *
     * @param encryptBytes
     *            待解密的byte[]
     * @param decryptKey
     *            解密金鑰
     * @return 解密後的String
     * @throws Exception
     */
    private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);

        Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES"));
        byte[] decryptBytes = cipher.doFinal(encryptBytes);

        return new String(decryptBytes);
    }

    /**
     * 將base 64 code AES解密
     *
     * @param encryptStr
     *            待解密的base 64 code
     * @param decryptKey
     *            解密金鑰
     * @return 解密後的string
     * @throws Exception
     */
    public static String decrypt(String encryptStr, String decryptKey) throws Exception {
        return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey);
    }

}