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();


6、有了公钥秘钥之后就可以进行加密解密了
//公钥加密byte[] dataCode = encrypt(data.getBytes(),publicKEY);System.out.println(base64.encode(dataCode));


//秘钥解密byte[] dateprivate =decrypt(dataCode,privateKey);System.out.println(new String(dateprivate));


7、附上完整代码

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、运行结果