RSA密钥对的生成:
public class KeyPairGenUtil {
/**
* 指定加密算法为RSA
*/
private static final String ALGORITHM = "RSA";
/**
* 密钥长度,用来初始化
*/
private static final int KEYSIZE = 1024;
/**
* 生成密钥对
*
* @throws Exception
*/
public static void generateKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(KEYSIZE);
/** 生成密匙对 */
KeyPair keyPair = keyPairGenerator.generateKeyPair();
/** 得到公钥 */
Key publicKey = keyPair.getPublic();
/** 得到私钥 */
Key privateKey = keyPair.getPrivate();
String publicKeyStr = new BASE64Encoder().encode(publicKey.getEncoded());
String privateKeyStr = new BASE64Encoder().encode(privateKey.getEncoded());
System.out.println("pub:" + publicKeyStr);
System.out.println("pri:" + privateKeyStr);
}
}
加密与解密
public class RsaUtils {
private static final String ALGORITHM = "RSA";
/**
* 得到PublicKey
* @param publicKeyString
* @return
* @throws Exception
*/
public static PublicKey getPublicKeyFromStringValue(String publicKeyString) throws Exception {
//base64 decode the key data to get a byte array (byte[])
byte[] bytes = new BASE64Decoder().decodeBuffer(publicKeyString);
//Create a new X509EncodedKeySpec using the byte array
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes);
//Get an instance of KeyFactory using KeyFactory.getInstance("RSA") assuming RSA here
//call the method generatePublic(KeySpec) with the X509EncodedKeySpec
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM).generatePublic(x509EncodedKeySpec);
//Result /should/ be a public key for your usage.
return publicKey;
}
/**
* 得到PrivateKey
* @param privateKeyString
* @return
* @throws Exception
*/
public static PrivateKey getPrivateKeyFromStringValue(String privateKeyString) throws Exception {
//base64 decode the key data to get a byte array (byte[])
byte[] bytes = new BASE64Decoder().decodeBuffer(privateKeyString);
//Create a new X509EncodedKeySpec using the byte array
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(bytes);
//Get an instance of KeyFactory using KeyFactory.getInstance("RSA") assuming RSA here
//call the method generatePublic(KeySpec) with the X509EncodedKeySpec
PrivateKey privateKey = KeyFactory.getInstance(ALGORITHM).generatePrivate(pkcs8EncodedKeySpec);
//Result /should/ be a public key for your usage.
return privateKey;
}
/**
*
* @param source
* @param publicKey
* @return
* @throws Exception
*/
public static String encrypt(String source, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] b = source.getBytes();
byte[] b1 = cipher.doFinal(b);
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b1);
}
/**
*
* @param encryptedContent
* @param privateKey
* @return
* @throws Exception
*/
public static String decrypt(String encryptedContent, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
BASE64Decoder decoder = new BASE64Decoder();
byte[] b1 = decoder.decodeBuffer(encryptedContent);
byte[] b = cipher.doFinal(b1);
return new String(b);
}
}