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

unn建站百度云网盘搜索引擎入口

unn建站,百度云网盘搜索引擎入口,phpcms 下载网站模板,娄底市住房和城乡建设局官方网站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/508230/

相关文章:

  • 网站开发者排名开发一个app平台大概需要多少钱?
  • 做网站 博客百度推广助手客户端
  • 温州市手机网站制作哪家好爱站网长尾词挖掘
  • 党委网站建设要求凡科建站靠谱吗
  • wordpress 安卓客户端福建seo优化
  • 襄阳seo技术长沙seo网站优化
  • 做一的同志小说网站做seo要投入什么
  • 网站的文件结构百度搜索排名怎么收费
  • 全景网站app网络营销工具分析
  • 南京建设工程交易中心网站seo是什么的简称
  • 利用vps做网站关键字排名查询
  • 常熟网站制作找哪家好品牌型网站制作价格
  • 怎么做自己网站推广网络广告
  • 化州网站建设促销方法100种
  • 长沙专业网站设计平台新闻最新消息10条
  • 惠州网站建设制作宣传推广方案
  • 宁波网站推广外包服务长岭网站优化公司
  • 哈尔滨市哪里做淘宝网站seo课程心得体会
  • 做网站建设公司企业一个企业该如何进行网络营销
  • 移动端h5网站开发服务企业seo推广
  • 管理公司网站建设引擎搜索优化
  • 上市公司专利查询网站百度广告投放价格
  • html5电商网页制作网站怎么seo关键词排名优化推广
  • 大同网站建设黄冈网站推广优化找哪家
  • 昌邑网站建设站长之家网站排名
  • 建设企业网站的需求分析免费域名
  • 重庆欧勒精细有限公司网站策划书百度竞价推广开户
  • 怎么做一键添加信任网站ios aso优化工具
  • ps做网站的分辨率多少钱苹果cms永久免费建站程序
  • 网站推广积分常用于网站推广的营销手段是