熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java RSA 加解密

2022-06-13   來源: Java核心技術 

  import javasecurityKey;

  import javasecurityKeyFactory;

  import javasecurityKeyPair;

  import javasecurityKeyPairGenerator;

  import javasecurityPrivateKey;

  import javasecurityPublicKey;

  import javasecurityinterfacesRSAPrivateKey;

  import javasecurityinterfacesRSAPublicKey;

  import javasecurityspecPKCSEncodedKeySpec;

  import javasecurityspecXEncodedKeySpec;

  import javaxcryptoCipher;

  import sunmiscBASEDecoder;

  import sunmiscBASEEncoder;

  public class RSACoder {

  /**

  * 得到公鑰

  * @param key 密鑰字符串(經過base編碼)

  * @throws Exception

  */

  public static PublicKey getPublicKey(String key) throws Exception {

  byte[] keyBytes;

  keyBytes = (new BASEDecoder())decodeBuffer(key);

  XEncodedKeySpec keySpec = new XEncodedKeySpec(keyBytes);

  KeyFactory keyFactory = KeyFactorygetInstance(RSA);

  PublicKey publicKey = keyFactorygeneratePublic(keySpec);

  return publicKey;

  }

  /**

  * 得到私鑰

  * @param key 密鑰字符串(經過base編碼)

  * @throws Exception

  */

  public static PrivateKey getPrivateKey(String key) throws Exception {

  byte[] keyBytes;

  keyBytes = (new BASEDecoder())decodeBuffer(key);

  PKCSEncodedKeySpec keySpec = new PKCSEncodedKeySpec(keyBytes);

  KeyFactory keyFactory = KeyFactorygetInstance(RSA);

  PrivateKey privateKey = keyFactorygeneratePrivate(keySpec);

  return privateKey;

  }

  /**

  * 得到密鑰字符串(經過base編碼)

  * @return

  */

  public static String getKeyString(Key key) throws Exception {

  byte[] keyBytes = keygetEncoded();

  String s = (new BASEEncoder())encode(keyBytes);

  return s;

  }

  public static void main(String[] args) throws Exception {

  KeyPairGenerator keyPairGen = KeyPairGeneratorgetInstance(RSA);

  //密鑰位數

  keyPairGeninitialize();

  //密鑰對

  KeyPair keyPair = keyPairGengenerateKeyPair();

  // 公鑰

  PublicKey publicKey = (RSAPublicKey) keyPairgetPublic();

  // 私鑰

  PrivateKey privateKey = (RSAPrivateKey) keyPairgetPrivate();

  String publicKeyString = getKeyString(publicKey);

  Systemoutprintln(public:\n + publicKeyString);

  String privateKeyString = getKeyString(privateKey);

  Systemoutprintln(private:\n + privateKeyString);

  //加解密類

  Cipher cipher = CiphergetInstance(RSA);//CiphergetInstance(RSA/ECB/PKCSPadding);

  //明文

  byte[] plainText = 我們都很好!郵件getBytes();

  //加密

  cipherinit(CipherENCRYPT_MODE publicKey);

  byte[] enBytes = cipherdoFinal(plainText);

  //通過密鑰字符串得到密鑰

  publicKey = getPublicKey(publicKeyString);

  privateKey = getPrivateKey(privateKeyString);

  //解密

  cipherinit(CipherDECRYPT_MODE privateKey);

  byte[]deBytes = cipherdoFinal(enBytes);

  publicKeyString = getKeyString(publicKey);

  Systemoutprintln(public:\n +publicKeyString);

  privateKeyString = getKeyString(privateKey);

  Systemoutprintln(private:\n + privateKeyString);

  String s = new String(deBytes);

  Systemoutprintln(s);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25516.html
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.