RAS加密

作者: 雨来 | 来源:发表于2022-11-30 14:00 被阅读0次

参考:
https://www.jianshu.com/p/0bf4fa78928e
有关获取项目路径:
https://blog.csdn.net/weixin_43888891/article/details/122401256

public class RSAEncrypt {
    //指定加密算法为RSA
    private static final String ALGORITHM = "RSA";
    //指定密钥长度
    private static final int KEY_SIZE = 1024;
    //指定私钥存放文件
    private static final String PRIVATE_KEY_FILE = "PrivateKey";
    //指定公钥存放文件
    private static final String PUBLIC_KEY_FILE = "PublicKey";

    /**
     * 生成密钥
     */
    void generateKey() {
        try {
            //指定随机数源
            SecureRandom secureRandom = new SecureRandom();
            //为RSA算法创建一个KeyPairGenerator对象
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
            //初始化KeyPairGenerator对象
            keyPairGenerator.initialize(KEY_SIZE, secureRandom);
            //生成密钥对
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            //获取私钥
            Key privateKey = keyPair.getPrivate();
            //获取公钥
            Key publicKey = keyPair.getPublic();
            //将私钥和公钥写入文件
            ObjectOutputStream privateKeyStream = new ObjectOutputStream(
                    new FileOutputStream(PRIVATE_KEY_FILE));
            ObjectOutputStream publicKeyStream = new ObjectOutputStream(
                    new FileOutputStream(PUBLIC_KEY_FILE));
            privateKeyStream.writeObject(privateKey);
            publicKeyStream.writeObject(publicKey);
            privateKeyStream.close();
            publicKeyStream.close();
        } catch (NoSuchAlgorithmException | IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加密算法
     * @param plaintext  明文
     * @return  密文
     */
    public String encrypt(String plaintext){
        try {
            //读取文件获取公钥
            ObjectInputStream inputStream = new ObjectInputStream(
                    new FileInputStream(PUBLIC_KEY_FILE));
            Key publicKey = (Key) inputStream.readObject();
            inputStream.close();
            //得到Cipher对象来实现RSA加密算法
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] bytes = cipher.doFinal(plaintext.getBytes());
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(bytes);
        } catch (IOException | ClassNotFoundException | NoSuchPaddingException
                 | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
                 | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密算法
     * @param ciphertext  密文
     * @return  明文
     */
    public String decrypt(String ciphertext){
        try {
            //读取文件获取私钥
            ObjectInputStream inputStream = new ObjectInputStream(
                    new FileInputStream(PRIVATE_KEY_FILE));
            Key privateKey = (Key) inputStream.readObject();
            inputStream.close();
            //得到Cipher对象来实现RSA解密算法
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] bytes = base64Decoder.decodeBuffer(ciphertext);
            return new String(cipher.doFinal(bytes));
        } catch (IOException | ClassNotFoundException | NoSuchPaddingException
                 | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
                 | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }
}

生成公钥和私钥文件

在一个java 编译环境下生成两个文件

  RSAEncrypt rsaEncrypt = new RSAEncrypt();
        rsaEncrypt.generateKey();
image.png

公钥加密私钥解密

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.security.*;
public class Main {
    //指定加密算法为RSA
    private static final String ALGORITHM = "RSA";
    //指定密钥长度
    private static final int KEY_SIZE = 1024;
    private static String projectPath ="C:\\Users\\yulai\\IdeaProjects\\AESDemo\\";

    public static void main(String[] args) {
        String secretStr = encrypt("中国人");
        System.out.println("加密后的值:"+secretStr);
        String decrypt = decrypt(secretStr);
        System.out.println(decrypt);
    }
    /**
     * 加密算法
     * @param plaintext  明文
     * @return  密文
     */
    public static String encrypt(String plaintext){
        try {
            //读取文件获取公钥 这里放生成的key
            ObjectInputStream inputStream = new ObjectInputStream(
                    new FileInputStream(projectPath+"PublicKey"));
            Key publicKey = (Key) inputStream.readObject();
            inputStream.close();
            //得到Cipher对象来实现RSA加密算法
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] bytes = cipher.doFinal(plaintext.getBytes());
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(bytes);
        } catch (IOException | ClassNotFoundException | NoSuchPaddingException
                 | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
                 | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密算法
     * @param ciphertext  密文
     * @return  明文
     */
    public static String decrypt(String ciphertext){
        try {
            //读取文件获取私钥
            ObjectInputStream inputStream = new ObjectInputStream(
                    new FileInputStream(projectPath+"PrivateKey"));
            Key privateKey = (Key) inputStream.readObject();
            inputStream.close();
            //得到Cipher对象来实现RSA解密算法
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] bytes = base64Decoder.decodeBuffer(ciphertext);
            return new String(cipher.doFinal(bytes));
        } catch (IOException | ClassNotFoundException | NoSuchPaddingException
                 | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
                 | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }
}
image.png

相关文章

  • ras 加密

  • RAS加密

  • RAS加密

    参考:https://www.jianshu.com/p/0bf4fa78928e[https://www.jia...

  • AES&RSA 加密算法流程图

    AES&RAS加密算法流程图

  • PHP RAS加密解密

    前言:RSA加密一般用在涉及到重要数据时所使用的加密算法,比如用户的账户密码传输,订单的相关数据传输等。 加密方式...

  • 登录密码校验

    RAS加密 生成公、私钥 公钥给前端,用来加密密码 私钥存储在服务端,用来解密密码 数据库加密存储 解密通过后,通...

  • 数据安全之RSA (对称加密)

    RAS下载地址公钥 密钥在此给出一对公钥和密钥 不用说了,先引入RSA 概念 加密和加签加密:公钥放在ios客户...

  • angularjs和vue实现RAS加密

    一. 什么是RSA?RSA算法是现今使用最广泛的公钥密码算法,也是号称地球上最安全的加密算法。在了解RSA算法之前...

  • RAS加密的数学原理

    概述 RSA算法是现今使用最广泛的公钥密码算法,也是号称地球上最安全的加密算法。在了解RSA算法之前,先熟悉下几个...

  • 加密

    1.MD5---单向-----哈希,散列函数2.AES---对称3.RAS---非对称安全性:使用非对称加密对称

网友评论

      本文标题:RAS加密

      本文链接:https://www.haomeiwen.com/subject/mpfsfdtx.html