使用DES对称加密中cbc模式(key值和iv值一致)
加密内容=当前时间戳+#+信用卡号
密钥为appkey的后8位 例如:
CreditCardNO= des_encrypt(time(). '#240000000000386078', substr($appkey,-8));
测试加密方法 Xcrypt::encrypt( '12345#6789012345', '12345678') == 8e519cf90bf4240f7f653af ff4f6d658f5e402a4ff2581a7
以下是Java版信用卡加密算法:
public class HexUtil { public HexUtil() { } private static final String HEX_CHARS = "0123456789abcdef"; /** * Converts a byte array to hex string. * * @param bytes the input byte array * @return hex string representation of b. */ public static String toHexString(byte[] bytes) { StringBuilder buffer = new StringBuilder(); for (byte aByte : bytes) { buffer.append(HexUtil.HEX_CHARS.charAt(aByte >>> 4 & 0x0F)); buffer.append(HexUtil.HEX_CHARS.charAt(aByte & 0x0F)); } return buffer.toString(); } /** * Converts a hex string into a byte array. * * @param string string to be converted * @return byte array converted from s */ public static byte[] toByteArray(String string) { byte[] buffer = new byte[string.length() / 2]; int j = 0; for (int i = 0; i < buffer.length; i++) { buffer[i] = (byte) ((Character.digit(string.charAt(j++), 16) << 4) | Character.digit(string.charAt(j++), 16)); } return buffer; } public static String appendParam(String returnStr, String paramId, String paramValue) { if (!returnStr.equals("")) { if (!paramValue.equals("")) { returnStr = returnStr + "&" + paramId + "=" + paramValue; } } else { if (!paramValue.equals("")) { returnStr = paramId + "=" + paramValue; } } return returnStr; } }
public class CipherUtil { public static final String CHARSET = "UTF-8"; public static final String KEY_DES = "DES"; static final String CIPHER_DES = "DES/CBC/PKCS5Padding"; private static final String HEX_CHARS = "0123456789abcdef"; /** * DES对称加密 * * @param content * @param password 对称加密的key * @return * @throws Exception */ public static String desEncrypt(String content,String password) throws Exception{ SecretKeySpec secretKey=new SecretKeySpec(password.getBytes(CHARSET), KEY_DES); Cipher cipher=Cipher.getInstance(CIPHER_DES); byte[] byteContent=content.getBytes(CHARSET); IvParameterSpec iv=new IvParameterSpec(password.getBytes(CHARSET)); cipher.init(Cipher.ENCRYPT_MODE, secretKey,iv); byte[] result=cipher.doFinal(byteContent); return toHexString(result); } public static String toHexString(byte[] bytes) { StringBuilder buffer = new StringBuilder(); for (byte aByte : bytes) { buffer.append(HEX_CHARS.charAt(aByte >>> 4 & 0x0F)); buffer.append(HEX_CHARS.charAt(aByte & 0x0F)); } return buffer.toString(); } /** * DES对称解密 * * @param content * @param password 对称加密的key * @return * @throws Exception */ public static String desDecrypt(String content, String password) throws Exception { byte[] bytes = HexUtil.toByteArray(content); Cipher cipher = Cipher.getInstance(CIPHER_DES); DESKeySpec desKeySpec = new DESKeySpec(password.getBytes(CHARSET)); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_DES); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(password.getBytes(CHARSET)); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytes); return new String(retByte); }
public static void main(String[] args) throws Exception { //6e70202d8683a94a51910ce527abea29aa40d01f47f6e136 //String Number = "6e70202d8683a94a51910ce527abea29aa40d01f47f6e136"; String timeStamp=String.valueOf(new Date().getTime()/1000); //String Number=timeStamp+"#4336660000000000"; String Number=timeStamp+"#4336660000000000"; String password = "78d6a4ee"; // System.out.println("Number 加密前:" + Number); // //DES加密 Number = CipherUtil.desEncrypt(Number, password); System.out.println("Number DES加密后:" + Number); // System.out.println(result.length()); //DES加密 Number = CipherUtil.desDecrypt(Number, password); System.out.println("Number DES解密后:" + Number); String year=timeStamp+"#2016"; year = CipherUtil.desEncrypt(year, password); System.out.println("ExpirationYear DES加密后:" + year); year = CipherUtil.desDecrypt(year, password); System.out.println("ExpirationYear DES解密后:" + year); String month=timeStamp+"#6"; month = CipherUtil.desEncrypt(month, password); System.out.println("ExpirationMonth DES加密后:" + month); month = CipherUtil.desDecrypt(month, password); System.out.println("ExpirationMonth DES解密后:" + month); String CVV=timeStamp+"#471"; CVV = CipherUtil.desEncrypt(CVV, password); System.out.println("CVV DES加密后:" + CVV); CVV = CipherUtil.desDecrypt(CVV, password); System.out.println("CVV DES解密后:" + CVV); String HolderName=timeStamp+"#James White"; HolderName = CipherUtil.desEncrypt(HolderName, password); System.out.println("HolderName DES加密后:" + HolderName); HolderName = CipherUtil.desDecrypt(HolderName, password); System.out.println("HolderName DES解密后:" + HolderName); String IdType=timeStamp+"#2"; IdType = CipherUtil.desEncrypt(IdType, password); System.out.println("IdType DES加密后:" + IdType); IdType = CipherUtil.desDecrypt(IdType, password); System.out.println("IdType DES解密后:" + IdType); String IdNo=timeStamp+"#4033910000000000"; IdNo = CipherUtil.desEncrypt(IdNo, password); System.out.println("IdNo DES加密后:" + IdNo); IdNo = CipherUtil.desDecrypt(IdNo, password); System.out.println("IdNo DES解密后:" + IdNo); // String number0="8c8d2569b1600c5cbd09668ab1b514feeda776b383c07697602862d9cd048f18"; // String key="78d6a4ee"; // System.out.println(CipherUtil.desDecrypt(number0, key)); }