private void send(string sapdu) { sapdu = xTool.ConvertTool.RemoveSpace(sapdu); string apdu = this.claChannel(sapdu.Substring(0, 2)) + sapdu.Substring(2); this.Command = apdu; byte[] temp = this.send(ConvertTool.String2Bytes(apdu)); string resp = ConvertTool.Bytes2String(temp); if ((resp.Length == 4) && (this.autoRes != 0)) { if (resp.Substring(0, 2) == "6C" || resp.Substring(0, 2) == "6c") { temp = this.send(ConvertTool.String2Bytes(apdu.Substring(0, apdu.Length - 2) + resp.Substring(2, 2))); resp = ConvertTool.Bytes2String(temp); } else if (resp.Substring(0, 2) == "61") { temp = this.send(ConvertTool.String2Bytes(this.claChannel("00") + "C00000" + resp.Substring(2, 2))); resp = ConvertTool.Bytes2String(temp); while (resp.Substring(0, 2) == "61") //61xx { temp = this.send(ConvertTool.String2Bytes(this.claChannel("00") + "C00000" + resp.Substring(2, 2))); resp = ConvertTool.Bytes2String(temp); } } } this.Response = resp; }
/// <summary> /// AES解密 /// </summary> /// <param name="Data">被解密的密文</param> /// <param name="Key">密钥</param> /// <param name="Vector">向量</param> /// <returns>明文</returns> public static String AesCBCDe(String Data, String Key, String Vector) { Data = ConvertTool.RemoveSpace(Data); Key = ConvertTool.RemoveSpace(Key); Vector = ConvertTool.RemoveSpace(Vector); if (Key.Length != 32 && Key.Length != 48 && Key.Length != 64) { throw new Exception("Invalid Key, Not 16 or 24 or 32 bytes"); } if (Data.Length % 32 != 0 || Data.Length == 0) { throw new Exception("Invalid Cipher, Not 16*n bytes"); } if (Vector.Length != 32) { throw new Exception("Invalid IV, Not 16*n bytes"); } Byte[] encryptedBytes = ConvertTool.String2Bytes(Data); Byte[] bKey = ConvertTool.String2Bytes(Key); Byte[] bVector = ConvertTool.String2Bytes(Vector); Byte[] original = null; // 解密后的明文 Rijndael Aes = Rijndael.Create(); Aes.Mode = CipherMode.CBC; Aes.Padding = PaddingMode.None; Aes.KeySize = Key.Length * 4;; // 开辟一块内存流,存储密文 using (MemoryStream Memory = new MemoryStream(encryptedBytes)) { // 把内存流对象包装成加密流对象 using (CryptoStream Decryptor = new CryptoStream(Memory, Aes.CreateDecryptor(bKey, bVector), CryptoStreamMode.Read)) { // 明文存储区 using (MemoryStream originalMemory = new MemoryStream()) { Byte[] Buffer = new Byte[1024]; Int32 readBytes = 0; while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0) { originalMemory.Write(Buffer, 0, readBytes); } original = originalMemory.ToArray(); } } } return(ConvertTool.Bytes2String(original)); }
/// <summary> /// AES加密 /// </summary> /// <param name="Data">被加密的明文</param> /// <param name="Key">密钥</param> /// <param name="Vector">向量</param> /// <returns>密文</returns> public static String AesCBCEn(String Data, String Key, String Vector) { Data = ConvertTool.RemoveSpace(Data); Key = ConvertTool.RemoveSpace(Key); Vector = ConvertTool.RemoveSpace(Vector); if (Key.Length != 32 && Key.Length != 48 && Key.Length != 64) { throw new Exception("Invalid Key, Not 16 or 24 or 32 bytes"); } if (Data.Length % 32 != 0 || Data.Length == 0) { throw new Exception("Invalid Data, Not 16*n bytes"); } if (Vector.Length != 32) { throw new Exception("Invalid IV, Not 16*n bytes"); } Byte[] plainBytes = ConvertTool.String2Bytes(Data); Byte[] bKey = ConvertTool.String2Bytes(Key); Byte[] bVector = ConvertTool.String2Bytes(Vector); Byte[] Cryptograph = null; // 加密后的密文 Rijndael Aes = Rijndael.Create(); Aes.Mode = CipherMode.CBC; Aes.Padding = PaddingMode.None; Aes.KeySize = Key.Length * 4;; // 开辟一块内存流 using (MemoryStream Memory = new MemoryStream()) { // 把内存流对象包装成加密流对象 using (CryptoStream Encryptor = new CryptoStream(Memory, Aes.CreateEncryptor(bKey, bVector), CryptoStreamMode.Write)) { // 明文数据写入加密流 Encryptor.Write(plainBytes, 0, plainBytes.Length); Encryptor.FlushFinalBlock(); Cryptograph = Memory.ToArray(); } } return(ConvertTool.Bytes2String(Cryptograph)); }
public static string SM4EncryptECB(string input, string key) { input = ConvertTool.RemoveSpace(input); key = ConvertTool.RemoveSpace(key); if (key.Length != 32) { throw new Exception("Invalid Key, Not 16 bytes"); } if (input.Length % 32 != 0 || input.Length == 0) { throw new Exception("Invalid Data, Not 16*n bytes"); } byte[] output = SM4.Encrypt_ECB(ConvertTool.String2Bytes(input), ConvertTool.String2Bytes(key)); return(ConvertTool.Bytes2String(output)); }
/// <summary> /// AES解密(无向量) /// </summary> /// <param name="encryptedBytes">被加密的明文</param> /// <param name="key">密钥</param> /// <returns>明文</returns> public static string AesECBDe(String Data, String Key) { Data = ConvertTool.RemoveSpace(Data); Key = ConvertTool.RemoveSpace(Key); if (Key.Length != 32 && Key.Length != 48 && Key.Length != 64) { throw new Exception("Invalid Key, Not 16 or 24 or 32 bytes"); } if (Data.Length % 32 != 0 || Data.Length == 0) { throw new Exception("Invalid Cipher, Not 16*n bytes"); } Byte[] encryptedBytes = ConvertTool.String2Bytes(Data); Byte[] bKey = ConvertTool.String2Bytes(Key); MemoryStream mStream = new MemoryStream(encryptedBytes); RijndaelManaged aes = new RijndaelManaged { Mode = CipherMode.ECB, Padding = PaddingMode.None, KeySize = Key.Length * 4 }; ; aes.Key = bKey; CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read); byte[] tmp = new byte[encryptedBytes.Length + 32]; int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32); byte[] ret = new byte[len]; Array.Copy(tmp, 0, ret, 0, len); cryptoStream.Close(); mStream.Close(); aes.Clear(); return(ConvertTool.Bytes2String(ret)); }
public string xor(string key, string data) { try { key = ConvertTool.RemoveSpace(key); data = ConvertTool.RemoveSpace(data); if (key.Length % 2 != 0) { key = "0" + key; } if (data.Length % 2 != 0) { data = "0" + data; } while (key.Length > data.Length) { data = "0" + data; } while (key.Length < data.Length) { key = "0" + key; } byte[] data1 = ConvertTool.String2Bytes(key); byte[] data2 = ConvertTool.String2Bytes(data); byte[] res = new byte[data1.Length]; for (int i = 0; i < data1.Length; i++) { res[i] = (byte)(data1[i] ^ data2[i]); } return(ConvertTool.Bytes2String(res)); } catch (Exception ex) { print(error_prefix + ex.Message); return(null); } }
public static string HashSHA1(string data) { SHA1 sha = SHA1Managed.Create(); string res = ""; if (data.Contains(":")) { using (FileStream fs = new FileStream(data, FileMode.Open)) { res = ConvertTool.Bytes2String(sha.ComputeHash(fs)); } } else { data = ConvertTool.RemoveSpace(data); res = ConvertTool.Bytes2String(sha.ComputeHash(ConvertTool.String2Bytes(data))); } return(res); }
public static string SHA3_512(string data) { data = ConvertTool.RemoveSpace(data); SHA3 sha3 = new SHA3(512); string res = ""; if (data.Contains(":")) { using (FileStream fs = new FileStream(data, FileMode.Open)) { res = ConvertTool.Bytes2String(sha3.ComputeHash(fs)); } } else { res = ConvertTool.Bytes2String(sha3.ComputeHash(ConvertTool.String2Bytes(data))); } return(res); }
public static string HMAC_SHA384(string key, string data) { key = ConvertTool.RemoveSpace(key); data = ConvertTool.RemoveSpace(data); HMACSHA384 hmac = new HMACSHA384(ConvertTool.String2Bytes(key)); byte[] hashValue; if (data.Contains(":")) { using (FileStream inStream = new FileStream(data, FileMode.Open)) { hashValue = hmac.ComputeHash(inStream); } } else { hashValue = hmac.ComputeHash(ConvertTool.String2Bytes(data)); } return(ConvertTool.Bytes2String(hashValue)); }
public static string MD5(string data) { MD5 md5 = System.Security.Cryptography.MD5.Create(); string res = ""; if (data.Contains(":")) { using (FileStream fs = new FileStream(data, FileMode.Open)) { res = ConvertTool.Bytes2String(md5.ComputeHash(fs)); } } else { data = ConvertTool.RemoveSpace(data); res = ConvertTool.Bytes2String(md5.ComputeHash(ConvertTool.String2Bytes(data))); } return(res); }
public string APDU(string sapdu, string response) { sapdu = xTool.ConvertTool.RemoveSpace(sapdu); string apdu = this.claChannel(sapdu.Substring(0, 2)) + sapdu.Substring(2); byte[] temp = this.send(ConvertTool.String2Bytes(apdu)); string resp = ConvertTool.Bytes2String(temp); if (response != "") { if (response.Length == 4) { string sw = resp.Substring(resp.Length - 4); if (!sw.Equals(response, StringComparison.OrdinalIgnoreCase)) { Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Input, new Action(() => { print("Error, Expected SW: " + response); errCount++; })); } } else { if (!resp.Equals(response, StringComparison.OrdinalIgnoreCase)) { Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Input, new Action(() => { print("Error, Expected Response: " + response); errCount++; })); } } } return(resp); }
//no padding public static string TriDesMAC(string data, string key, string icv = "0000000000000000") { data = ConvertTool.RemoveSpace(data); key = ConvertTool.RemoveSpace(key); icv = ConvertTool.RemoveSpace(icv); if (key.Length != 32) { throw new Exception("Invalid Key, Not 16 bytes"); } if (data.Length % 16 != 0 || data.Length == 0) { throw new Exception("Invalid Data, Not 8*n bytes"); } if (icv.Length != 16) { throw new Exception("Invalid IV, , Not 8 bytes"); } byte[] inputByteArray = ConvertTool.String2Bytes(data); string KeyA = key.Substring(0, 16); string KeyB = key.Substring(16, 16); int i; byte[] data1 = new byte[8]; byte[] data2 = ConvertTool.String2Bytes(icv); byte[] xorres = new byte[8]; for (i = 0; i < inputByteArray.Length; i += 8) { Array.Copy(inputByteArray, i, data1, 0, 8); for (int k = 0; k < 8; k++) { xorres[k] = (byte)(data1[k] ^ data2[k]); } data2 = ConvertTool.String2Bytes(DesECBEn(ConvertTool.Bytes2String(xorres), KeyA)); } return(DesECBEn(DesECBDe(ConvertTool.Bytes2String(data2), KeyB), KeyA)); }
public static string SM4DecryptCBC(string input, string iv, string key) { input = ConvertTool.RemoveSpace(input); key = ConvertTool.RemoveSpace(key); iv = ConvertTool.RemoveSpace(iv); if (key.Length != 32) { throw new Exception("Invalid Key, Not 16 bytes"); } if (input.Length % 32 != 0 || input.Length == 0) { throw new Exception("Invalid Cipher, Not 16*n bytes"); } if (iv.Length != 32) { throw new Exception("Invalid IV, Not 16 bytes"); } byte[] output = SM4.Decrypt_CBC(ConvertTool.String2Bytes(input), ConvertTool.String2Bytes(key), ConvertTool.String2Bytes(iv)); return(ConvertTool.Bytes2String(output)); }
public static string SM3(string data) { data = ConvertTool.RemoveSpace(data); string res = ""; SM3Cng sm3 = new SM3Cng(); if (data.Contains(":")) { using (FileStream fs = new FileStream(data, FileMode.Open)) { res = ConvertTool.Bytes2String(sm3.ComputeHash(fs)); } } else { //if (data.Length % 2 != 0) // throw new Exception("Invalid Data, Not 2*n bytes"); res = ConvertTool.Bytes2String(sm3.ComputeHash(ConvertTool.String2Bytes(data))); } return(res); }
public static string SM4MAC(string input, string iv, string key) { input = ConvertTool.RemoveSpace(input); key = ConvertTool.RemoveSpace(key); iv = ConvertTool.RemoveSpace(iv); if (key.Length != 32) { throw new Exception("Invalid Key, Not 16 bytes"); } if (input.Length % 32 != 0 || input.Length == 0) { throw new Exception("Invalid Cipher, Not 16*n bytes"); } if (iv.Length != 32) { throw new Exception("Invalid IV, Not 16 bytes"); } byte[] data_I = ConvertTool.String2Bytes(iv); for (int i = 0; i < input.Length; i = i + 32) { string temp = input.Substring(i, 32); byte[] data1 = ConvertTool.String2Bytes(temp); for (int j = 0; j < 16; j++) { data_I[j] = (byte)(data1[j] ^ data_I[j]); } string res; res = SM4EncryptECB(ConvertTool.Bytes2String(data_I), key); data_I = ConvertTool.String2Bytes(res); } return(ConvertTool.Bytes2String(data_I)); }
public void Reset() { this.securityLevel = "00"; try { string temp; byte[] atr; if (isDebug() == false) { if (this.ReaderName == null || this.ReaderName == "") { errCount++; throw new PCSCException(SCardError.NoReaderSelected, "PCSC: 0x" + ((int)SCardError.NoReaderSelected).ToString("X8") + ", " + getEnumDescription(SCardError.NoReaderSelected)); } if (this.Reader.IsConnected) { this.Reader.Disconnect(SCardReaderDisposition.Reset); } SCardError rc = this.Reader.Connect(this.ReaderName, SCardShareMode.Shared, SCardProtocol.Any); if (rc != SCardError.Success) { errCount++; throw new PCSCException(rc, "PCSC: 0x" + ((int)rc).ToString("X8") + ", " + getEnumDescription(rc)); //throw new ArgumentException("Description: " + getEnumDescription(rc) + "\n" + "PCSC Error: " + "0x" + ((int)rc).ToString("X8") + "\n"); } else { this.Reader.GetAttrib(SCardAttribute.AtrString, out atr); temp = ConvertTool.Bytes2String(atr); Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Input, new Action(() => { print("ATR: " + temp); /*if (this.isTime) * { * var tm = (t2.Hour * 60 * 60 + t2.Minute * 60 + t2.Second) * 1000 + t2.Millisecond - (t1.Hour * 60 * 60 + t1.Minute * 60 + t1.Second) * 1000 - t1.Millisecond; * print("Time: " + tm + "ms\n"); * }*/ })); } } else { int iport = System.Convert.ToInt32(this.port, 10); temp = ConvertTool.Bytes2String(debug.reset(iport)); Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Input, new Action(() => { print("ATR: " + temp); })); } this.Response = temp; } //catch (ArgumentNullException) //{ // Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Input, // new Action(() => // { // print("\n" + "No Selectd Redaer" + "\n\n"); // })); //} catch (Exception ex) { Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Input, new Action(() => { print(ex.Message); })); } }