我们在开发中需要对用户敏感数据进行加解密,比如密码
这边科普一下RSA算法
RSA是非对称加密算法,与对称加密算法不同;在对称加密中,相同的密钥用于加密和解密数据,因此密钥的安全性至关重要;而在RSA非对称加密中,有两个密钥,一个是公钥,用于加密数据,另一个是私钥,用于解密数据;这意味着公钥可以公开分发,而私钥必须保持秘密;
RSA非对称加密的主要应用包括:
数据加密:使用接收者的公钥加密数据,只有拥有相应私钥的接收者才能解密;
数字签名:使用发送者的私钥对数据签名,接收者可以使用发送者的公钥验证签名,确保数据的完整性和来源的真实性;
密钥协商:RSA也用于安全协议中,如TLS/SSL,用于安全地交换对称加密密钥,从而实现保密通信;
非对称加密算法提供了更高的安全性,因为加密和解密使用不同的密钥,攻击者无法从公钥推导出私钥;但由于非对称加密计算成本高昂,通常不用于大规模数据的加密,而是用于安全协商和数字签名等场景今天就实现了一个RSA工具类,可以很轻松的对数据进行加解密
不需要加依赖,代码如下
public class RSAUtils {
    /**
     * @param plaintext 要加密的字符串
     * @param publicKeyStr 传入的公钥,是一个字符串
     * @return 加密后的字符串, 以Base64编码的形式返回
     * @throws Exception 异常
     * 这个方法接受一个要加密的字符串和一个公钥字符串,使用公钥进行加密,然后返回加密后的字符串
     */
    public static String encrypt(String plaintext, String publicKeyStr) throws Exception {
        PublicKey publicKey = getPublicKeyFromString(publicKeyStr);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }
    /**
     * @param encryptedText 要解密的字符串
     * @param privateKeyStr 传入的私钥,是一个字符串
     * @return 解密后的原始字符串
     * @throws Exception 异常
     * 这个方法接受一个要解密的字符串和一个私钥字符串,使用私钥进行解密,然后返回解密后的原始字符串
     */
    public static String decrypt(String encryptedText, String privateKeyStr) throws Exception {
        PrivateKey privateKey = getPrivateKeyFromString(privateKeyStr);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }
    /**
     * @return
     * @throws Exception
     * 随机生成一个长度为2048的RSA公私钥对
     */
    public static KeyPair generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }
    /**
     * @param publicKey
     * @return
     * 拿出刚生成Base64格式的私钥对的公钥字符串
     */
    public static String publicKeyToString(PublicKey publicKey) {
        return Base64.getEncoder().encodeToString(publicKey.getEncoded());
    }
    /**
     * @param privateKey
     * @return
     * 拿出刚生成Base64格式的私钥对的私钥字符串
     */
    public static String privateKeyToString(PrivateKey privateKey) {
        return Base64.getEncoder().encodeToString(privateKey.getEncoded());
    }
    /**
     * @param publicKeyStr
     * @return 公钥私钥对象
     * @throws Exception
     * 将刚拿出的Base64格式的私钥对的私钥字符串生成公钥对象
     */
    public static PublicKey getPublicKeyFromString(String publicKeyStr) throws Exception {
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyStr);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(spec);
    }
    /**
     * @param privateKeyStr
     * @return
     * @throws Exception
     * 将刚拿出的Base64格式的私钥对的私钥字符串生成私钥对象
     */
    public static PrivateKey getPrivateKeyFromString(String privateKeyStr) throws Exception {
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyStr);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(spec);
    }
    public static void main(String[] args) throws Exception {
        // 生成RSA密钥对
        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        // 将公钥和私钥转换为字符串
        String publicKeyStr = publicKeyToString(publicKey);
        String privateKeyStr = privateKeyToString(privateKey);
        System.out.println("公钥: " + publicKeyStr);
        System.out.println("私钥: " + privateKeyStr);
        // 加密和解密测试
        String plaintext = "大白猫真厉害";
        String encryptedText = encrypt(plaintext, publicKeyStr);
        System.out.println("加密后的子串: " + encryptedText);
        String decryptedText = decrypt(encryptedText, privateKeyStr);
        System.out.println("解密后的子串: " + decryptedText);
    }
}
结果如下

将数据用公钥加密,用私钥解密,这样就可以了

![队列排序:给定序列a,每次操作将a[1]移动到 从右往左第一个严格小于a[1]的元素的下一个位置,求能否使序列有序,若可以,求最少操作次数](https://img-blog.csdnimg.cn/direct/d00b9c3138bb48b7a5e3850fe91d6aa8.png)

















