私鑰加密-->公鑰解密,反之亦然,但不安全。也能夠當作數字簽名。
public
class
RSACoder {
//非對稱加密算法
public
static
final
String
KEY_ALGORITHM
=
"RSA"
;
//公鑰
private
static
final
String
PUBLIC_KEY
=
"RSAPublicKey"
;
//私鑰
private
static
final
String
PRIVATE_KEY
=
"RSAPrivateKey"
;
//密鑰長度,默認1024位,512~65536位,必須是64的倍數
private
static
final
int
KEY_SIZE
=512;
//私鑰解密
public
static
byte
[] decryptByPrivateKey(
byte
[] data,
byte
[] key)
throws
KeyException, Exception{
//取得私鑰,
PKCS8EncodedKeySpec pkcs8KeySpec=
new
PKCS8EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8KeySpec);
//對數據解密
Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.
DECRYPT_MODE
, privateKey);
return
cipher.doFinal(data);
}
//公鑰解密
public
static
byte
[] decryptByPublicKey(
byte
[] data,
byte
[] key)
throws
Exception{
X509EncodedKeySpec x509=
new
X509EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
//生成公鑰
PublicKey publicKey=keyFactory.generatePublic(x509);
//對數據解密
Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.
DECRYPT_MODE
, publicKey);
return
cipher.doFinal(data);
}
//公鑰加密
public
static
byte
[] encrpytByPublicKey(
byte
[] data,
byte
[] key)
throws
Exception{
//取得公鑰
X509EncodedKeySpec x509=
new
X509EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
PublicKey pubKey=keyFactory.generatePublic(x509);
//對數據加密
Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.
ENCRYPT_MODE
, pubKey);
return
cipher.doFinal(data);
}
//私鑰加密
public
static
byte
[] encryptByPrivate(
byte
[] data,
byte
[] key)
throws
Exception, GeneralSecurityException{
//取得私鑰
PKCS8EncodedKeySpec pkcs8=
new
PKCS8EncodedKeySpec(key);
KeyFactory keyFactory=KeyFactory. getInstance(
KEY_ALGORITHM
);
//生成私鑰
PrivateKey privateKey=keyFactory.generatePrivate(pkcs8);
//對數據加密
Cipher cipher=Cipher. getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.
ENCRYPT_MODE
, privateKey);
return
cipher.doFinal(data);
}
//取得私鑰
public
static
byte
[] getPrivateKey(Map<String,Object> keyMap){
Key key=(Key)keyMap.get(
PRIVATE_KEY
);
return
key.getEncoded();
}
//取得公鑰
public
static
byte
[] getPublicKey(Map<String,Object> keyMap){
Key key=(Key)keyMap.get(
PUBLIC_KEY
);
return
key.getEncoded();
}
//初始化密鑰
public
static
Map<String,Object> initKey()
throws
Exception{
KeyPairGenerator keyPairGen=KeyPairGenerator.getInstance(
KEY_ALGORITHM
);
keyPairGen.initialize(
KEY_SIZE
);
KeyPair keyPair=keyPairGen.generateKeyPair();
//公鑰
RSAPublicKey publicKey=(RSAPublicKey)keyPair.getPublic();
//私鑰
RSAPrivateKey privateKey=(RSAPrivateKey)keyPair.getPrivate();
//封裝密鑰
Map<String,Object> keyMap=
new
HashMap<String,Object>(2);
keyMap.put(
PRIVATE_KEY
, privateKey);
keyMap.put(
PUBLIC_KEY
, publicKey);
return
keyMap;
}
}
public
class
RSATest {
private
static
byte
[]
privateKey
;
private
static
byte
[]
publicKey
;
public
static
void
initKey()
throws
Exception{
//初始化密鑰
Map<String,Object> keyMap=RSACoder. initKey();
publicKey
=RSACoder.getPublicKey(keyMap);
privateKey
=RSACoder.getPrivateKey(keyMap);
System.
out
.println(
"公鑰:\n"
+Base64.encodeBase64String (
publicKey
));
System.
out
.println(
"私鑰:\n"
+Base64.encodeBase64String (
privateKey
));
}
public
static
void
main(String[] args)
throws
GeneralSecurityException, Exception {
//
TODO
Auto-generated method stub
initKey();
String inputStr1=
"RSA加密算法"
;
byte
[] data1=inputStr1.getBytes();
System.
out
.println(
"原文:\n"
+inputStr1);
//私鑰加密
byte
[] encodeData1=RSACoder.encryptByPrivate(data1,
privateKey
);
System.
err
.println(
"加密後:\n"
+Base64.encodeBase64String (encodeData1));
//公鑰解密
byte
[] decodeData1=RSACoder.decryptByPublicKey(encodeData1,
publicKey
);
String outputStr1=
new
String(decodeData1);
System.
err
.println(
"解密後:\n"
+outputStr1);
assertEquals(inputStr1,outputStr1);
}
}