“RAS算法”這個術語本身并不常見或標準,它可能指向兩個主要領域的不同概念,具體取決于上下文:
- 更可能是拼寫錯誤:指 RSA 算法(密碼學)
- 這是最常見的情況。 “RAS” 極有可能是 “RSA” 的拼寫錯誤。
- RSA 算法 是現代密碼學中最重要和最廣泛使用的公鑰加密算法之一。它由 Ron Rivest, Adi Shamir 和 Leonard Adleman 于 1977 年提出,因此得名。
- 核心概念:
- 非對稱加密: 使用一對密鑰:公鑰和私鑰。
- 公鑰: 可以公開給任何人,用于加密消息或驗證數字簽名。
- 私鑰: 必須嚴格保密,用于解密用對應公鑰加密的消息或創建數字簽名。
- 工作原理基礎:
- 密鑰生成:
- 選擇兩個非常大的、不同的質數
p
和q
。 - 計算模數
n = p * q
。 - 計算歐拉函數
φ(n) = (p-1) * (q-1)
。 - 選擇一個整數
e
(公鑰指數),滿足1 < e < φ(n)
且e
與φ(n)
互質(即gcd(e, φ(n)) = 1
)。 - 計算整數
d
(私鑰指數),滿足(d * e) ≡ 1 mod φ(n)
。即d
是e
關于模φ(n)
的模反元素。
- 公鑰:
(e, n)
- 私鑰:
(d, n)
- 選擇兩個非常大的、不同的質數
- 加密 (使用公鑰): 對于明文消息
M
(轉換為小于n
的整數),計算密文C = M^e mod n
。 - 解密 (使用私鑰): 對于密文
C
,計算明文M = C^d mod n
。
- 密鑰生成:
- 安全性基礎: RSA 的安全性依賴于大整數分解難題。從公開的
n
推導出私鑰d
需要分解n
得到p
和q
。當p
和q
是足夠大(例如 2048 位或更長)的質數時,分解n
在計算上是不可行的(至少在經典計算機上是這樣)。 - 應用:
- 安全數據傳輸(如 HTTPS/SSL/TLS)
- 數字簽名(驗證文件來源和完整性)
- 軟件保護
- 安全電子郵件 (PGP, S/MIME)
- 區塊鏈和加密貨幣
source接口
/**
* @ClassName RAS加密算法 Source
* 待適配
* @Author ykx
* @Date 2024/5/8 8:44
* @Version 1.0
*/
@Data
public class RASUtil {/*** 公鑰*/public static PublicKey publicKey;/*** 私鑰*/public static PrivateKey privateKey;/*** 加密器*/public static Cipher cipher;public static final String RSA = "RSA";/*** 獲取公鑰和私鑰*/static {// 1.獲取公鑰私鑰KeyPairGenerator keyPairGen = null;// 獲取RSA算法實例try {// // 獲取RSA算法實例keyPairGen = KeyPairGenerator.getInstance(RSA);// 1024代表密鑰二進制位數keyPairGen.initialize(1024);// 產生KeyPair工廠KeyPair keyPair = keyPairGen.generateKeyPair();publicKey = keyPair.getPublic();privateKey = keyPair.getPrivate();cipher = Cipher.getInstance(RSA);} catch (Exception e) {e.printStackTrace();}}/*** 私鑰加密** @return 加密后的數據*/public byte[] privateEncode(String content) throws Exception {cipher.init(Cipher.ENCRYPT_MODE, privateKey);return cipher.doFinal(content.getBytes());}/*** 公鑰解密** @return 解密后的數據*/public String publicDecode(byte[] encodeContent) throws Exception {cipher.init(Cipher.DECRYPT_MODE, publicKey);byte[] decodeContent = cipher.doFinal(encodeContent);return new String(decodeContent);}}
目標接口:TargetAble
public interface TargetAble {/*** 私鑰加密** @return 加密后的數據*/byte[] privateEncode(String content) throws Exception;/*** 公鑰解密** @return 解密后的數據*/String publicDecode(byte[] encodeContent) throws Exception;/*** 公鑰加密** @return 加密后的數據*/byte[] publicEncode(String content) throws Exception;/*** 私鑰解密** @return 解密后的數據*/String privateDecode(byte[] encodeContent) throws Exception;/*** 加簽** @param bytes 加簽數據* @return 簽名后的結果*/String sign(byte[] bytes) throws Exception;/*** 驗簽** @param bytes* @return* @throws Exception*/Boolean verify(byte[] bytes, String sign) throws Exception;}
適配器:
@EqualsAndHashCode(callSuper = false)
@Data
public class Adapter extends RASUtil implements TargetAble {/*** 加簽*/public static Signature signature;public final static String SIGNATURE_ALGORITHM = "MD5withRSA";static {try {// 獲取簽名算法signature = Signature.getInstance(SIGNATURE_ALGORITHM);signature.initSign(privateKey);} catch (Exception e) {e.printStackTrace();}}/*** 公鑰加密** @param content* @return 加密后的數據*/@Overridepublic byte[] publicEncode(String content) throws Exception {RASUtil.cipher.init(Cipher.ENCRYPT_MODE, publicKey);return cipher.doFinal(content.getBytes());}/*** 私鑰解密** @param encodeContent* @return 解密后的數據*/@Overridepublic String privateDecode(byte[] encodeContent) throws Exception {cipher.init(Cipher.DECRYPT_MODE, privateKey);byte[] decodeContent = cipher.doFinal(encodeContent);return new String(decodeContent);}/*** 加簽** @param bytes 加簽數據* @return 簽名后的結果*/@Overridepublic String sign(byte[] bytes) throws Exception {signature.update(bytes);return Base64.getEncoder().encodeToString(signature.sign());}/*** 驗簽** @param bytes* @return* @throws Exception*/@Overridepublic Boolean verify(byte[] bytes, String sign) throws Exception {signature.initVerify(publicKey);signature.update(bytes);return signature.verify(Base64.getDecoder().decode(sign));}
}
測試類:
public class Main {// Adapter RASUtil TargetAble 三個類組合的適配器為類的適配器模式public static void main(String[] args) throws Exception {TargetAble targetAble = new Adapter();String content = "學習算法,有益身心健康!!";// 公鑰加密,私鑰解密byte[] publicEncode = targetAble.publicEncode(content);System.out.println("公鑰加密:" + Base64.getEncoder().encodeToString(publicEncode));System.out.println("私鑰解密:" + targetAble.privateDecode(publicEncode));// 私鑰加密,公鑰解密byte[] privateEncode = targetAble.privateEncode(content);System.out.println("私鑰加密:" + Base64.getEncoder().encodeToString(privateEncode));System.out.println("公鑰解密:" + targetAble.publicDecode(privateEncode));// 加簽String sign = targetAble.sign(content.getBytes());System.out.println("加簽后:" + sign);// 驗簽Boolean verify = targetAble.verify(content.getBytes(), sign);System.out.println("驗簽結果:" + verify);}
}