服务器之家

服务器之家 > 正文

Java实现RSA算法的方法详解

时间:2021-04-18 15:44     来源/作者:chengqiuming

本文实例讲述了java实现rsa算法的方法。分享给大家供大家参考,具体如下:

一 介绍

唯一广泛接受并实现
用于数据加密和数字签名
公钥加密、私钥解密
私钥加密、公钥解密

二 rsa参数说明

Java实现RSA算法的方法详解

三 实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.imooc.security.rsa;
import java.security.keyfactory;
import java.security.keypair;
import java.security.keypairgenerator;
import java.security.privatekey;
import java.security.publickey;
import java.security.interfaces.rsaprivatekey;
import java.security.interfaces.rsapublickey;
import java.security.spec.pkcs8encodedkeyspec;
import java.security.spec.x509encodedkeyspec;
import javax.crypto.cipher;
import org.apache.commons.codec.binary.base64;
public class imoocrsa {
  private static string src = "cakin24 security rsa";
  public static void main(string[] args) {
    jdkrsa();
  }
  public static void jdkrsa() {
    try {
      //1.初始化密钥
      keypairgenerator keypairgenerator = keypairgenerator.getinstance("rsa");
      keypairgenerator.initialize(512);
      keypair keypair = keypairgenerator.generatekeypair();
      rsapublickey rsapublickey = (rsapublickey)keypair.getpublic();
      rsaprivatekey rsaprivatekey = (rsaprivatekey)keypair.getprivate();
      system.out.println("public key : " + base64.encodebase64string(rsapublickey.getencoded()));
      system.out.println("private key : " + base64.encodebase64string(rsaprivatekey.getencoded()));
      //2.私钥加密、公钥解密——加密
      pkcs8encodedkeyspec pkcs8encodedkeyspec = new pkcs8encodedkeyspec(rsaprivatekey.getencoded());
      keyfactory keyfactory = keyfactory.getinstance("rsa");
      privatekey privatekey = keyfactory.generateprivate(pkcs8encodedkeyspec);
      cipher cipher = cipher.getinstance("rsa");
      cipher.init(cipher.encrypt_mode, privatekey);
      byte[] result = cipher.dofinal(src.getbytes());
      system.out.println("私钥加密、公钥解密——加密 : " + base64.encodebase64string(result));
      //3.私钥加密、公钥解密——解密
      x509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec(rsapublickey.getencoded());
      keyfactory = keyfactory.getinstance("rsa");
      publickey publickey = keyfactory.generatepublic(x509encodedkeyspec);
      cipher = cipher.getinstance("rsa");
      cipher.init(cipher.decrypt_mode, publickey);
      result = cipher.dofinal(result);
      system.out.println("私钥加密、公钥解密——解密:" + new string(result));
      //4.公钥加密、私钥解密——加密
      x509encodedkeyspec = new x509encodedkeyspec(rsapublickey.getencoded());
      keyfactory = keyfactory.getinstance("rsa");
      publickey = keyfactory.generatepublic(x509encodedkeyspec);
      cipher = cipher.getinstance("rsa");
      cipher.init(cipher.encrypt_mode, publickey);
      result = cipher.dofinal(src.getbytes());
      system.out.println("公钥加密、私钥解密——加密 : " + base64.encodebase64string(result));
      //5.公钥加密、私钥解密——解密
      pkcs8encodedkeyspec = new pkcs8encodedkeyspec(rsaprivatekey.getencoded());
      keyfactory = keyfactory.getinstance("rsa");
      privatekey = keyfactory.generateprivate(pkcs8encodedkeyspec);
      cipher = cipher.getinstance("rsa");
      cipher.init(cipher.decrypt_mode, privatekey);
      result = cipher.dofinal(result);
      system.out.println("公钥加密、私钥解密——解密:" + new string(result));
    } catch (exception e) {
      e.printstacktrace();
    }
  }
}

四 实现效果

public key : mfwwdqyjkozihvcnaqebbqadswawsajbajcbeob97idkkirbmx3moy5e4erwh0uvc2bcnly1rdo0lz8ibr1bl1rjxwkhv7u0aso/5dblnnngbqrtsjlscpmcaweaaq==
private key : miibvaibadanbgkqhkig9w0baqefaascat4wgge6ageaakealwf45v3sh2ssksgbhcw5jl7h5hchs68lyfw2vjwsojsvnyjthvuxveldaqe/ttqbi7/kmgweecztbg2wmwwi8widaqabakadskprsl+ew3s2n+cemizxfyyb0xhs1d84qapafpixkunvwl0a4ovrwsnwt4mejatwvtufnvtxizczdx+q5dbbaiea9tzzymgru+3mdlax0icf+niqwvlqyvedka4ksx55gvuciqcdoex6mqgrp78aqjykweogwliszju5fn/lfvkzrcgbjwihamvbblzzaykhy0ikw75kd/lksyouty+20bap+midrqgzaia6r36eerkzqubtcl8lxypb5f79htxd5dcvnib/zgp0uwigwtxi7ixhjycsnomsjdu1j3du9kqquw/eohxrk/oguye=
私钥加密、公钥解密——加密 : vjkfsoivelvkes5rprhsjk9tdtzohdells7yluidcyee7dkucm9srj8kwadynhi4m0olafjhk6447hp7ia8x7a==
私钥加密、公钥解密——解密:cakin24 security rsa
公钥加密、私钥解密——加密 : gawl73uxhto+efkkpmfmdhtk0vh7hb8n+30l1hp8aamiagd21h2x/q/ns+sogsoxnovunzasgzenmzzcjb4vea==
公钥加密、私钥解密——解密:cakin24 security rsa

五 应用场景

Java实现RSA算法的方法详解

希望本文所述对大家java程序设计有所帮助。

原文链接:https://blog.csdn.net/chengqiuming/article/details/78755050

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部