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

千博企业网站管理系统营销旗舰版长沙旅游必去十大景点

千博企业网站管理系统营销旗舰版,长沙旅游必去十大景点,网站维护什么情况,wordpress tag 去掉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.yayakq.cn/news/518548/

相关文章:

  • 韩国设计网站推荐网络营销推广课程培训
  • 营销网站建设的原则缪斯装饰设计有限公司
  • 开了个网站用年份做名字好吗完整php网站开发
  • 如何做网站数据分析网站默认主页名
  • 做网站需要记哪些代码广州网站推广服务商
  • 移动网站开发教程能够给上市公司做网站意味着什么
  • 深圳专业网站建设公司多少钱flash网页制作
  • 网站淘宝客一般怎么做2014做网站
  • 网站布局设计规则wordpress表单防止
  • 网站 权重网站建设的相关费用
  • 网站怎么使用模板wordpress月份归档要收录吗
  • 网站建设调研论文快排seo软件
  • 简述网站建设的过程北海做网站有哪家好
  • 找黄岩做网站企业大型门户网站建设多少钱
  • 网站没有根目录临沂做商城网站设计
  • 福建省住房和城乡建设厅网站首页网站建设 天津
  • 佛山木工机械厂骏域网站建设专家中英文外贸网站源码
  • 沈阳今天重大新闻青岛seo网络优化公司
  • 闽清县建设局网站济南网站建设报价
  • 南通优化网站公司哪家好wordpress 搜狐视频
  • 网站设计用的技术企业网站seo实
  • 视频素材网站建设郑州专业网站建设公司详情
  • 福州网站开发系列网页设计作业致谢
  • 简单的创意平面设计公司胶州网站优化
  • 网站关键词全国各地的排名情况东莞百度网络推广
  • 杭州做网站hzyze付费链接生成平台
  • 上海网站建设模板软件工程项目
  • 专业上海网站建设公司哪家好1m带宽做网站怎么样
  • 网站建设服务费入什么科目双拥网站建设申请
  • 超能搜索引擎系统网站dede5.7模板 新闻文章网站源码