欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java 中RSA的方式实现非对称加密的实例

程序员文章站 2023-12-21 13:57:58
java 中rsa的方式实现非对称加密的实例 rsa通俗理解: 你只要去想:既然是加密,那肯定是不希望别人知道我的消息,所以只有我才能解密,所以可得出公钥负责加密,私钥...

java 中rsa的方式实现非对称加密的实例

rsa通俗理解:

你只要去想:既然是加密,那肯定是不希望别人知道我的消息,所以只有我才能解密,所以可得出公钥负责加密,私钥负责解密;同理,既然是签名,那肯定是不希望有人冒充我发消息,只有我才能发布这个签名,所以可得出私钥负责签名,公钥负责验证。

实现代码:

package com.sahadev; 
 
import java.security.keyfactory; 
import java.security.keypair; 
import java.security.keypairgenerator; 
import java.security.privatekey; 
import java.security.publickey; 
import java.security.interfaces.rsaprivatekey; 
import java.security.interfaces.rsapublickey; 
import java.security.spec.pkcs8encodedkeyspec; 
import java.security.spec.x509encodedkeyspec; 
 
import javax.crypto.cipher; 
 
import org.apache.commons.codec.binary.base64; 
 
public class rsa { 
 
  public static void main(string[] args) { 
    jdkrsa(); 
  } 
 
  public static void jdkrsa() { 
    try { 
      // 构建密钥对儿 
      keypairgenerator keypairgenerator = keypairgenerator.getinstance("rsa"); 
      keypairgenerator.initialize(512); 
      keypair keypair = keypairgenerator.generatekeypair(); 
      rsapublickey rsapublickey = (rsapublickey) keypair.getpublic(); 
      rsaprivatekey rsaprivatekey = (rsaprivatekey) keypair.getprivate(); 
      system.out.println("public key : " + org.apache.commons.codec.binary.base64.encodebase64string(rsapublickey.getencoded())); 
      system.out.println("private key : " + org.apache.commons.codec.binary.base64.encodebase64string(rsaprivatekey.getencoded())); 
 
      // 2.私钥加密,公钥解密---加密 
      pkcs8encodedkeyspec pkcs8encodedkeyspec = new pkcs8encodedkeyspec(rsaprivatekey.getencoded()); 
      keyfactory keyfactory = keyfactory.getinstance("rsa"); 
      privatekey privatekey = keyfactory.generateprivate(pkcs8encodedkeyspec); 
      cipher cipher = cipher.getinstance("rsa"); 
      cipher.init(cipher.encrypt_mode, privatekey); 
      byte[] result = cipher.dofinal(dh.src.getbytes()); 
      system.out.println("私钥加密,公钥解密------加密 : " + base64.encodebase64string(result)); 
 
      // 3.私钥加密,公钥解密---解密 
      x509encodedkeyspec x509encodedkeyspec = new x509encodedkeyspec(rsapublickey.getencoded()); 
      keyfactory = keyfactory.getinstance("rsa"); 
      publickey publickey = keyfactory.generatepublic(x509encodedkeyspec); 
      cipher = cipher.getinstance("rsa"); 
      cipher.init(cipher.decrypt_mode, publickey); 
      result = cipher.dofinal(result); 
      system.out.println("私钥加密,公钥解密------解密 : " + new string(result)); 
 
      // 4.公钥加密,私钥解密---加密 
      x509encodedkeyspec = new x509encodedkeyspec(rsapublickey.getencoded()); 
      keyfactory = keyfactory.getinstance("rsa"); 
      publickey = keyfactory.generatepublic(x509encodedkeyspec); 
      cipher = cipher.getinstance("rsa"); 
      cipher.init(cipher.encrypt_mode, publickey); 
      result = cipher.dofinal(dh.src.getbytes()); 
 
      system.out.println("公钥加密,私钥解密------加密 : " + base64.encodebase64string(result)); 
 
      // 5.公钥加密,私钥解密---解密 
      pkcs8encodedkeyspec = new pkcs8encodedkeyspec(rsaprivatekey.getencoded()); 
      keyfactory = keyfactory.getinstance("rsa"); 
      privatekey = keyfactory.generateprivate(pkcs8encodedkeyspec); 
      cipher = cipher.getinstance("rsa"); 
      cipher.init(cipher.decrypt_mode, privatekey); 
      result = cipher.dofinal(result); 
      system.out.println("公钥加密,私钥解密------解密 : " + new string(result)); 
    } catch (exception e) { 
      e.printstacktrace(); 
    } 
  } 
} 

附上输出结果: 

public key : mfwwdqyjkozihvcnaqebbqadswawsajbajcfshop5xm94r2atbe++amzmzjtxhxdpqmmq9al1cethr2xtac+eckv32kzb4r8c/6vobnbfd8swx9fu5n2y6ccaweaaq== 
private key : miibvaibadanbgkqhkig9w0baqefaascat4wgge6ageaakealwwygg/ngb3ivzpnt775oxkzmm1cfeomqyxd1ovvws2gvze1pz4rys/fyrnvhhwl/q85s0f93yxbh0vtk3blpwidaqabakbdaa7rabug6irgykztyfcmjxaud5asl358jnoafuctmo4skfxh1vr4enfue0lt0zj3ckvh0o9uqdng0r4jeograiea3kuxps9og0axuxlggg0e7b1os5c3gnrh2fmqapjtpy0ciqcvm4epfdrjjnqim0atvbt5jznw3pru33pdyiswuomhawigdolmsuzjxj2y5si2cyigoymw/g9f99tovlbvzhy8y+ucif94mvu/uqdgnljsmv3tu4cpklmxrzq9lhduu1hpujcbaieay/0os2q9ebfmpwhsgxf4wmxj4wg0xznfwxsezb0qxzk= 
私钥加密,公钥解密------加密 : nxf87irbemk3x/fw7iwy4gua8ap4rvj4djsp2cjjsoo5x/schhkdvytlm+9touavmeukwk5wh70g53q7+e73zg== 
私钥加密,公钥解密------解密 : hello,sahadev! 
公钥加密,私钥解密------加密 : hjl8928jcbntvcrhkayaxqnjmiiaktbskhff2i01pbahtfgh6gjtcp1i345/3i9ozwovfxvi/oxgvjsisg7moq== 
公钥加密,私钥解密------解密 : hello,sahadev! 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: