java中的使用RSA演算法進行公鑰加密私鑰解密 .

weixin_34391854發表於2013-01-31

 一個比較簡單的實現:一個三個類KeyGenerater生成公鑰私鑰對,Signaturer類使用私鑰簽名,SignProvider用公鑰驗證。公鑰和私鑰使用Base64加密Base64這個類也在部落格裡面

public class KeyGenerater {
 private byte[] priKey;
 private byte[] pubKey;

 public void generater() {
  try {
   java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator
     .getInstance("RSA");
   SecureRandom secrand = new SecureRandom();
   secrand.setSeed("syj".getBytes()); // 初始化隨機產生器
   keygen.initialize(1024, secrand);
   KeyPair keys = keygen.genKeyPair();

   PublicKey pubkey = keys.getPublic();
   PrivateKey prikey = keys.getPrivate();

   pubKey = Base64.encodeToByte(pubkey.getEncoded());
   priKey = Base64.encodeToByte(prikey.getEncoded());

   System.out.println("pubKey = " + new String(pubKey));
   System.out.println("priKey = " + new String(priKey));
  } catch (java.lang.Exception e) {
   System.out.println("生成金鑰對失敗");
   e.printStackTrace();
  }
 }

 public byte[] getPriKey() {
  return priKey;
 }

 public byte[] getPubKey() {
  return pubKey;
 }
}

 

public class Signaturer {
 /**
  *
  * Description:數字簽名
  *
  * @param priKeyText
  * @param plainText
  * @return
  * @author 孫鈺佳
  * @since:2007-12-27 上午10:51:48
  */
 public static byte[] sign(byte[] priKeyText, String plainText) {
  try {
   PKCS8EncodedKeySpec priPKCS8 = new PKCS8EncodedKeySpec(Base64
     .decode(priKeyText));
   KeyFactory keyf = KeyFactory.getInstance("RSA");
   PrivateKey prikey = keyf.generatePrivate(priPKCS8);

   // 用私鑰對資訊生成數字簽名
   java.security.Signature signet = java.security.Signature
     .getInstance("MD5withRSA");
   signet.initSign(prikey);
   signet.update(plainText.getBytes());
   byte[] signed = Base64.encodeToByte(signet.sign());
   return signed;
  } catch (java.lang.Exception e) {
   System.out.println("簽名失敗");
   e.printStackTrace();
  }
  return null;
 }
}

public class SignProvider {
 private SignProvider() {

 }

 /**
  *
  * Description:校驗數字簽名,此方法不會丟擲任務異常,成功返回true,失敗返回false,要求全部引數不能為空
  *
  * @param pubKeyText
  *            公鑰,base64編碼
  * @param plainText
  *            明文
  * @param signTest
  *            數字簽名的密文,base64編碼
  * @return 校驗成功返回true 失敗返回false
  * @author 孫鈺佳
  * @since:2007-12-27 上午09:33:55
  */
 public static boolean verify(byte[] pubKeyText, String plainText,
   byte[] signText) {
  try {
   // 解密由base64編碼的公鑰,並構造X509EncodedKeySpec物件
   java.security.spec.X509EncodedKeySpec bobPubKeySpec = new java.security.spec.X509EncodedKeySpec(
     Base64.decode(pubKeyText));
   // RSA對稱加密演算法
   java.security.KeyFactory keyFactory = java.security.KeyFactory
     .getInstance("RSA");
   // 取公鑰匙物件
   java.security.PublicKey pubKey = keyFactory
     .generatePublic(bobPubKeySpec);
   // 解密由base64編碼的數字簽名
   byte[] signed = Base64.decode(signText);
   java.security.Signature signatureChecker = java.security.Signature
     .getInstance("MD5withRSA");
   signatureChecker.initVerify(pubKey);
   signatureChecker.update(plainText.getBytes());
   // 驗證簽名是否正常
   if (signatureChecker.verify(signed))
    return true;
   else
    return false;
  } catch (Throwable e) {
   System.out.println("校驗簽名失敗");
   e.printStackTrace();
   return false;
  }
 }
}

相關文章