1、什麼是RSA 算法
RSA加密算法是一種非對稱加密算法。在公開密鑰加密和電子商業中RSA被廣泛使用。RSA是1977年由羅納德·李維斯特(Ron Rivest)、阿迪·薩莫爾(Adi Shamir)和倫納德·阿德曼(Leonard Adleman)一起提出的。當時他們三人都在麻省理工學院工作。RSA就是他們三人姓氏開頭字母拼在一起組成的。
對極大整數做因數分解的難度決定了RSA算法的可靠性。換言之,對一極大整數做因數分解愈困難,RSA算法愈可靠。假如有人找到一種快速因數分解的算法的話,那麼用RSA加密的資訊的可靠性就肯定會極度下降。但找到這樣的算法的可能性是非常小的。今天隻有短的RSA鑰匙才可能被強力方式解破。到目前為止,世界上還沒有任何可靠的攻擊RSA算法的方式。隻要其鑰匙的長度足夠長,用RSA加密的資訊實際上是不能被解破的。
2、RSA算法過程
3、RAS算法實作
3.1、JDK算法實作
package lzf.cipher.jdk;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
/**
* @author Java小工匠
*/
public class JdkRsaUtils {
// 初始化密鑰對
public static KeyPair initKey() {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
// 512 -65536 && 64 的倍數
generator.initialize(1024);
return generator.generateKeyPair();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
// 擷取公鑰
public static byte[] getPublicKey(KeyPair keyPair) {
byte[] bytes = keyPair.getPublic().getEncoded();
return bytes;
}
// 擷取公鑰
public static String getPublicKeyStr(KeyPair keyPair) {
byte[] bytes = keyPair.getPublic().getEncoded();
return encodeHex(bytes);
}
// 擷取私鑰
public static byte[] getPrivateKey(KeyPair keyPair) {
byte[] bytes = keyPair.getPrivate().getEncoded();
return bytes;
}
// 擷取私鑰
public static String getPrivateKeyStr(KeyPair keyPair) {
byte[] bytes = keyPair.getPrivate().getEncoded();
return encodeHex(bytes);
}
// 加密資料
public static byte[] encryptRsa(byte[] data, byte[] key) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
RSAPublicKey secretKey = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.PUBLIC_KEY, secretKey);
byte[] rs = cipher.doFinal(data);
return rs;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 解密資料
public static byte[] decryptRsa(byte[] data, byte[] key) {
try {
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key);
RSAPrivateCrtKey secretKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(keySpec);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.PRIVATE_KEY, secretKey);
byte[] rs = cipher.doFinal(data);
return rs;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 資料準16進制編碼
public static String encodeHex(final byte[] data) {
return encodeHex(data, true);
}
// 資料轉16進制編碼
public static String encodeHex(final byte[] data, final boolean toLowerCase) {
final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
final char[] DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
final char[] toDigits = toLowerCase ? DIGITS_LOWER : DIGITS_UPPER;
final int l = data.length;
final char[] out = new char[l << 1];
// two characters form the hex value.
for (int i = 0, j = 0; i < l; i++) {
out[j++] = toDigits[(0xF0 & data[i]) >>> 4];
out[j++] = toDigits[0x0F & data[i]];
}
return new String(out);
}
public static void main(String[] args) throws Exception {
String str = "java小工匠";
KeyPair keyPair = initKey();
byte[] publicKey = getPublicKey(keyPair);
byte[] privateKey = getPrivateKey(keyPair);
byte[] secretData = encryptRsa(str.getBytes(), publicKey);
System.out.println("RSA加密:" + encodeHex(secretData));
byte[] data = decryptRsa(secretData, privateKey);
System.out.println("RSA解密:" + new String(data));
}
}