public class RSAStrategy implements IStrategy
{
private RSAPublicKey rsaPublicKey;
private RSAPrivateKey rsaPrivateKey;
public String encode(String src)
{
try
{
// 初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
rsaPublicKey = (RSAPublicKey)keyPair.getPublic();
rsaPrivateKey = (RSAPrivateKey)keyPair.getPrivate();
// 私钥加密 公钥解密
PKCS8EncodedKeySpec pkcs8EncodedKeySpec
= new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, privateKey);
byte[] resultBytes = cipher.doFinal(src.getBytes());
return Hex.encodeHexString(resultBytes);
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
public String decode(String src)
{
try
{
// 私钥加密 公钥解密
X509EncodedKeySpec x509EncodedKeySpec =
new X509EncodedKeySpec(rsaPublicKey.getEncoded());
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, publicKey);
byte[] resultBytes = cipher.doFinal(Hex.decodeHex(src.toCharArray()));
return new String(resultBytes);
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
|