久久久精品一区ed2k-女人被男人叉到高潮的视频-中文字幕乱码一区久久麻豆樱花-俄罗斯熟妇真实视频

Java中怎么使用RSA加密算法

Java中怎么使用RSA加密算法,針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

站在用戶的角度思考問題,與客戶深入溝通,找到山陽(yáng)網(wǎng)站設(shè)計(jì)與山陽(yáng)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋山陽(yáng)地區(qū)。

代碼如下

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
 
public class RSADecrypt {
  public static Map<String, String> createKeyPair() throws NoSuchAlgorithmException {
    //創(chuàng)建秘鑰對(duì)生成器
    KeyPairGenerator generator = KeyPairGenerator.getInstance("rsa");
    //初始化密鑰對(duì)生成器
    generator.initialize(1024);
    //生成密鑰對(duì)
    KeyPair keyPair = generator.generateKeyPair();
    //獲取公鑰私鑰
    RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
    String publicKeyStr = new String(publicKey.getEncoded());
    String privateKeyStr = new String(privateKey.getEncoded());
    //公鑰私鑰存放map
    Map<String, String> keyMap = new HashMap<>();
    keyMap.put("publicKey", publicKeyStr);
    keyMap.put("privateKey", privateKeyStr);
    return keyMap;
  }
 
  /**
   * 獲取公鑰
   * @param publicKey
   * @return
   */
  public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance("rsa");
    //X509編碼
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getBytes());
    RSAPublicKey rsaPublicKey = (RSAPublicKey) keyFactory.generatePublic(x509EncodedKeySpec);
    return rsaPublicKey;
  }
 
  /**
   * 獲取私鑰
   * @param privateKey
   * @return
   */
  public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    KeyFactory keyFactory = KeyFactory.getInstance("rsa");
    //PKCS#8編碼
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getBytes());
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8EncodedKeySpec);
    return rsaPrivateKey;
  }
 
  /**
   * 公鑰加密
   * @param src
   * @param publicKey
   * @return
   */
  public static byte[] publicEncrypt(String src, RSAPublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("rsa");
    cipher.init(Cipher.ENCRYPT_MODE,publicKey);
    byte[] encryptedData = cipher.doFinal(src.getBytes());
    return encryptedData;
  }
 
  /**
   * 私鑰解密
   * @param data
   * @param privateKey
   * @return
   * @throws NoSuchPaddingException
   * @throws NoSuchAlgorithmException
   * @throws InvalidKeyException
   * @throws BadPaddingException
   * @throws IllegalBlockSizeException
   */
  public static String privateDecrypt(byte[] data, RSAPrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("rsa");
    cipher.init(Cipher.DECRYPT_MODE,privateKey);
    byte[] result = cipher.doFinal(data);
    return new String(result);
  }
 
  public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
    Map<String,String> keyPair = createKeyPair();
    RSAPublicKey publicKey = getPublicKey(keyPair.get("publicKey"));
    RSAPrivateKey privateKey = getPrivateKey(keyPair.get("privateKey"));
    String str = "hello world";
    /**
     * 公鑰加密,私鑰解密
     */
    byte[] encryptedData = publicEncrypt(str,publicKey);
    String result = privateDecrypt(encryptedData, privateKey);
 
    /**
     * 私鑰加密,公鑰解密
     */
    byte[] encrypted = privateEncrypt(str, privateKey);
    String source = publicDecrypt(encrypted, publicKey);
 
  }
 
  /**
   * 私鑰加密
   * @param src
   * @param privateKey
   * @return
   * @throws NoSuchPaddingException
   * @throws NoSuchAlgorithmException
   * @throws InvalidKeyException
   * @throws BadPaddingException
   * @throws IllegalBlockSizeException
   */
  public static byte[] privateEncrypt(String src, RSAPrivateKey privateKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("rsa");
    cipher.init(Cipher.DECRYPT_MODE,privateKey);
    byte[] result = cipher.doFinal(src.getBytes());
    return result;
  }
 
  /**
   * 公鑰解密
   * @param data
   * @param publicKey
   * @return
   * @throws NoSuchPaddingException
   * @throws NoSuchAlgorithmException
   * @throws InvalidKeyException
   * @throws BadPaddingException
   * @throws IllegalBlockSizeException
   */
  public static String publicDecrypt(byte[] data, RSAPublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    Cipher cipher = Cipher.getInstance("rsa");
    cipher.init(Cipher.DECRYPT_MODE,publicKey);
    byte[] result = cipher.doFinal(data);
    return new String(result);
  }
}

上面例子使用密鑰長(zhǎng)度1024,如果想使用2048密鑰,只需要在初始化密鑰對(duì)生成器做一些變動(dòng),其他部分可以復(fù)用。

generator.initialize(2048);

關(guān)于Java中怎么使用RSA加密算法問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

網(wǎng)站標(biāo)題:Java中怎么使用RSA加密算法
分享URL:http://sd-ha.com/article36/jiijpg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序網(wǎng)站制作、Google、定制開發(fā)、虛擬主機(jī)定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)