2015-04-27 51 views
0

我想獲得像這樣的公鑰。 java可以做到嗎?例如,我輸入姓名,電子郵件和密碼。所以Java生成它的公鑰和私鑰。這個公鑰的例子如何使用Java生成電子郵件以獲取公鑰和私鑰

-----BEGIN PGP PUBLIC KEY BLOCK----- 
Comment: http://openpgpjs.org 
xsBNBFU9xmABCAC/9KabOlwoa+OdFAWkMt9blHeg9dggGCdwrAr66/E+5zqJ 
reintA7h7cVphdyOiRvMZjxHK80Osdb0F/3ywHoyPiZtOsBk3NgaCCvKz5ai 
8rQzbOg+ge0LAs0/rbH+BWh/6aCOwTAymDVo7QrJedNZsQTtJ1qFRK8qoRkS 
nMlG5qjZuRSOAK5sqEAReT3jWGvpcRh2A+IM8RxC02ijxA+57qN0ncahV2E+ 
0ROCe20mW9Wo2tUONV418UsuZzaGmcfThW6JJPMKeCAeQseV/J/BvcK7NSOi 
u6dwBWZqOP1/LTtbFOLoLaeeNSc3KhopuRDpYNCWZej099T/zugJVQ5fABEB 
AAHNGktveWFtYSA8aGltaXRzdUB5YWhvby5jb20+wsByBBABCAAmBQJVPcZh 
BgsJCAcDAgkQOSpK7V+SVG8EFQgCCgMWAgECGwMCHgEAAG8mCACqAyCglWDU 
I/zhq/5PJO7FRuhj3yU93Vju99z+E/0dQzUGgGAYtdzUxhAKk0ny9ZXC3FSa 
LboXY8GlgkcmEyzyDKAstLXv7//Se7a1dMcI9yluYLOCNo7z4h0o8MBAg3K2 
jU0iPtEqiGBk2+4Iy5Sh0uGoeE6uq0JauL5Uq6P2aZWepf1ZIQrVbFYHcez4 
W1JBnGFmb57ovUq1BvRYl5MJQcvqzXpLN8ZE1ZpMw8u3osMljV1Z4DaAd9bu 
y+S4gF2Toyz8cr9DUIfyG5i5lh6Pxj7XT3dMeOQa/ZygrtZZt5DP0TDIN3s8 
7xSvKFFMym9v9S9B9LsqaMnYig8tX3MBzsBNBFU9xmABCACvxd9JpfiyJZw1 
MFr9c/DcUXMkE+pWWWTSybR/5b60BtIWu5mi9IDD3dtcrjiJzBuBYvxHAzS2 
AlYM0qEhyDlKhWYKqZW/xGaZcqNUCRCWzG4eplaSOQbJC84n/d2Qg5mebwgO 
mtjoJ0C+5TgwkLdYd8TnWtkulqRW5Wsgk1LIZMvAWzQNc8eNR5uLaXbNckK1 
PG6/9myBJSKOhJcSg0mHbcleF6egFfthg6tRXTm2EgHTlNnkRUTSDx3OhCT6 
DEQDmDo92lAs1ocJrBXUNRCMGgNrsowsi0pvmEJY2eBQkb4syUh7YzfeIdYw 
eQo60AwA/yazFqwlZvmUDgMtXcNBABEBAAHCwF8EGAEIABMFAlU9xmIJEDkq 
Su1fklRvAhsMAABhOAf9FujFEEv8DfK4wHP3m+VUtOl6TOcK3oDnuw+wO+Jb 
VyURM0BktLK4NtCD2q8l8+3W9MaOFQ2nFmf5bnFTghvItRcuMmWJF+vyLlIv 
QhfpAPMVjFnQjsVr8fYd3t+kep+Hw2ihPBXaHxyESUxA5QRiRuPs56yLFUK7 
horcHsDXl3I8+FyW3iQ2eBgk5+9d97va1ZxjUvNDZDm8fDvBKyRT3As4QBbF 
DPI26u+v89EfAI3Q2HZwt08vhu8TkjK2y/wZYT9ihruzDfdR/XXExCwUq/rx 
WhaKXDwc9fuqhU9wSNadjKVPytW9vkro9i4PgW7zRy+NepfWNlrP7R9jxLc+ 
2w== 
=wGlq 
-----END PGP PUBLIC KEY BLOCK----- 

回答

1

你可以用Bouncy Castle Crypto APIs https://www.bouncycastle.org來做到這一點。

KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); 
keyGen.initialize(1024); 
KeyPair key = keyGen.generateKeyPair(); 
PrivateKey priv = key.getPrivate(); 
PublicKey pub = key.getPublic(); 
String privateKey = new String(Base64.encode(priv.getEncoded(), 0,priv.getEncoded().length, Base64.NO_WRAP)); 
String publicKey1 = new String(Base64.encode(pub.getEncoded(), 0,pub.getEncoded().length, Base64.NO_WRAP)); 
String publicKey = new String(Base64.encode(publicKey1.getBytes(),0, publicKey1.getBytes().length, Base64.NO_WRAP)); 

至於添加名稱,電子郵件和密碼的東西,你指的是生成一個證書,這需要生成一個密鑰對。證書類型因使用情況而異,生成證書的方式也不同。

如果你希望真正保護通信,http://www.amazon.com/Applied-Cryptography-Protocols-Algorithms-Source/dp/0471117099/是一個重要的閱讀,因爲生成不良的證書和密鑰交換比根本沒有密碼更糟糕。

+0

這有幫助嗎?如果有的話,請接受答案!乾杯。 –