public static PayPhoneSecureData GetPayphoneSecureDataInfo(string msisdn, string paymentMethodTypeCode, string ttid, long amount = -1, string version = "10") { var payPhoneSecureDataInfo = new PayPhoneSecureData() { CustomerId = msisdn, PrimaryMobileNumber = msisdn, PinDesKey = GenerateRandomString(32), Identifier = ttid, PayPhoneSecureDataVersion = version, PayPhoneSecureMacVersion = version, Type = paymentMethodTypeCode }; if (amount != -1) { payPhoneSecureDataInfo.Amount = amount.ToString(); } return(payPhoneSecureDataInfo); }
public static string CalculateSecureData12(PayPhoneSecureData secureDataInfo, byte[] macDesBytes, string macDataVaue = null) { var secureBytes = new byte[64]; var headerBytes = Hex.Decode(secureDataInfo.PayPhoneSecureDataHeaderValue); var formatVersionBytes = Hex.Decode(secureDataInfo.PayPhoneSecureDataVersion); var macVersionBytes = Hex.Decode(secureDataInfo.PayPhoneSecureMacVersion); var footerBytes = Hex.Decode(secureDataInfo.FooterBytes); byte[] pinDesKey; try { if (secureDataInfo.PinDesKey.Length == 32) { pinDesKey = Hex.Decode(secureDataInfo.PinDesKey); } else { pinDesKey = Hex.Decode(string.Format("{0}{1}", secureDataInfo.PinDesKey.Length.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0'), secureDataInfo.PinDesKey.PadRight(30, '0'))); } } catch (Exception e) { throw new Exception(string.Format("Error while Hex.Decoding account number: {0}", secureDataInfo.PinDesKey), e); } byte[] customerIdBytes; try { var customerId = string.IsNullOrWhiteSpace(secureDataInfo.CustomerId) ? secureDataInfo.Pan : secureDataInfo.CustomerId; customerIdBytes = Hex.Decode(string.Format("{0}{1}{2}", (customerId.Length).ToString().Length, customerId.Length, customerId).PadRight(40, '0')); } catch (Exception e) { throw new Exception(string.Format("Error while Hex.Decoding pan: {0}", secureDataInfo.Pan), e); } var macData = ""; if (string.IsNullOrWhiteSpace(macDataVaue)) { macData = string.Format("{0}default{1}", secureDataInfo.PrimaryMobileNumber, secureDataInfo.Identifier); } else { macData = macDataVaue; } var macBytes = BouncyCastleGetCbcMac(macData, macDesBytes); // macBytes = Hex.Encode(macBytes); Array.Copy(headerBytes, 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(macDesBytes, 0, secureBytes, 19, 16); Array.Copy(customerIdBytes, 0, secureBytes, 35, 20); Array.Copy(macBytes, 0, secureBytes, 55, 4); //use this string to pard the data var paddingStringBytes = Hex.Decode(SetFixedLength("", 28, true, '0')); Array.Copy(paddingStringBytes, 0, secureBytes, 59, 4); Array.Copy(footerBytes, 0, secureBytes, 63, 1); var encryptedSecureData = Hex.Encode(BouncyCastleRsaEncrypt(secureBytes, secureDataInfo.RsaPublicKeyExponent, secureDataInfo.RsaPublicKeyModulus)); return(Encoding.UTF8.GetString(encryptedSecureData, 0, encryptedSecureData.Length)); }