信用卡如何加密
使用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)); }
Js版信用卡加密算法:
const crypto = require('crypto'); // DES 加密 function desEncrypt (message, key) { key = key.length >= 8 ? key.slice(0, 8) : key.concat('0'.repeat(8 - key.length)) const keyHex = new Buffer(key) const cipher = crypto.createCipheriv('des-cbc', keyHex, keyHex) cipher.setAutoPadding(true) let c = cipher.update(message, 'utf8', 'hex') c += cipher.final('hex') return c } module.exports = { desEncrypt }
C# 版信用卡加密算法:
using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common.Util.Tests { [TestClass()] public class EncryptHelperTests { [TestMethod()] public void ElongDesCBCDecryptorTest() { string input = "8e519cf90bf4240f7f653afff4f6d658f5e402a4ff2581a7"; string keyIvData = "12345678"; var result = DESHelper.DesCBCDecryptor(input, keyIvData, keyIvData); Assert.IsNotNull(result); Assert.AreEqual(result, "12345#6789012345"); } [TestMethod()] public void ElongDesCBCEncryptorTest() { string input = "12345#6789012345"; string keyIvData = "12345678"; var result = DESHelper.DesCBCEncryptor(input, keyIvData, keyIvData); Assert.IsNotNull(result); Assert.AreEqual(result, "8e519cf90bf4240f7f653afff4f6d658f5e402a4ff2581a7"); } } }
using System;标签:加密,string,C#,DES,System,艺龙,key,password,String From: https://www.cnblogs.com/Leo_wl/p/16828913.html
using System.Collections.Generic;
using System.IO; using System.Linq; using System.Reflection; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace Common.Util { /// <summary> /// DES Encrypt Helper. /// </summary> public class DESHelper { #region ========加密======== /// <summary> /// DESs the CBC encryptor. /// DES CBC 加密. /// </summary> /// <param name="encryStr">The encry string.</param> /// <param name="key">The key.</param> /// <param name="iv">The iv.</param> /// <returns>encrypt context.</returns> public static string DesCBCEncryptor(string encryStr, string key, string iv) { if (!string.IsNullOrEmpty(encryStr)) { return Encrypt(encryStr, key, iv).ToLower(); } else { return string.Empty; } } /// <summary> /// Encrypts the specified encry string. /// </summary> /// <param name="encryStr">The encry string.</param> /// <param name="key">The key.</param> /// <param name="iv">The iv.</param> /// <returns>encrypt context.</returns> [Obsolete(@"可用 (AES) 高级加密标准较新的对称加密算法。 请考虑使用 AesCryptoServiceProvider 类,而不是 TripleDESCryptoServiceProvider 类。 TripleDESCryptoServiceProvider仅用于与旧版应用程序和数据兼容。 参考地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.tripledescryptoserviceprovider?view=netframework-3.5")] public static string Encrypt(string encryStr, string key, string iv) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray; inputByteArray = Encoding.Default.GetBytes(encryStr); des.Key = Encoding.UTF8.GetBytes(key.Substring(0, 8)); des.IV = Encoding.UTF8.GetBytes(iv.Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); // {0:X2}大写的十六进制 } return ret.ToString(); } #endregion #region ========解密======== /// <summary> /// DES CBC 解密. /// </summary> /// <param name="decryStr">decryStr.</param> /// <param name="key">key.</param> /// <param name="iv">iv.</param> /// <returns>string.</returns> public static string DesCBCDecryptor(string decryStr, string key, string iv) { if (!string.IsNullOrEmpty(decryStr)) { return Decrypt(decryStr, key, iv); } else { return string.Empty; } } /// <summary> /// Decrypts the specified decry string. /// </summary> /// <param name="decryStr">The decry string.</param> /// <param name="key">The key.</param> /// <param name="iv">The iv.</param> /// <returns>decrypts context.</returns> [Obsolete(@"可用 (AES) 高级加密标准较新的对称加密算法。 请考虑使用 AesCryptoServiceProvider 类,而不是 TripleDESCryptoServiceProvider 类。 TripleDESCryptoServiceProvider仅用于与旧版应用程序和数据兼容。 参考地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.security.cryptography.tripledescryptoserviceprovider?view=netframework-3.5")] public static string Decrypt(string decryStr, string key, string iv) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); int len; len = decryStr.Length / 2; byte[] inputByteArray = new byte[len]; int x, i; for (x = 0; x < len; x++) { i = Convert.ToInt32(decryStr.Substring(x * 2, 2), 16); inputByteArray[x] = (byte)i; } des.Key = Encoding.UTF8.GetBytes(key.Substring(0, 8)); des.IV = Encoding.UTF8.GetBytes(iv.Substring(0, 8)); System.IO.MemoryStream ms = new System.IO.MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.Default.GetString(ms.ToArray()); } #endregion } }