-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTestMain.java
More file actions
74 lines (56 loc) · 2.75 KB
/
MyTestMain.java
File metadata and controls
74 lines (56 loc) · 2.75 KB
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
import java.io.File;
import java.io.ObjectInputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
/**
* Created by bahb on 2017/7/12.
*/
public class MyTestMain {
/**
* String to hold the name of the private key file.
*/
public static final String PRIVATE_KEY_FILE = "../Certification/rsa_1024_priv.pem";
/**
* String to hold name of the public key file.
*/
public static final String PUBLIC_KEY_FILE = "../Certification/rsa_1024_pub.pem";
public static String encodeB64(byte[] data) {
return Base64.getEncoder().encodeToString(data);
}
public static byte[] decodeB64(String base64Str) {
return Base64.getDecoder().decode(base64Str);
}
public static void main(String[] args) {
try {
// // Check if the pair of keys are present else generate those.
// if (!EncryptionUtil.areKeysPresent()) {
// // Method generates a pair of keys using the RSA algorithm and stores it
// // in their respective files
// EncryptionUtil.generateKey();
// }
// 公钥
File publicKeyFile = new File(PUBLIC_KEY_FILE);
PublicKey publicKey = EncryptionUtil.loadPublicKeyFromPEMFile(publicKeyFile);
// 私钥
File privateKeyFile = new File(PRIVATE_KEY_FILE);
final PrivateKey privateKey = EncryptionUtil.loadPrivateKeyFromPkcs1OrPkcs8EncodedPEMFile(privateKeyFile);
byte[] cipherText = EncryptionUtil.encrypt("测试中文1", publicKey);
String plainText = EncryptionUtil.decrypt(cipherText, privateKey);
System.out.println("Encrypted: " + encodeB64(cipherText));
System.out.println("Decrypted: [" + plainText + "]");
cipherText = EncryptionUtil.encrypt("测试中文2", privateKey);
plainText = EncryptionUtil.decrypt(cipherText, publicKey);
System.out.println("Encrypted: " + encodeB64(cipherText));
System.out.println("Decrypted: [" + plainText + "]");
// 测试解密,输入其他语言加密的 Base64 字符串,然后进行解密
final String encryptedDataWithB64Encoding = "atrcDtLqoCRU5N1wM7NyIy7tTnSqLRnv2YK7pIMY4M4N/1BBMg/LS4nMD06ngZ1+hcHfv4MGllGZPV7GyNf5RMEcaPa1uT2/X4c1+wmsPV8XmB7iiHGNE39dZG0fWD3rx6G/RRVjWVPFeZ25klwSANo5a9USEey+O6zpBaw3ygc=";
System.out.println("Encrypted: " + encryptedDataWithB64Encoding);
cipherText = decodeB64(encryptedDataWithB64Encoding);
plainText = EncryptionUtil.decrypt(cipherText, privateKey);
System.out.println("Decrypted: [" + plainText + "]");
} catch (Exception e) {
e.printStackTrace();
}
}
}