当前位置: 首页 > news >正文

b2c网站建设平台成都推广系统

b2c网站建设平台,成都推广系统,118论坛网址之家,网站制作行业越来越难做Java(110):非对称加密RSA的使用(KeyPair生成密钥) RSA 算法是一种非对称加解密算法。服务方生成一对 RSA 密钥,即公钥 私钥,将公钥提供给调用方,调用方使用公钥对数据进行加密后,服务方根据私钥进行解密。 1、RSA生…

Java(110):非对称加密RSA的使用(KeyPair生成密钥)

RSA 算法是一种非对称加解密算法。服务方生成一对 RSA 密钥,即公钥 + 私钥,将公钥提供给调用方,调用方使用公钥对数据进行加密后,服务方根据私钥进行解密。

1、RSA生成密钥方法

        keyPairGen.initialize(1024);//生成"密钥对"对象KeyPair keyPair = keyPairGen.generateKeyPair();//分别获取私钥和公钥对象RSAPrivateKey PrivateKey =(RSAPrivateKey) keyPair.getPrivate();RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();

2、RSA加密和解密方法

   /*** 公钥加密* @param publicKey 公钥* @param obj 明文* @return byte[] 密文*/public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) throws Exception {Cipher cipher =Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE,publicKey);//返回加密后的内容return cipher.doFinal(obj);}/*** 私钥解密* @param privateKey 公钥* @param obj 密文* @return byte[] 密文*/public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj)throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);//返回解密后的数组return cipher.doFinal(obj);}

3、Base64编码和解码

maven

<dependency><groupId>commons-codec</groupId><artifactId>commons-codec</artifactId><version>1.11</version>
</dependency>
    /*** 编码* @param txt byte字节数组* @return encode Base64编码*/public static byte[] encode(byte[] txt) {return org.apache.commons.codec.binary.Base64.encodeBase64(txt);}/*** 解码* @param txt 编码后的byte* @return decode Base64解码*/public static byte[] decode(String txt){return org.apache.commons.codec.binary.Base64.decodeBase64(txt);}

4、调用加解密

    public static void main(String[] args)throws Exception {//获取RSA算法的密钥生成器对象KeyPairGenerator keyPairGen =KeyPairGenerator.getInstance("RSA");//设定密钥长度为1024位keyPairGen.initialize(1024);//生成"密钥对"对象KeyPair keyPair = keyPairGen.generateKeyPair();//分别获取私钥和公钥对象RSAPrivateKey PrivateKey =(RSAPrivateKey) keyPair.getPrivate();RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();//执行加密和解密过程String InData="Hello World!";//得到要加密内容的数组byte[] byteInData =InData.getBytes("UTF-8");//用公钥加密byte[] cipherByte= encrypt(publicKey,byteInData);  //RSA加密String cipher=new String(encode(cipherByte));   //Base64a编码System.out.println("公钥加密,密文:"+cipher);//用私钥解密byte[] plain =decrypt(PrivateKey,decode(cipher)); //Base64a解码System.out.println("私钥解密,明文:"+new String(plain)); //RSA解密}

5、RSA加解密代码示例:

package jmj;import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;/*** Description :** @author : HMF* Date : Created in 20:32 2023/3/13* @version :*/
public class RSATest {public static void main(String[] args)throws Exception {//获取RSA算法的密钥生成器对象KeyPairGenerator keyPairGen =KeyPairGenerator.getInstance("RSA");//设定密钥长度为1024位keyPairGen.initialize(1024);//生成"密钥对"对象KeyPair keyPair = keyPairGen.generateKeyPair();//分别获取私钥和公钥对象RSAPrivateKey PrivateKey =(RSAPrivateKey) keyPair.getPrivate();RSAPublicKey publicKey =(RSAPublicKey) keyPair.getPublic();//执行加密和解密过程String InData="Hello World!";//得到要加密内容的数组byte[] byteInData =InData.getBytes("UTF-8");//用公钥加密byte[] cipherByte= encrypt(publicKey,byteInData);  //RSA加密String cipher=new String(encode(cipherByte));   //Base64a编码System.out.println("公钥加密,密文:"+cipher);//用私钥解密byte[] plain =decrypt(PrivateKey,decode(cipher)); //Base64a解码System.out.println("私钥解密,明文:"+new String(plain)); //RSA解密}/*** 编码* @param txt byte字节数组* @return encode Base64编码*/public static byte[] encode(byte[] txt) {return org.apache.commons.codec.binary.Base64.encodeBase64(txt);}/*** 解码* @param txt 编码后的byte* @return decode Base64解码*/public static byte[] decode(String txt){return org.apache.commons.codec.binary.Base64.decodeBase64(txt);}/*** 公钥加密* @param publicKey 公钥* @param obj 明文* @return byte[] 密文*/public static byte[] encrypt(RSAPublicKey publicKey, byte[] obj) throws Exception {Cipher cipher =Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE,publicKey);//返回加密后的内容return cipher.doFinal(obj);}/*** 私钥解密* @param privateKey 公钥* @param obj 密文* @return byte[] 密文*/public static byte[] decrypt(RSAPrivateKey privateKey, byte[] obj)throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, privateKey);//返回解密后的数组return cipher.doFinal(obj);}}

执行结果:

 

参考:https://blog.csdn.net/piaoranyuji/article/details/126140261

http://www.hkea.cn/news/161200/

相关文章:

  • 四川省住建厅官方网站3分钟搞定网站seo优化外链建设
  • 做网站阳泉巨量千川广告投放平台
  • 温岭哪里有做网站的如何自制网站
  • 知道创于 wordpress搜索引擎优化宝典
  • 乌兰县wap网站建设公司有效获客的六大渠道
  • 微信网站开发教程视频教程百度一下主页官网
  • 网站开发专业前景关键词挖掘排名
  • 网站开发属于什么职位类别seo查询站长工具
  • wordpress postmetaseoul national university
  • 商务网站的主要存在形式杭州百度快照优化公司
  • 个人备案网站做购物网站可以不班级优化大师免费下载电脑版
  • 贸易网站建设互联网广告代理加盟
  • 深圳网站建设网络公司河北关键词排名推广
  • 在工商网上怎么注册公司seo优化博客
  • 免费的小程序怎么赚钱历下区百度seo
  • 河北石家庄最新疫情最新消息优化防疫政策
  • 一站式做网站哪家强新闻小学生摘抄
  • 江西南昌网站建设公司哪家好谷歌google 官网下载
  • 公司网站用什么开发百度指数怎么用
  • 建站主机 wordpress济南网站万词优化
  • 哈尔滨app开发seo自学网官网
  • 网站答辩ppt怎么做全网关键词云在哪里看
  • 网站建设 视频seo关键词词库
  • 网站应用软件设计成都网站建设技术外包
  • 用哪个软件做网站网址查询域名解析
  • 网站安全优化域名停靠浏览器
  • 我做中医培训去哪个网站找学员谷歌排名算法
  • 如何将网站让百度收录网店培训班
  • wordpress旧版页面编辑界面百度seo推广计划类型包括
  • 网站建设茶店网网站换友链平台