java-加解密

0x01 RSA加解密和签名的应用

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package test;

import java.security.*;
//import java.security.KeyFactory;
//import java.security.KeyPair;
//import java.security.KeyPairGenerator;
//import java.security.NoSuchAlgorithmException;
//import java.security.PrivateKey;
//import java.security.PublicKey;
//import java.security.Signature;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.Cipher;
/**
* 非对称加密RSA
* @author 0x00
*/
public class RSAUtils {

/**
* 公钥加密
* @param content
* @param publicKey
* @return
* @throws Exception
*/
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
//加密的数据必须为字节
return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes()));
}

/**
* 私钥解密
* @param content
* @param privateKey
* @return
* @throws Exception
*/
public static String decrypt(String data, PrivateKey privateKey) throws Exception {
byte[] bytes = Base64.getDecoder().decode(data);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(bytes));
}

/**
* 签名
* @param data
* @param privateKey
* @return
* @throws Exception
* @throws Exception
*/
public static String sign(String content, byte[] privateKeyBytes) throws Exception
{
//单例获取key工厂类,将拿到的privateKeyBytes创建PKCS8EncodedKeySpec对象,通过其获取PrivateKey对象
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
// 用key工厂对象生成私钥
PrivateKey priKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes));

//获取Signature对象,签名算法为SHA1WithRSA,此处还有较多可选择比如MD2withRSA/MD5withRSA/SHA1withRSA/SHA256withRSA等
Signature signature = Signature.getInstance("SHA1WithRSA");
//初始化签名
signature.initSign(priKey);
//签名的数据必须为字节
signature.update(content.getBytes());
byte[] encodeResult = signature.sign();
return Base64.getEncoder().encodeToString(encodeResult);
}

/**
* 验证
* @param data
* @param sign
* @param publicKey
* @return
* @throws Exception
*/
public static boolean verify(String content, String sign, byte[] publicKeyBytes) throws Exception {
byte[] bytes = Base64.getDecoder().decode(sign);
//单例获取key工厂类,将拿到的publicKey创建X509EncodedKeySpec对象,通过其获取PublicKey对象
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes));

//获取Signature对象,签名算法为SHA1WithRSA,此处还有较多可选择比如MD2withRSA/MD5withRSA/SHA1withRSA/SHA256withRSA等
Signature signature = Signature.getInstance("SHA1WithRSA");
signature.initVerify(pubKey);
signature.update( content.getBytes() );
boolean bverify = signature.verify( bytes );
return bverify;
}

/**
* 生成密钥对
* @return
* @throws Exception
*/
public static KeyPair genKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
return keyPairGenerator.generateKeyPair();
}


public static void main(String[] args) throws Exception {

String data = "this is a securet message dfgsgdfgfgdfg";

KeyPair keyPair = genKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
byte[] publicKeyBytes = publicKey.getEncoded();
byte[] privateKeyBytes = privateKey.getEncoded();

// 获取公,私钥,并以base64格式打印出来
// System.out.println("公钥:" + Base64.getEncoder().encodeToString( publicKeyBytes ) );
// System.out.println("私钥:" + Base64.getEncoder().encodeToString( privateKeyBytes ) );

// 公钥加密
String encryptedString = encrypt(data, publicKey);
System.out.println("加密后:" + encryptedString);
System.out.println("加密后的数据长度: " + encryptedString.length() );

// 私钥解密
String decryptedString = decrypt(encryptedString, privateKey);
System.out.println("解密后:" + decryptedString);

// 私钥签名
String sign = sign(data, privateKeyBytes);
System.out.println("签名:" + sign);
System.out.println("签名后的数据长度:" + sign.length() );

// 公钥验签
boolean flag = verify(data, sign, publicKeyBytes);
System.out.println("验证:" + flag);

}


}

0x02 AES加解密

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
package test;

import java.security.Key;
import java.security.SecureRandom;
import java.util.Base64;

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


public class DemoAESUtil {
/**
* 加密
* @param str
* @param key
* @return
* @throws Exception
*/
public static String encrypt(String str, String key) throws Exception {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, generateKey(key));
byte[] bytes = cipher.doFinal(str.getBytes());

return Base64.getEncoder().encodeToString(bytes);
}

/**
* 解密
* @param str
* @param key
* @return
* @throws Exception
*/
public static String decrypt(String str, String key) throws Exception {
byte[] bytes = Base64.getDecoder().decode(str);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, generateKey(key));
return new String(cipher.doFinal(bytes));
}

/**
* 生成key
* @param key
* @return
* @throws Exception
*/
private static Key generateKey(String key) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(128, new SecureRandom(key.getBytes())); // 密钥长度 128/192/256
return new SecretKeySpec(keyGenerator.generateKey().getEncoded(), "AES");
}

public static void main(String[] args) throws Exception {

String str = "hello world";
String key = "password";

// 加密
String encryptedString = encrypt(str, key);
System.out.println("加密后:" + encryptedString);

// 解密
String decryptedString = decrypt(encryptedString, key);
System.out.println("解密后:" + new String(decryptedString));

}

}

参考

https://github.com/ghyg525/util_java_safe
https://tools.ietf.org/html/rfc5246 –ssl

坚持原创技术分享,您的支持将鼓励我继续创作!