卡密解密规范

PHP代码示例


  $orderNo = "2019090610433272234",//订单号
  $secret = "256CD47085308ED7009622CBC825EE87",//秘钥
  $encrypted = "XJC3eYuALfVHn++3lGo8D9eD01KqGULO+7NIo9mdImykg9vzTtINFjRU0zfFQI2G3rtBf8HC+C5D6DPcZBX5OJtAuT/2GJmK5sPkpMAt7fI=",//加密后的卡密字符串


  1, 计算key:
  $key = md5($orderNo . $secret); //得出:63e14a048bfbc360853736aa6b29b54d,
  $key = substr($key, 4, 8); //得出:4a048bfb,

  2, 使用加密后的卡密字符串和key进行DES解密
  $encrypted = base64_decode($encrypted);
  $openssl_options = "des-ede3-cbc";
  $decrypted = openssl_decrypt( $encrypted , $openssl_options, $key, OPENSSL_RAW_DATA);
  $result = rtrim( rtrim( $decrypted,chr(0) ), chr(7) );

  得出:"[{"number":"1234567","password":"abcppp","deadline":"2019-12-12 00:00:00"}]"

Java代码示例


  package com.iwankeji.word;

  import javax.crypto.Cipher;
  import javax.crypto.SecretKey;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;
  import java.math.BigInteger;
  import java.security.MessageDigest;
  import java.security.spec.AlgorithmParameterSpec;
  import java.util.Base64;

  public class Main {
        private static final String ENCODING = "UTF-8";
        private static final String ALGORITHM = "DES";
        private static final String CIPHER_TRANSFORMAT = "DES/CBC/PKCS5Padding";
        private static final byte[] DES_IV =  { (byte) 0x00, (byte) 0x00, (byte) 0x00,(byte) 0x00,(byte) 0x00, (byte) 0x00, (byte) 0x00,(byte) 0x00 };

        public static String decryptFromBase64(String base64, String key) throws Exception {
                Cipher c1 = Cipher.getInstance(CIPHER_TRANSFORMAT);
                SecretKey deskey = new SecretKeySpec(key.getBytes(), ALGORITHM);

                IvParameterSpec iv = new IvParameterSpec(DES_IV);
                AlgorithmParameterSpec paramSpec = iv;
                c1.init(Cipher.DECRYPT_MODE, deskey, paramSpec);
                byte[] result = c1.doFinal(Base64.getDecoder().decode(base64));
                return new String(result, ENCODING);
        }

        public static void main(String[] args) {
                // write your code here
                 try {
                    //待解密的字符串
                    String soureString ="XJC3eYuALfVHn++3lGo8D9eD01KqGULO+7NIo9mdImykg9vzTtINFjRU0zfFQI2G3rtBf8HC+C5D6DPcZBX5OJtAuT/2GJmK5sPkpMAt7fI=";
                    String orderNO = "2019090610433272234";  //订单号
                    String secret = "256CD47085308ED7009622CBC825EE87";  //密钥
                    // key 是 md5(订单号 + 密钥)后的字符串截取4-12位
                    String key = "";
                    MessageDigest md5 = MessageDigest.getInstance("MD5");
                    md5.update(keyString.getBytes());

                    byte b[] = md5.digest();
                    int i;
                    StringBuffer buf = new StringBuffer("");
                    
                    for (int offset = 0; offset < b.length; offset++) {
                        i = b[offset];
                        if (i < 0)
                            i += 256;
                        if (i < 16)
                            buf.append("0");
                        buf.append(Integer.toHexString(i));
                    }
                    key = buf.toString();
                    key = key.substring(4, 12);

                    String decodeString = decryptFromBase64(soureString, key);
                    System.out.printf("解密后的字符串:" + decodeString);

                    //解密后的字符串:[{"number":"1234567","password":"abcppp","deadline":"2019-12-12 00:00:00"}]
                
                } catch (Exception e) {
                    System.out.print(e);
                }
                
        }
}