日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

JAVA使用openssl生成的公私鑰做加密解密 | PHPor 的Blog

 WindySky 2016-07-04

使用openssl生成密鑰對:

1
2
3
4
5
6
>openssl genrsa -out private.key
>openssl rsa -in private.key -pubout -outform PEM -out public.key
// 因?yàn)閖ava里面不識別x509格式的私鑰,所以必須轉(zhuǎn)換為 pkcs8格式方可使用
// java異常描述為: java.security.spec.InvalidKeySpecException: Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys
// 雖然RSAPrivate(Crt)KeySpec 也支持,但是目前還不知道怎么用
>openssl pkcs8 -topk8 -inform PEM -outform PEM -in private.key -out pkcs8_priv.pem -nocrypt

 

java代碼:

1. 注意: java 語言本身沒有實(shí)現(xiàn)base64編碼,而openssl生成的密鑰對一般做base64編碼,便于維護(hù),所以這里引用了 org.apache.commons.codec.binary.Base64;

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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Arrays;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
//下載地址: http://commons./codec/download_codec.cgi
public static class MyRsa {
/**
* String to hold name of the encryption algorithm.
*/
public static final String ALGORITHM = "RSA";
/**
* String to hold the name of the private key file.
*/
public static final String PRIVATE_KEY_FILE = "D:/rsa/pkcs8_priv.pem";
/**
* String to hold name of the public key file.
*/
public static final String PUBLIC_KEY_FILE = "D:/rsa/public.key";
/**
* Encrypt the plain text using public key.
*
* @param text
*            : original plain text
* @param key
*            :The public key
* @return Encrypted text
* @throws java.lang.Exception
*/
public static byte[] encrypt(String text, PublicKey key) {
byte[] cipherText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// encrypt the plain text using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
return cipherText;
}
/**
* Decrypt text using private key.
*
* @param text
*            :encrypted text
* @param key
*            :The private key
* @return plain text
* @throws java.lang.Exception
*/
public static String decrypt(byte[] text, PrivateKey key) {
byte[] dectyptedText = null;
try {
// get an RSA cipher object and print the provider
final Cipher cipher = Cipher.getInstance(ALGORITHM);
// decrypt the text using the private key
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);
} catch (Exception ex) {
ex.printStackTrace();
}
return new String(dectyptedText);
}
public static void test() {
String s = "Hello world";
try {
BufferedReader privateKey = new BufferedReader(new FileReader(
PRIVATE_KEY_FILE));
BufferedReader publicKey = new BufferedReader(new FileReader(
PUBLIC_KEY_FILE));
String strPrivateKey = "";
String strPublicKey = "";
String line = "";
while((line = privateKey.readLine()) != null){
strPrivateKey += line;
}
while((line = publicKey.readLine()) != null){
strPublicKey += line;
}
privateKey.close();
publicKey.close();
// 私鑰需要使用pkcs8格式的,公鑰使用x509格式的
String strPrivKey = strPrivateKey.replace("-----BEGIN PRIVATE KEY-----", "")
.replace("-----END PRIVATE KEY-----", "");
String strPubKey = strPublicKey.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "");
//System.out.print(strPrivKey);
//System.out.println(strPubKey);
byte [] privKeyByte = Base64.decodeBase64(strPrivKey);
byte [] pubKeyByte = Base64.decodeBase64(strPubKey);
PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(privKeyByte);
//PKCS8EncodedKeySpec pubKeySpec = new PKCS8EncodedKeySpec(pubKeyByte);
//X509EncodedKeySpec privKeySpec = new X509EncodedKeySpec(privKeyByte);
X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubKeyByte);
KeyFactory kf = KeyFactory.getInstance("RSA");
PrivateKey privKey = kf.generatePrivate(privKeySpec);
PublicKey pubKey = kf.generatePublic(pubKeySpec);
byte [] encryptByte = encrypt(s, pubKey);
System.out.println(decrypt(encryptByte, privKey));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

 

稍后提供一個PHP加密Java解密的實(shí)現(xiàn)

 

參考資料:

http:///questions/11787571/how-to-read-pem-file-to-get-private-and-public-key

http:///questions/8647165/how-to-sign-a-generic-text-with-rsa-key-and-encode-with-base64-in-java

http://www./tutorials/cryptography/rsa_encryption.shtml

http://snowolf./blog/381767

密鑰結(jié)構(gòu)及格式: https:///kb/cryptography/asn1-key-structures-in-der-and-pem

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多