Java实现RSA加密算法

栏目: 编程工具 · 发布时间: 7年前

内容简介:首先创建然后初始化密钥对的长度,最低长度512位,并且长度不能低于明文的长度利用

生成密钥对

首先创建 KeyPairGenerator 类的对象,用于生成公钥和私钥对

// 生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");

然后初始化密钥对的长度,最低长度512位,并且长度不能低于明文的长度

// 初始化密钥大小为1024位
keyPairGen.initialize(1024);

利用 keyPairGen 对象生成密钥对,保存在 KeyPair 类中

KeyPair keyPair = keyPairGen.generateKeyPair();

keyPair 类中获取公钥和私钥

RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

加密(encrypt)

protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) {
    if (publicKey != null) {
        try {
            // Cipher负责完成加密或解密工作,基于RSA
            Cipher cipher = Cipher.getInstance(ALGORITHM);

            // 根据公钥,对Cipher对象进行初始化
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            // 加密,结果保存进resultBytes,并返回
            byte[] resultBytes = cipher.doFinal(srcBytes);
            return resultBytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

解密(decrypt)

protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) {
    if (publicKey != null) {
        try {
            // Cipher负责完成加密或解密工作,基于RSA
            Cipher cipher = Cipher.getInstance("RSA");

            // 根据公钥,对Cipher对象进行初始化
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);

            // 加密,结果保存进resultBytes,并返回
            byte[] resultBytes = cipher.doFinal(srcBytes);
            return resultBytes;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return null;
}

完整代码

import java.security.*;
import java.security.interfaces.*;
import javax.crypto.*;

public class RSA {

    private RSAPublicKey publicKey;
    private RSAPrivateKey privateKey;
    private byte[] resultBytes;
    private final static String ALGORITHM = "RSA";

    public RSA() {
        try {
            String message = "你好,我很喜欢加密算法";
            System.out.println("明文是:" + message);
            // 生成公钥和私钥对,基于RSA算法生成对象
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM);

            // 初始化密钥对生成器,密钥大小为1024位
            keyPairGen.initialize(1024);

            // 生成一个密钥对,保存在keyPair中
            KeyPair keyPair = keyPairGen.generateKeyPair();

            // 得到公钥和私钥
            publicKey = (RSAPublicKey) keyPair.getPublic();
            privateKey = (RSAPrivateKey) keyPair.getPrivate();

            // 用公钥加密
            byte[] srcBytes = message.getBytes();
            resultBytes = RSA.encrypt(publicKey, srcBytes);
            String result = new String(resultBytes);
            System.out.println("用公钥加密后密文是:" + result);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) {
        if (publicKey != null) {
            try {
                // Cipher负责完成加密或解密工作,基于RSA
                Cipher cipher = Cipher.getInstance(ALGORITHM);

                // 根据公钥,对Cipher对象进行初始化
                cipher.init(Cipher.ENCRYPT_MODE, publicKey);

                // 加密,结果保存进resultBytes,并返回
                byte[] resultBytes = cipher.doFinal(srcBytes);
                return resultBytes;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    protected byte[] decrypt(RSAPrivateKey privateKey, byte[] encBytes) {
        if (privateKey != null) {
            try {
                Cipher cipher = Cipher.getInstance(ALGORITHM);

                // 根据私钥对Cipher对象进行初始化
                cipher.init(Cipher.DECRYPT_MODE, privateKey);

                // 解密并将结果保存进resultBytes
                byte[] decBytes = cipher.doFinal(encBytes);
                return decBytes;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        long rsaStart = System.currentTimeMillis();
        RSA rsa = new RSA();
        byte[] decBytes = rsa.decrypt(rsa.privateKey, rsa.resultBytes);
        String dec = new String(decBytes);
        long rsaEnd = System.currentTimeMillis();
        System.out.println("用私钥解密后的结果是:" + dec);
        System.out.println("共计用时:" + (rsaEnd - rsaStart));
    }
}

运行结果见下面的GIF:

Java实现RSA加密算法

以上所述就是小编给大家介绍的《Java实现RSA加密算法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

超简单!一学就懂的互联网金融

超简单!一学就懂的互联网金融

视觉图文 / 人民邮电出版社 / 2015-2-1 / 45.00元

零基础、全图解,通过130多个精辟的知识点、220多张通俗易懂的逻辑图表,让您一书在手,即可彻底看懂、玩转互联网金融从菜鸟成为达人,从新手成为互联网金融高手! 本书主要特色:最简洁的版式+最直观的图解+最实用的内容。 本书细节特色:10章专题内容详解+80多个特别提醒奉献+130多个知识点讲解+220多张图片全程图解,深度剖析互联网金融的精华之处,帮助读者在最短的时间内掌握互联网金融知......一起来看看 《超简单!一学就懂的互联网金融》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

SHA 加密
SHA 加密

SHA 加密工具