欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java与Node.js利用AES加密解密出相同结果的方法示例

程序员文章站 2024-03-06 17:08:44
前言 工作中遇到nodejs端通过aes加密,安卓客户端java解密,同样nodejs也需要解密安卓客户端加密过来的内容,发现两个加密结果不一样,查询资料发现java端需...

前言

工作中遇到nodejs端通过aes加密,安卓客户端java解密,同样nodejs也需要解密安卓客户端加密过来的内容,发现两个加密结果不一样,查询资料发现java端需要对密钥再md5加密一遍,以下是java与node.js利用aes加密解密出相同结果的方法,需要的朋友们下面来一起学习学习吧。

java代码如下:

package g.g;

import java.security.messagedigest;

import javax.crypto.cipher;
import javax.crypto.spec.secretkeyspec;

public class aesecb {
 public static final string default_coding = "utf-8"; 
  
 /** 
  * 解密 
  * @author lmiky 
  * @date 2014-2-25 
  * @param encrypted 
  * @param seed 
  * @return 
  * @throws exception 
  */ 
 private static string decrypt(string encrypted, string seed) throws exception { 
  byte[] keyb = seed.getbytes(default_coding); 
  messagedigest md = messagedigest.getinstance("md5"); 
  byte[] thedigest = md.digest(keyb); 
  secretkeyspec skey = new secretkeyspec(thedigest, "aes"); 
  cipher dcipher = cipher.getinstance("aes"); 
  dcipher.init(cipher.decrypt_mode, skey); 
 
  byte[] clearbyte = dcipher.dofinal(tobyte(encrypted)); 
  return new string(clearbyte); 
 } 
 
 /** 
  * 加密 
  * @author lmiky 
  * @date 2014-2-25 
  * @param content 
  * @param key 
  * @return 
  * @throws exception 
  */ 
 public static string encrypt(string content, string key) throws exception { 
  byte[] input = content.getbytes(default_coding); 
   
  messagedigest md = messagedigest.getinstance("md5"); 
  byte[] thedigest = md.digest(key.getbytes(default_coding)); 
  secretkeyspec skc = new secretkeyspec(thedigest, "aes"); 
  cipher cipher = cipher.getinstance("aes/ecb/pkcs5padding"); 
  cipher.init(cipher.encrypt_mode, skc); 
   
  byte[] ciphertext = new byte[cipher.getoutputsize(input.length)]; 
  int ctlength = cipher.update(input, 0, input.length, ciphertext, 0); 
  ctlength += cipher.dofinal(ciphertext, ctlength); 
    
  return parsebyte2hexstr(ciphertext); 
 } 
  
 /** 
  * 字符串转字节数组 
  * @author lmiky 
  * @date 2014-2-25 
  * @param hexstring 
  * @return 
  */ 
 private static byte[] tobyte(string hexstring) { 
  int len = hexstring.length() / 2; 
  byte[] result = new byte[len]; 
  for (int i = 0; i < len; i++) { 
   result[i] = integer.valueof(hexstring.substring(2 * i, 2 * i + 2), 16).bytevalue(); 
  } 
  return result; 
 } 
  
 /** 
  * 字节转16进制数组 
  * @author lmiky 
  * @date 2014-2-25 
  * @param buf 
  * @return 
  */ 
 private static string parsebyte2hexstr(byte buf[]) { 
  stringbuffer sb = new stringbuffer(); 
  for (int i = 0; i < buf.length; i++) { 
   string hex = integer.tohexstring(buf[i] & 0xff); 
   if (hex.length() == 1) { 
    hex = '0' + hex; 
   } 
   sb.append(hex); 
  } 
  return sb.tostring(); 
 } 
  
 public static void main(string[] args) throws exception { 
  system.out.println(aesecb.encrypt("fsadfsdafsdafsdafsadfsadfsadf", "1evriqy7b9uv7zmm")); 
  system.out.println(aesecb.decrypt("b123e2d9199598c0e3f1999dc9e723387b68e29d2b3a0d59fc7d5946c750c6b4", "1evriqy7b9uv7zmm")); 
 }
}

node.js代码如下:

var crypto = require('crypto');
exports.aes_algorithm = "aes-128-ecb";
exports.aes_secrect = "1evriqy7b9uv7zmm";

exports.encrypt = function (text) {
 var cipher = crypto.createcipher(this.aes_algorithm, this.aes_secrect)
 var crypted = cipher.update(text, 'utf8', 'hex')
 crypted += cipher.final('hex');
 return crypted;
};

exports.decrypt = function (text) {
 var decipher = crypto.createdecipher(this.aes_algorithm, this.aes_secrect)
 var dec = decipher.update(text, 'hex', 'utf8')
 dec += decipher.final('utf8');
 return dec;
};

//var hw = this.encrypt("fsadfsdafsdafsdafsadfsadfsadf");
//console.log(hw);
//console.log(this.decrypt(hw));

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对的支持。