java RSA 加解密

來源:互聯網
上載者:User

轉自:http://java.chinaitlab.com/advance/798576_2.html

代碼未經測試。

 

import java.security.Key;

  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 sun.misc.BASE64Decoder;

  import sun.misc.BASE64Encoder;

  public class RSACoder {

  /**

  * 得到公開金鑰

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

  * @throws Exception

  */

  public static PublicKey getPublicKey(String key) throws Exception {

  byte[] keyBytes;

  keyBytes = (new BASE64Decoder()).decodeBuffer(key);

  X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

  KeyFactory keyFactory = KeyFactory.getInstance("RSA");

  PublicKey publicKey = keyFactory.generatePublic(keySpec);

  return publicKey;

  }

  /**

  * 得到私密金鑰

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

  * @throws Exception

  */

  public static PrivateKey getPrivateKey(String key) throws Exception {

  byte[] keyBytes;

  keyBytes = (new BASE64Decoder()).decodeBuffer(key);

  PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);

  KeyFactory keyFactory = KeyFactory.getInstance("RSA");

  PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

  return privateKey;

  }

/**

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

  * @return

  */

  public static String getKeyString(Key key) throws Exception {

  byte[] keyBytes = key.getEncoded();

  String s = (new BASE64Encoder()).encode(keyBytes);

  return s;

  }

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

  KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");

  //密鑰位元

  keyPairGen.initialize(1024);

  //金鑰組

  KeyPair keyPair = keyPairGen.generateKeyPair();

  // 公開金鑰

  PublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

  // 私密金鑰

  PrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

  String publicKeyString = getKeyString(publicKey);

  System.out.println("public:\n" + publicKeyString);

  String privateKeyString = getKeyString(privateKey);

  System.out.println("private:\n" + privateKeyString);

  //加解密類

  Cipher cipher = Cipher.getInstance("RSA");//Cipher.getInstance("RSA/ECB/PKCS1Padding");

  //明文

  byte[] plainText = "我們都很好!郵件:@sina.com".getBytes();

  //加密

  cipher.init(Cipher.ENCRYPT_MODE, publicKey);

  byte[] enBytes = cipher.doFinal(plainText);

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

  publicKey = getPublicKey(publicKeyString);

  privateKey = getPrivateKey(privateKeyString);

  //解密

  cipher.init(Cipher.DECRYPT_MODE, privateKey);

  byte[]deBytes = cipher.doFinal(enBytes);

  publicKeyString = getKeyString(publicKey);

  System.out.println("public:\n" +publicKeyString);

  privateKeyString = getKeyString(privateKey);

  System.out.println("private:\n" + privateKeyString);

  String s = new String(deBytes);

  System.out.println(s);

  }

  }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.