Java實現 數字簽名

jopen 10年前發布 | 36K 次閱讀 數字簽名 Java開發

  1.   生成 公鑰 和私鑰 對 

    </li> </ol>
    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;  } }</pre>

    2. 加密過程(數字簽名):利用私鑰對傳輸數據進行數據加密

    public class Signaturer {
     /*
       
       Description:數字簽名
       
       @param priKeyText
       @param plainText
       @return  
      /
     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;  } }</pre>

    3. 校驗數字簽名, 利用公鑰和數據明文進行編碼校驗 ”數據字簽名“的數據 Check。

    public class SignProvider {
     private SignProvider() {

     }

     /*        Description:校驗數字簽名,此方法不會拋出任務異常,成功返回true,失敗返回false,要求全部參數不能為空        @param pubKeyText               公鑰,base64編碼    @param plainText               明文    @param signTest               數字簽名的密文,base64編碼    @return 校驗成功返回true 失敗返回false     /  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;   }  } }</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!