RSA是最常用的非对称加密算法。
所谓非对称加密,就是说有两个密钥,一个密钥加密只可以用另外一个密钥解密,一般一个作为公钥,公开给所有人用来加密用,而另一个用来解密其他拥有公钥的加密结果,叫做私钥。另外,拥有私钥者可以用私钥加密信息,公钥可以解密获得加密内容,从而验证私钥拥有者的身份,这是一种特殊的加密,叫签名。
java实现
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
import org.apache.commons.codec.binary.Base64;
public class RSAUtils2 {
public static final String RSA_ECB_PKCS1_PADDING = "RSA/ECB/PKCS1Padding";
public static final int KEY_SIZE_2048 = 2048;
public static final int KEY_SIZE_1024 = 1024;
private static final String ALGORITHM = "RSA";
/**
* 设置key长度
* @return 设置key
*/
public static KeyPair generateKeyPair() {
return generateKeyPair(KEY_SIZE_2048);
}
/**
*
* @param keySize key
* @return key
*/
public static KeyPair generateKeyPair(int keySize) {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(keySize);
return keyPairGenerator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Failed to generate key pair!", e);
}
}
/**
* key
* @param base64PublicKey 获取公钥
* @return 公钥
*/
public static PublicKey getPublicKey(String base64PublicKey) {
try {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.decodeBase64(base64PublicKey));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
} catch (Exception e) {
throw new IllegalArgumentException("Failed to get public key!", e);
}
}
/**
* fs
* @param modulus vd
* @param exponent f
* @return f
*/
public static PublicKey getPublicKey(BigInteger modulus, BigInteger exponent) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(modulus, exponent);
return keyFactory.generatePublic(keySpec);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to get public key!", e);
}
}
/**
* dfws
* @param publicKey fds
* @return fs
*/
public static String getBase64PublicKey(PublicKey publicKey) {
return Base64.encodeBase64String(publicKey.getEncoded());
}
/**
* fs
* @param base64PrivateKey f
* @return fs
*/
public static PrivateKey getPrivateKey(String base64PrivateKey) {
try {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(base64PrivateKey));
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
} catch (Exception e) {
throw new IllegalArgumentException("Failed to get private key!", e);
}
}
public static PrivateKey getPrivateKey(BigInteger modulus, BigInteger exponent) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(modulus, exponent);
return keyFactory.generatePrivate(keySpec);
} catch (Exception e) {
throw new IllegalArgumentException("Failed to get private key!", e);
}
}
public static String getBase64PrivateKey(PrivateKey privateKey) {
return Base64.encodeBase64String(privateKey.getEncoded());
}
public static byte[] encryptAsByteArray(String data, PublicKey publicKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ECB_PKCS1_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data.getBytes());
} catch (Exception e) {
throw new IllegalArgumentException("Encrypt failed!", e);
}
}
public static byte[] encryptAsByteArray(String data, String base64PublicKey) {
return encryptAsByteArray(data, getPublicKey(base64PublicKey));
}
public static String encryptAsString(String data, PublicKey publicKey) {
return Base64.encodeBase64String(encryptAsByteArray(data, publicKey));
}
public static String encryptAsString(String data, String base64PublicKey) {
return Base64.encodeBase64String(encryptAsByteArray(data, getPublicKey(base64PublicKey)));
}
public static String decrypt(byte[] data, PrivateKey privateKey) {
try {
Cipher cipher = Cipher.getInstance(RSA_ECB_PKCS1_PADDING);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return new String(cipher.doFinal(data));
} catch (Exception e) {
throw new IllegalArgumentException("Decrypt failed!", e);
}
}
public static String decrypt(byte[] data, String base64PrivateKey) {
return decrypt(data, getPrivateKey(base64PrivateKey));
}
public static String decrypt(String data, PrivateKey privateKey) {
return decrypt(Base64.decodeBase64(data), privateKey);
}
public static String decrypt(String data, String base64PrivateKey) {
return decrypt(Base64.decodeBase64(data), getPrivateKey(base64PrivateKey));
}
public static void main(String[] args) {
KeyPair keyPair = RSAUtils2.generateKeyPair();
String privateKey = Base64.encodeBase64String(keyPair.getPrivate().getEncoded());
String publicKey = Base64.encodeBase64String(keyPair.getPublic().getEncoded());
System.out.println("私钥privateKey:" + privateKey);
System.out.println("公钥publicKey:" + publicKey);
String content = "18811122222";
System.out.println("原始字符串:" + content);
//公钥加密
String encryptAsString = RSAUtils2.encryptAsString(content, publicKey);
System.out.println("rsa加密后字符串:" + encryptAsString);
//私钥解密
String decrypt = RSAUtils2.decrypt(encryptAsString, privateKey);
System.out.println("rsa解密后字符串:" + decrypt);
}
}