java实现RSA非对称加密算法 对数据进行加密【附代码】
来源:互联网 发布:java订餐系统 编辑:程序博客网 时间:2024/06/06 16:54
今天看了一下RSA非对称加密算法 上网搜索了许多资料 都没有找到比较详细的讲解和实现代码
后来和朋友交流了下数据交互 突然发现了数据加密的重要性 ,决定自己来写一篇关于非对称加密的文章 附上代码 看看怎么一步一步的实现rsa加密的
1、需要引入的包
import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.util.Date;import java.util.concurrent.ExecutionException;import javax.crypto.Cipher;import javax.xml.crypto.Data;import sun.misc.BASE64Encoder;
2、编写Rsa类
public class Rsa {}
3、写入rsa算法需要的方法
public class Rsa { //生成密钥对 public static KeyPair genKeyPair(int keyLength) throws Exception{ KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); return keyPairGenerator.generateKeyPair(); } //公钥加密 public static byte[] encrypt(byte[] content, PrivateKey publicKey) throws Exception{ Cipher cipher=Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding" cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(content); } //私钥解密 public static byte[] decrypt(byte[] content, PublicKey privateKey) throws Exception{ Cipher cipher=Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(content); } }
4、方法有了之后就可以开始进行操作打了 首先要有个概念 公匙加密 私匙解密 或者 私匙加密 公匙解密
首先准备一个需要加密的数据
public static String data="{date:'hellow',time:'2017-10-29'}";
5、有了数据之后准备公钥秘钥
//公钥秘钥PublicKey publicKEY =keyPair.getPublic();PrivateKey privateKey =keyPair.getPrivate();
//公钥加密byte[] dataCode = encrypt(data.getBytes(),publicKEY);System.out.println(base64.encode(dataCode));
//秘钥解密byte[] dateprivate =decrypt(dataCode,privateKey);System.out.println(new String(dateprivate));
import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.util.Date;import java.util.concurrent.ExecutionException;import javax.crypto.Cipher;import javax.xml.crypto.Data;import sun.misc.BASE64Encoder;public class Rsa {public static String data="{date:'hellow',time:'2017-10-29'}"; public static void main(String[] args) throws Exception {//base64编码对象final BASE64Encoder base64 =new BASE64Encoder();KeyPair keyPair=genKeyPair(1024);//公钥秘钥PublicKey publicKEY =keyPair.getPublic();PrivateKey privateKey =keyPair.getPrivate();//公钥加密byte[] dataCode = encrypt(data.getBytes(),publicKEY);System.out.println(base64.encode(dataCode));System.out.println("");//秘钥解密byte[] dateprivate =decrypt(dataCode,privateKey);System.out.println(new String(dateprivate));}//生成密钥对 public static KeyPair genKeyPair(int keyLength) throws Exception{ KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); return keyPairGenerator.generateKeyPair(); } //公钥加密 public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception{ Cipher cipher=Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding" cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(content); } //私钥解密 public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception{ Cipher cipher=Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(content); } }
8、运行结果
阅读全文
0 0
- java实现RSA非对称加密算法 对数据进行加密【附代码】
- JAVA实现RSA加密,非对称加密算法
- 使用RSA加密算法对文件进行非对称加密
- JAVA实现RSA非对称加密算法
- RSA非对称加密算法Java实现
- Java加密技术——非对称加密算法RSA
- Java加密技术——非对称加密算法RSA
- Java加密技术(四)非对称加密算法RSA
- Java加密技术(四)非对称加密算法RSA
- JAVA加密&解密——非对称加密算法RSA
- Java加密技术篇(四)非对称加密算法RSA
- Java加密技术四——非对称加密算法RSA
- Java加密技术——非对称加密算法RSA
- JAVA 非对称加密算法RSA
- Java非对称加密算法--RSA
- Java非对称加密算法RSA
- 使用RSA算法对数据进行非对称加密和加密
- Java实现Rsa非对称加密
- 智能音箱、语音助手太火?或许你该了解下计算机视觉 | CSIG讲习班
- DOM概念
- adb远程调试apk
- 索尼PS4 Pro国行版发布:2999元,6月7日开售
- 在大数据的主场,李彦宏说“数据不是根本” | 数博会 2017
- java实现RSA非对称加密算法 对数据进行加密【附代码】
- uboot1.1.6顶层mkconfig详解
- Java的基本运算符
- css样式踩坑总结
- Angular 模态框 入坑记
- 跑在纯国产处理器上的麒麟云,有哪些不为人知的故事?
- IoT“永恒之蓝”来袭:路由器等智能硬件成重灾区
- Android之父Andy Rubin或在月底发布新手机
- Java运算符的实际运用