public static Dictionary <string, string> generateSecureData(Dictionary <string, string> options, Dictionary <string, string> pinData) { string pin = "0000"; string cvv = "000"; string expiry = "0000"; Random rand = new Random(); string ttId = rand.Next(999).ToString(); string pan = "0000000000000000"; string amt = ""; string msisdn = ""; string pubMod = publicKeyModulus; string pubExp = publicKeyExponent; if (options.ContainsKey("pan")) { options.TryGetValue("pan", out pan); pan = (pan == null || pan.Equals("")) ? "0000000000000000" : pan; } if (options.ContainsKey("ttId")) { options.TryGetValue("ttId", out ttId); ttId = (ttId == null || ttId.Equals("")) ? rand.Next(999).ToString() : ttId; } if (options.ContainsKey("amount")) { options.TryGetValue("amount", out amt); amt = (amt == null) ? "" : amt; } if (options.ContainsKey("mobile")) { options.TryGetValue("mobile", out msisdn); msisdn = (msisdn == null) ? "" : msisdn; } if (pinData.ContainsKey("pin")) { pinData.TryGetValue("pin", out pin); pin = (pin == null || pin.Equals("")) ? "0000" : pin; } if (pinData.ContainsKey("cvv")) { pinData.TryGetValue("cvv", out cvv); cvv = (cvv == null || cvv.Equals("")) ? "000" : cvv; } if (pinData.ContainsKey("expiry")) { pinData.TryGetValue("expiry", out expiry); expiry = (expiry == null || expiry.Equals("")) ? "0000" : expiry; } byte[] pinKey = DESUtils.generateKey(); /* * if(options.ContainsKey("publicKeyModulus")) * { * options.TryGetValue("publicKeyModulus", out pubMod); * pubMod = (pubMod == null || pubMod.Equals("")) ? publicKeyModulus : pubMod; * } * if(options.ContainsKey("publicKeyExponent")) * { * options.TryGetValue("publicKeyExponent", out pubExp); * pubExp = (pubExp == null || pubExp.Equals("")) ? publicKeyExponent : pubExp; * } */ string secureData = getGenericSecure(pan, msisdn, ttId, amt, pinKey, pinKey); string pinBlock = getEncryptedPinCvv2ExpiryDateBlock(pin, cvv, expiry, pinKey); string macData = getMacCipherText(msisdn, ttId, amt); string mac = MACUtils.getMacValueUsingHMAC(macData, pinKey); Dictionary <string, string> secure = new Dictionary <string, string>(); secure.Add("secureData", secureData); secure.Add("pinBlock", pinBlock); secure.Add("mac", mac); return(secure); }
public static String getGenericSecure(string pan, string msisdn, string ttId, string amt, byte[] pinKey, byte[] macKey) { byte[] secureBytes = new byte[64]; byte[] headerBytes = new byte[1]; byte[] formatVersionBytes = new byte[1]; byte[] macVersionBytes = new byte[1]; byte[] pinDesKey = new byte[16]; byte[] macDesKey = new byte[16]; byte[] customerIdBytes = new byte[10]; byte[] macBytes = new byte[4]; byte[] otherHexBytes = new byte[14]; byte[] footerBytes = new byte[1]; headerBytes = AppUtils.hexConverter(Constants.SECURE_HEADER); formatVersionBytes = AppUtils.hexConverter(Constants.SECURE_FORMAT_VERSION); macVersionBytes = AppUtils.hexConverter(Constants.SECURE_MAC_VERSION); pinDesKey = pinKey; macDesKey = macKey; footerBytes = AppUtils.hexConverter(Constants.SECURE_FOOTER); Array.Copy(headerBytes, headerBytes.GetLowerBound(0), secureBytes, 0, 1); Array.Copy(formatVersionBytes, 0, secureBytes, 1, 1); Array.Copy(macVersionBytes, 0, secureBytes, 2, 1); Array.Copy(pinDesKey, 0, secureBytes, 3, 16); Array.Copy(macDesKey, 0, secureBytes, 19, 16); string customerIdLen = pan == null ? "" : pan.Length.ToString(); string customerIdLenLen = customerIdLen.Length.ToString(); string customerIdBlock = customerIdLenLen + customerIdLen + pan; int customerIdBlockLen = customerIdBlock.Length; int maxLen = 20; int pandiff = maxLen - customerIdBlockLen; for (int i = 0; i < pandiff; i++) { customerIdBlock = customerIdBlock + "F"; } customerIdBytes = AppUtils.hexConverter(customerIdBlock); Array.Copy(customerIdBytes, 0, secureBytes, 35, 10); string macData = getMacCipherText(msisdn, ttId, amt); string mac = MACUtils.getMacValueUsingHMAC(macData, pinKey); mac = mac.Substring(0, 8); macBytes = AppUtils.hexConverter(mac); Array.Copy(macBytes, 0, secureBytes, 45, 4); string otherHex = "0000000000000000000000000000"; otherHexBytes = AppUtils.hexConverter(otherHex); Array.Copy(otherHexBytes, 0, secureBytes, 49, 14); Array.Copy(footerBytes, 0, secureBytes, 63, 1); var sb = new StringBuilder("new byte[] { "); foreach (var b in secureBytes) { sb.Append(b + ", "); } sb.Append("}"); Console.WriteLine(sb.ToString()); String encrytedSecure = Encoding.Default.GetString(RSAUtils.rsaEncrypt(publicKeyModulus, publicKeyExponent, secureBytes)); AppUtils.zeroise(secureBytes); return(encrytedSecure); }