栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

非对称加密 RSA -- java版

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

非对称加密 RSA -- java版

概念

RSA是由罗纳德·李维斯特(Ron Rivest)、阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)在1977年一起提出的。当时他们三人都在麻省理工学院工作。RSA 就是他们三人姓氏开头字母拼在一起组成的。

对极大整数做因数分解的难度决定了 RSA 算法的可靠性

java案例
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;


public class RsaUtils {

    private static final String ALGORITHM = "RSA";

    public static KeyPair generateKey() throws NoSuchAlgorithmException {
        return generateKey(1024);
    }

    
    public static KeyPair generateKey(int keysize) throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
        keyPairGenerator.initialize(keysize); // 512 1024 2048 4096 8192 16384 32768 65536
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        return keyPair;
    }

    private static PublicKey getPublicKey(byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException {
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(key);
        PublicKey publicKey = KeyFactory.getInstance(ALGORITHM).generatePublic(x509EncodedKeySpec);
        return publicKey;
    }

    private static PrivateKey getPrivateKey(byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException {
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);
        PrivateKey privateKey = KeyFactory.getInstance(ALGORITHM).generatePrivate(pkcs8EncodedKeySpec);
        return privateKey;
    }

    
    public static byte[] encrypt(boolean isPrivate, byte[] key, byte[] data) throws Exception {
        Key thisKey = isPrivate ? getPrivateKey(key) : getPublicKey(key);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, thisKey);
        byte[] bytes = cipher.doFinal(data);
        return bytes;
    }

    
    public static byte[] decrypt(boolean isPrivate, byte[] key, byte[] data) throws Exception {
        Key thisKey = isPrivate ? getPrivateKey(key) : getPublicKey(key);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, thisKey);
        byte[] bytes = cipher.doFinal(data);
        return bytes;
    }
}

测试

import org.apache.commons.codec.binary.base64;

import java.security.KeyPair;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public class RsaUtilsTest {

    public static void main(String[] args) throws Exception {
        KeyPair keyPair = RsaUtils.generateKey();
        RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
        RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
        byte[] publicKey = rsaPublicKey.getEncoded();
        System.out.println("公钥:n" + base64.encodebase64String(publicKey));
        byte[] privateKey = rsaPrivateKey.getEncoded();
        System.out.println("私钥:n" + base64.encodebase64String(privateKey));

        //test1(publicKey, privateKey, "你好世界");
        test2(publicKey, privateKey, "你好祖国");
    }

    private static void test1(byte[] publicKey, byte[] privateKey, String text) throws Exception {
        // 公钥加密 私钥解密---加密
        byte[] data = RsaUtils.encrypt(false, publicKey, text.getBytes());
        System.out.println("密文:n" + base64.encodebase64String(data));
        // 公钥加密 私钥解密---解密
        byte[] result = RsaUtils.decrypt(true, privateKey, data);
        System.out.println("解密后:n" + new String(result));
    }

    private static void test2(byte[] publicKey, byte[] privateKey, String text) throws Exception {
        // 私钥加密 公钥解密---加密
        byte[] data = RsaUtils.encrypt(true, privateKey, text.getBytes());
        System.out.println("密文:n" + base64.encodebase64String(data));
        // 私钥加密 公钥解密---解密
        byte[] result = RsaUtils.decrypt(false, publicKey, data);
        System.out.println("解密后:n" + new String(result));
    }
}

gitee-code

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号