前言
工作中遇到nodejs端通过aes加密,安卓客户端Java解密,同样nodejs也需要解密安卓客户端加密过来的内容,发现两个加密结果不一样,查询资料发现java端需要对密钥再MD5加密一遍,以下是Java与Node.js利用AES加密解密出相同结果的方法,需要的朋友们下面来一起学习学习吧。
JAVA代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
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代码如下:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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));
|
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对快网idc的支持。
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-25 24
-
2025-05-29 13
-
2025-05-29 87
-
2025-05-29 15
-
2025-05-27 24
热门评论