RSA是由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)在1977年一起提出的。当时他们三人都在麻省理工学院工作。RSA 就是他们三人姓氏开头字母拼在一起组成的。
对极大整数做因数分解的难度决定了 RSA 算法的可靠性
java案例
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class RsaUtils {
private static final String ALGORITHM = "RSA";
public static KeyPair generateKey() throws NoSuchAlgorithmException {
return generateKey(1024);
}
public static KeyPair generateKey(int keysize) throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
keyPairGenerator.initialize(keysize); // 512 1024 2048 4096 8192 16384 32768 65536
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
private static PublicKey getPublicKey(byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
PublicKey publicKey = KeyFactory.getInstance(ALGORITHM).generatePublic(x509EncodedKeySpec);
return publicKey;
}
private static PrivateKey getPrivateKey(byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
PrivateKey privateKey = KeyFactory.getInstance(ALGORITHM).generatePrivate(pkcs8EncodedKeySpec);
return privateKey;
}
public static byte[] encrypt(boolean isPrivate, byte[] key, byte[] data) throws Exception {
Key thisKey = isPrivate ? getPrivateKey(key) : getPublicKey(key);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, thisKey);
byte[] bytes = cipher.doFinal(data);
return bytes;
}
public static byte[] decrypt(boolean isPrivate, byte[] key, byte[] data) throws Exception {
Key thisKey = isPrivate ? getPrivateKey(key) : getPublicKey(key);
Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, thisKey);
byte[] bytes = cipher.doFinal(data);
return bytes;
}
}
测试
import org.apache.commons.codec.binary.base64;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
public class RsaUtilsTest {
public static void main(String[] args) throws Exception {
KeyPair keyPair = RsaUtils.generateKey();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
byte[] publicKey = rsaPublicKey.getEncoded();
System.out.println("公钥:n" + base64.encodebase64String(publicKey));
byte[] privateKey = rsaPrivateKey.getEncoded();
System.out.println("私钥:n" + base64.encodebase64String(privateKey));
//test1(publicKey, privateKey, "你好世界");
test2(publicKey, privateKey, "你好祖国");
}
private static void test1(byte[] publicKey, byte[] privateKey, String text) throws Exception {
// 公钥加密 私钥解密---加密
byte[] data = RsaUtils.encrypt(false, publicKey, text.getBytes());
System.out.println("密文:n" + base64.encodebase64String(data));
// 公钥加密 私钥解密---解密
byte[] result = RsaUtils.decrypt(true, privateKey, data);
System.out.println("解密后:n" + new String(result));
}
private static void test2(byte[] publicKey, byte[] privateKey, String text) throws Exception {
// 私钥加密 公钥解密---加密
byte[] data = RsaUtils.encrypt(true, privateKey, text.getBytes());
System.out.println("密文:n" + base64.encodebase64String(data));
// 私钥加密 公钥解密---解密
byte[] result = RsaUtils.decrypt(false, publicKey, data);
System.out.println("解密后:n" + new String(result));
}
}
gitee-code



