亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Taobao ?? ??? ?? ?? / 電子憑證簽名加密和解密

電子憑證簽名加密和解密

背景:

1、基于安全考慮,電子憑證在getOrderCodes接口返回的code進行了簽名,但是碼商必須依賴這個接口比對自己庫中的碼,是否核銷,是否過期等來做下一步的業(yè)務(wù)決策,所以向碼商提供加密和解密的算法的文檔

demo:

public class TripleDes {
   private static final String Algorithm = "DESede/CBC/PKCS5Padding"; // 定義
   
   private static final String secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 碼商入駐給的密鑰,如果遺忘了,請聯(lián)系內(nèi)部當時碼商入駐接口人.
   
 
   /**
    * 加密
    * 
    * @param src
    *            需要加密的字符串
    * @param secret
    *            密鑰,24字節(jié)
    */
   public static String encrypt(String src, String secret) {
      if (StringUtils.isEmpty(src) || StringUtils.isEmpty(secret)
            || secret.length() != 24) {
         return src;
      }
 
      try {
         // 生成密鑰
         SecretKey deskey = new SecretKeySpec(secret.getBytes(), "DESede");
 
         // 加密
         Cipher c1 = Cipher.getInstance(Algorithm);
         IvParameterSpec iv = new IvParameterSpec(secret.substring(0, 8)
                .getBytes());
         c1.init(Cipher.ENCRYPT_MODE, deskey, iv);
         BASE64Encoder base64 = new BASE64Encoder();
         return base64.encode(c1.doFinal(src.getBytes()));
      } catch (java.security.NoSuchAlgorithmException e1) {
      } catch (javax.crypto.NoSuchPaddingException e2) {
      } catch (java.lang.Exception e3) {
      }
      return null;
   }
 
   /**
    * 解密
    * 
    * @param src
    *            需要解密的字符串
    * @param secret
    *            密鑰,24字節(jié)
    */
   public static String decrypt(String src, String secret) {
      if (StringUtils.isEmpty(src) || StringUtils.isEmpty(secret)
            || secret.length() != 24) {
         return src;
      }
 
      try {
         // 生成密鑰
         SecretKey deskey = new SecretKeySpec(secret.getBytes(), "DESede");
         Cipher c1 = Cipher.getInstance(Algorithm);
         IvParameterSpec iv = new IvParameterSpec(secret.substring(0, 8)
                .getBytes());
         c1.init(Cipher.DECRYPT_MODE, deskey, iv);
         BASE64Decoder base64 = new BASE64Decoder();
         return new String(c1.doFinal(base64.decodeBuffer(src)));
      } catch (java.security.NoSuchAlgorithmException e1) {
      } catch (javax.crypto.NoSuchPaddingException e2) {
      } catch (java.lang.Exception e3) {
      }
      return null;
   }
 
   public static void main(String[] args) {
      String code = "xxxxxxxxxxxxxxxxxxxxx";// 加密過的碼
      String newSecret = secret.substring(0, 24);//加密串取前24位,這個請務(wù)必注意.
      System.out.println(new String(TripleDes.decrypt(code, newSecret)));// 解密
      System.out.println(new String(TripleDes.encrypt(
            TripleDes.decrypt(code, newSecret), newSecret))); // 加密
 
   }
}


FAQ

  • 關(guān)于此文檔暫時還沒有FAQ