国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
JAVA使用openssl生成的公私鑰做加密解密 | PHPor 的Blog

使用openssl生成密鑰對(duì):

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里面不識(shí)別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 語(yǔ)言本身沒(méi)有實(shí)現(xiàn)base64編碼,而openssl生成的密鑰對(duì)一般做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.apache.org/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();
}
}
}

 

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

 

參考資料:

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

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

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

http://snowolf.iteye.com/blog/381767

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

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
RSA加解密的java實(shí)現(xiàn)(及中途的各種異常情況分析)
公鑰,私鑰和數(shù)字簽名這樣最好理解
非對(duì)稱加密算法---加密學(xué)習(xí)筆記(四)
iOS中使用RSA對(duì)數(shù)據(jù)進(jìn)行加密解密
RSA加密解密及數(shù)字簽名Java實(shí)現(xiàn)
RSA加密解密(無(wú)數(shù)據(jù)大小限制,php、go、java互通實(shí)現(xiàn))
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服