public PublicKey(byte[] certData) { HSMPublicKey = certData; //HSMPublicKey = new byte[certData.Length - 2]; //Array.Copy(certData, 2, HSMPublicKey, 0, certData.Length-2); //public certificate Asn1.Asn1Reader reader = new Asn1.Asn1Reader(certData); reader.MoveNext(); byte[] FirstDataSeq = reader.GetPayload(); string FirstDataSeqHex = HexByteUtils.ByteArrayToHex(FirstDataSeq); keyBytesData = FirstDataSeq; keyStringData = FirstDataSeqHex; //public modulus reader.MoveNext(); byte[] SecondDataSeq = reader.GetTagRawData(); string SecondDataSeqHex = HexByteUtils.ByteArrayToHex(SecondDataSeq); byte[] modulus = new byte[3]; Array.Copy(SecondDataSeq, 2, modulus, 0, 3); //create modulus object mod = new Modulus(modulus); }
public Dictionary <string, string> GenerateMacOnRSACertificate(byte[] certData) { Dictionary <string, string> result = new Dictionary <string, string>(); byte[] responseBytes = builder.BuildMacOnRSACertificate(certData); string responseAscii = Encoding.ASCII.GetString(responseBytes); string responseHex = HexByteUtils.ByteArrayToHex(responseBytes); String errorCode = responseAscii.Substring(8, 2); result.Add("Header", responseAscii.Substring(2, 4)); result.Add("ResponseCode", responseAscii.Substring(6, 2)); result.Add("ErrorCode", errorCode); if (errorCode == "00") { result.Add("MAC", responseHex.Substring(20, 8)); result.Add("PublicKey", responseHex.Substring(28)); } else { logger.Error("ERROR CODE: " + errorCode); } return(result); }
public Modulus(byte[] mod) { byte[] modulus = new byte[3]; //more than 4 octets then it has a length indicator if (mod.Length > 4) { Array.Copy(mod, 2, modulus, 0, 3); mod = modulus; } ModBytesData = mod; ModStringData = HexByteUtils.ByteArrayToHex(mod); ModInteger = int.Parse(ModStringData, NumberStyles.HexNumber); }
public byte[] getModulusBytes(int padding) { try { int discarded; return(HexByteUtils.GetBytes(ModStringData.PadLeft( padding, '0'), out discarded)); } catch (System.IO.InvalidDataException e) { string p = e.Message; return(new byte[0]); } }
/* * H8/H9 Command * * Input Data: * * 001-H801<FDC694A6> * <30550250AB378F98E373BBC6FA5E698F4F095A6D693A851E53C35CC9633947399C09D70932776DBEA5F2F0F0C4DAB4693CACB4D07B19242FF0435C55E3D4E28EFD563457F7EBA31BE1123DEA78CEC1573716130B020103> * ;99 * 0192 * <99658789F42672E7C51CB6ECAF3F061BBABCD954D4113E1CD9BD7BD4DF1BD94E6CBC10F497E9AE68265E87F77BFF293AA2D9FDE9C1A8F12A04D9B4D8DB9F5EAEE4690883838DEF670174E70C79E674F97E2457DD85EEEB346A17DD1F39CB3E8B2D69949436051994F8687F0FEE6558F28180D5A63946CD60604B1C82F6AE14454F5824CBFDCEE07478D2F0239299B64CD900DFF7559423E98F0C7AB8229933E4DD5A5E0BD736F8172668676949493577E323FC8EC592437F6DF20EDB5FBB6E92> * ;0080 * <7C9DDD3AEFF1D50BAFD11DBAF240BE827BAA156F9E8BB555CC019E183B3708F26EBE6C94702A9AD7CC1D2159CF587437532969D113C70BD622EB81AFC06E9408F1B69F3ED838A9EADFB41FB0E6E4202E> * ;1234567890123456;000 * * * Input data converted to binary * * <3030312D48383031FDC694A630550250AB378F98E373BBC6FA5E698F4F095A6D693A851E53C35CC9633947399C09D70932776DBEA5F2F0F0C4DAB4693CACB4D07B19242FF0435C55E3D4E28EFD563457F7EBA31BE1123DEA78CEC1573716130B0201033B39393031393299658789F42672E7C51CB6ECAF3F061BBABCD954D4113E1CD9BD7BD4DF1BD94E6CBC10F497E9AE68265E87F77BFF293AA2D9FDE9C1A8F12A04D9B4D8DB9F5EAEE4690883838DEF670174E70C79E674F97E2457DD85EEEB346A17DD1F39CB3E8B2D69949436051994F8687F0FEE6558F28180D5A63946CD60604B1C82F6AE14454F5824CBFDCEE07478D2F0239299B64CD900DFF7559423E98F0C7AB8229933E4DD5A5E0BD736F8172668676949493577E323FC8EC592437F6DF20EDB5FBB6E923B303038307C9DDD3AEFF1D50BAFD11DBAF240BE827BAA156F9E8BB555CC019E183B3708F26EBE6C94702A9AD7CC1D2159CF587437532969D113C70BD622EB81AFC06E9408F1B69F3ED838A9EADFB41FB0E6E4202E3B313233343536373839303132333435363B303030> * * * Response: * * 001-H900 * H604A678C8C78E1B9CFD415220D418E76 * U9912C5D8B113B5E9D6787D57EE9E43BA * 1122334455 * 9876543210987654 */ public byte[] BuildCrossAcquirerKeyEncryptionKeyunderInitialTransportKey(string publicKeyEncoding, byte[] macPublicKey, byte[] pinPadPublicKey, byte[] secretKey, byte[] dataBlock, string randomNumber) { String message = "H8"; //ascii message += publicKeyEncoding; //ascii byte[] bytes1 = Encoding.ASCII.GetBytes(message); message += macPublicKey; //binary message += pinPadPublicKey; //binary byte[] byte2 = macPublicKey.Concat(pinPadPublicKey).ToArray(); string message2 = ";99"; //ascii message2 += secretKey.Length.ToString("D4"); //ascii byte[] byte3 = Encoding.ASCII.GetBytes(message2); byte[] byte4 = secretKey; //binary string message4 = ";"; //ascii message4 += dataBlock.Length.ToString("D4"); //ascii byte[] byte5 = Encoding.ASCII.GetBytes(message4); byte[] byte6 = dataBlock; //binary byte[] byte7 = Encoding.ASCII.GetBytes(";"); //ascii int discarded; byte[] byte8 = HexByteUtils.GetBytes(randomNumber, out discarded); //binary message += ";000"; //ascii byte[] byte9 = Encoding.ASCII.GetBytes(";000"); byte[] dataBytes = bytes1.Concat(byte2) .Concat(byte3) .Concat(byte4) .Concat(byte5) .Concat(byte6) .Concat(byte7) .Concat(byte8) .Concat(byte9) .ToArray(); return(SendBytes(dataBytes)); }
public string GetPrivateKeyWithoutLengthString() { return(HexByteUtils.ByteArrayToHex(_certDataWithoutLength));; }
//EI HSM Command public RsaCertificate GenerateRsaKeyPair(int keyLength, string publicExponent = "03", string keyType = "2", string publicKeyEncoding = "01") { try { Dictionary <string, string> result = new Dictionary <string, string>(); byte[] responsebytes = builder.BuildRsaKeyPair(keyLength, publicExponent, keyType, publicKeyEncoding); string response = Encoding.ASCII.GetString(responsebytes); string hexdatatest = HexByteUtils.ByteArrayToHex(responsebytes); String errorCode = response.Substring(8, 2); result.Add("Header", response.Substring(2, 4)); result.Add("ResponseCode", response.Substring(6, 2)); result.Add("ErrorCode", errorCode); logger.Debug("ErrorCode: " + errorCode); if (errorCode == "00") { int runningPlace = 0; byte[] CertificateData = new byte[responsebytes.Length - 10]; result.Add("PPPK_PK_Combined", response.Substring(10)); Array.Copy(responsebytes, 10, CertificateData, 0, responsebytes.Length - 10); string hexdata = HexByteUtils.ByteArrayToHex(CertificateData); byte[] _PrivateKeyLength = new byte[4]; string cert_raw_data2 = BitConverter.ToString(CertificateData).Replace("-", string.Empty); int mod_inidex = cert_raw_data2.IndexOf("0203010001"); runningPlace = (mod_inidex / 2) + 5; var _PublicKey = new byte[runningPlace]; Array.Copy(CertificateData, 0, _PublicKey, 0, runningPlace); Array.Copy(CertificateData, runningPlace, _PrivateKeyLength, 0, 4); string cert_raw_data3 = BitConverter.ToString(_PublicKey).Replace("-", string.Empty); string cert_raw_data4 = BitConverter.ToString(_PrivateKeyLength).Replace("-", string.Empty); string priv_length = Encoding.UTF8.GetString(_PrivateKeyLength); int priv_len = Int32.Parse(priv_length); var _PrivateKey = new byte[priv_len]; runningPlace += _PrivateKeyLength.Length; Array.Copy(CertificateData, runningPlace, _PrivateKey, 0, priv_len); return(new RsaCertificate(_PublicKey, _PrivateKey)); } logger.Error("ERROR CODE: " + errorCode); return(null); } catch (Exception e) { logger.Error(e.ToString()); return(null); } }
public string getHSMPublicKeyString() { return(HexByteUtils.ByteArrayToHex(HSMPublicKey)); }
public void setModulus(int modulus) { ModStringData = modulus.ToString("X2"); ModBytesData = HexByteUtils.GetBytes(ModStringData, out discarded); ModInteger = int.Parse(ModStringData, NumberStyles.HexNumber); }
public Modulus(string mod) { ModStringData = mod; ModBytesData = HexByteUtils.GetBytes(mod, out discarded); }