【java】RSA前后端互通-前端开发牛翰社区-编程开发-牛翰网

【java】RSA前后端互通

后端

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
 
public class RsaUtil {

    /**
     * @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/ECB/PKCS1Padding");
        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/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }

    /**
     * @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 {
        //密钥生成:生成地址:http://web.chacuo.net/netrsakeypair  密码格式:PKCS#8
        String publicKeyStr = "";
        String privateKeyStr = "";
 
        String plaintext = "admin123455";
        String encryptedText = encrypt(plaintext, publicKeyStr);
        System.out.println("加密后: " + encryptedText);
        System.out.println("解密后: " + decrypt(encryptedText, privateKeyStr));

        System.out.println("公钥: " + publicKeyStr);
        System.out.println("私钥: " + privateKeyStr);
    }
}

  

前端

 

请登录后发表评论

    没有回复内容