/// <summary> /// Compute SHA1 hash for the given input data /// </summary> /// <param name="input"></param> /// <returns>Hash value</returns> public static string GetSha1(string input) { using (var sha1 = System.Security.Cryptography.SHA1.Create()) { byte[] inputBytes = StringTools.HexStringToByteArray(input); byte[] hash = sha1.ComputeHash(inputBytes); StringBuilder sb = new StringBuilder(); for (int i = 0; i < hash.Length; i++) { sb.Append(hash[i].ToString("X2")); } return(sb.ToString()); } }
/// <summary> /// Decrypt data using RSA Asymmetric Block Cipher /// </summary> /// <param name="data">The data to decrypt</param> /// <param name="exponent">The key exponent to use</param> /// <param name="key">The RSA key to use</param> /// <returns>The decrypted data</returns> public string DecryptRsa(string data, string exponent, string key) { BigInteger mod = new BigInteger(key, 16); BigInteger pubExp = new BigInteger(exponent, 16); RsaKeyParameters pubParameters = new RsaKeyParameters(false, mod, pubExp); IAsymmetricBlockCipher eng = new RsaEngine(); eng.Init(true, pubParameters); byte[] encdata = StringTools.HexStringToByteArray(data); encdata = eng.ProcessBlock(encdata, 0, encdata.Length); string result = StringTools.ByteArrayToHexString(encdata); return(result); }
public static byte[] parseTagLengthData(byte[] data) { int index = 0; List <byte> _tagList = new List <byte>(); while (index < data.Length) { var temptag = new List <byte>(); //Get the tag name temptag.Add(data[index]); if ((data[index] & EmvConstants.SeeSubsequentBytes) == EmvConstants.SeeSubsequentBytes) { index++; temptag.Add(data[index]); } Console.WriteLine("EmvTag " + StringTools.ByteArrayToHexString(temptag.ToArray())); index++; // Get the length of the data to follow if ((data[index] & 0x80) == 0x80) { int bytesForLenght = data[index] % 0x80; index++; for (int i = 0; i < bytesForLenght; i++) { index++; } } else { index++; } string tagname = StringTools.ByteArrayToHexString(temptag.ToArray()).ToUpper(); if (EmvConstants.PdolTags.ContainsKey(tagname)) { _tagList.AddRange(StringTools.HexStringToByteArray(EmvConstants.PdolTags[tagname])); } else { throw new Exception($"Unknown PDOL tag {tagname}"); } } return(_tagList.ToArray()); }
/// <summary> /// <para>Initializer of the certificate object. Parsing and assigning individual fields into properties</para> /// </summary> /// <param name="certificate">The certificate to parse in string format. /// <param name="remainder">The remainder of the certificate. /// <param name="certificateType">The type (CertificateType) of the certificate.</param> public EmvCertificate(string certificate, string remainder, CertificateType certificateType) { _certificate = certificate; _remainder = remainder; _certificateType = certificateType; Version = byte.Parse(certificate.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); CType = byte.Parse(certificate.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); Issuer = StringTools.HexStringToByteArray(certificate.Substring(4, 8)); ExpDate = StringTools.HexStringToByteArray(certificate.Substring(12, 4)); SerialNumber = StringTools.HexStringToByteArray(certificate.Substring(16, 6)); HashAlgIndicator = byte.Parse(certificate.Substring(22, 2), System.Globalization.NumberStyles.HexNumber); pkAlg = byte.Parse(certificate.Substring(24, 2), System.Globalization.NumberStyles.HexNumber); pkLength = byte.Parse(certificate.Substring(26, 2), System.Globalization.NumberStyles.HexNumber); pkExponent = byte.Parse(certificate.Substring(28, 2), System.Globalization.NumberStyles.HexNumber); int totalLength = pkLength + BeforeKeyLength + HashLength + TrailerLength - (remainder.Length / 2); HasPadding = (certificate.Length / 2) > totalLength; int i = 30 + (pkLength * 2) - remainder.Length; PublicKey = StringTools.HexStringToByteArray(certificate.Substring(30, (pkLength * 2) - remainder.Length) + remainder); if (HasPadding) { Padding = StringTools.HexStringToByteArray(certificate.Substring(30, certificate.Length - totalLength)); i += certificate.Length - totalLength; } Hash = StringTools.HexStringToByteArray(certificate.Substring(i, HashLength * 2)); Trailer = byte.Parse(certificate.Substring(i + HashLength * 2, 2), System.Globalization.NumberStyles.HexNumber); if (certificateType == CertificateType.CA) { checkCA(); } }