真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Java實(shí)現(xiàn)RSA算法的方法詳解-創(chuàng)新互聯(lián)

本文實(shí)例講述了Java實(shí)現(xiàn)RSA算法的方法。分享給大家供大家參考,具體如下:

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比丹徒網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式丹徒網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋丹徒地區(qū)。費(fèi)用合理售后完善,十余年實(shí)體公司更值得信賴。

一 介紹

唯一廣泛接受并實(shí)現(xiàn)
用于數(shù)據(jù)加密和數(shù)字簽名
公鑰加密、私鑰解密
私鑰加密、公鑰解密

二 RSA參數(shù)說(shuō)明

Java實(shí)現(xiàn)RSA算法的方法詳解

三 實(shí)現(xiàn)

package com.imooc.security.rsa;
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 ImoocRSA {
  private static String src = "cakin24 security rsa";
  public static void main(String[] args) {
    jdkRSA();
  }
  public static void jdkRSA() {
    try {
      //1.初始化密鑰
      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 : " + Base64.encodeBase64String(rsaPublicKey.getEncoded()));
      System.out.println("Private Key : " + 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(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(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();
    }
  }
}


本文名稱:Java實(shí)現(xiàn)RSA算法的方法詳解-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://weahome.cn/article/peojg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部