public static string RSACRTde(string data, string p, string q, string dp, string dq, string invq) { data = ConvertTool.RemoveSpace(data); p = ConvertTool.RemoveSpace(p); q = ConvertTool.RemoveSpace(q); dp = ConvertTool.RemoveSpace(dp); dq = ConvertTool.RemoveSpace(dq); invq = ConvertTool.RemoveSpace(invq); BigInteger cipher = new BigInteger(ConvertTool.String2Bytes(data)); BigInteger rsaP = new BigInteger(ConvertTool.String2Bytes(p)); BigInteger rsaQ = new BigInteger(ConvertTool.String2Bytes(q)); BigInteger rsaDP = new BigInteger(ConvertTool.String2Bytes(dp)); BigInteger rsaDQ = new BigInteger(ConvertTool.String2Bytes(dq)); BigInteger rsaINVQ = new BigInteger(ConvertTool.String2Bytes(invq)); BigInteger m, m1, m2, h; m1 = cipher.modPow(rsaDP, rsaP); m2 = cipher.modPow(rsaDQ, rsaQ); var temp = m1 - m2; while (temp < 0) { temp += rsaP; } h = (rsaINVQ * temp) % rsaP; m = m2 + (h * rsaQ); return(m.ToHexString()); }
public int big_num_compare(string a, string b, int radix) { try { a = ConvertTool.RemoveSpace(a); b = ConvertTool.RemoveSpace(b); BigInteger bignum_a = new BigInteger(a, radix); BigInteger bignum_b = new BigInteger(b, radix); if (bignum_a == bignum_b) { return(0); } else if (bignum_a > bignum_b) { return(1); } else { return(-1); } } catch (Exception ex) { error(ex.Message); return(-1); } }
public void setRSA(string rsa_n, string rsa_d, string rsa_e) { rsa_n = ConvertTool.RemoveSpace(rsa_n); rsa_d = ConvertTool.RemoveSpace(rsa_d); rsa_e = ConvertTool.RemoveSpace(rsa_e); this.n = rsa_n; this.d = rsa_d; this.e = rsa_e; }
/// <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)); }
public static void GetE(string sp, string sdp) { sp = ConvertTool.RemoveSpace(sp); sdp = ConvertTool.RemoveSpace(sdp); BigInteger e, p, dp; p = new BigInteger(sp, 16); dp = new BigInteger(sdp, 16); e = dp.modInverse(p - 1); //e = temp % (p - 1); RSA_E = e.ToHexString(); }
public static string Encrypt(string source, string n, string d) { source = ConvertTool.RemoveSpace(source); n = ConvertTool.RemoveSpace(n); d = ConvertTool.RemoveSpace(d); BigInteger D = new BigInteger(ConvertTool.String2Bytes(d)); BigInteger N = new BigInteger(ConvertTool.String2Bytes(n)); BigInteger Data = new BigInteger(ConvertTool.String2Bytes(source)); BigInteger biText = new BigInteger(Data); BigInteger biEnText = biText.modPow(D, N); return(biEnText.ToHexString()); }
public static string PKCS1(string indata, string n, string d) { indata = ConvertTool.RemoveSpace(indata); n = ConvertTool.RemoveSpace(n); d = ConvertTool.RemoveSpace(d); string data = "0001"; //FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ; while (data.Length < (n.Length - 72)) { data += "FF"; } data = data + "003021300906052B0E03021A05000414" + Hash.HashSHA1(indata); return(RSA.RSAde(data, n, d)); }
public static string TriDesCBCDe(string cipher, string key, string icv = "0000000000000000") { cipher = ConvertTool.RemoveSpace(cipher); key = ConvertTool.RemoveSpace(key); icv = ConvertTool.RemoveSpace(icv); if (key.Length != 32 && key.Length != 48) { throw new Exception("Invalid Key, Not 16 or 24 bytes"); } if (cipher.Length % 16 != 0 || cipher.Length == 0) { throw new Exception("Invalid Cipher, Not 8*n bytes"); } if (icv.Length % 16 != 0) { throw new Exception("Invalid IV, Not 8 bytes"); } TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); des.Padding = PaddingMode.None; Type t = Type.GetType("System.Security.Cryptography.CryptoAPITransformMode"); object obj = t.GetField("Decrypt", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).GetValue(t); MethodInfo mi = des.GetType().GetMethod("_NewEncryptor", BindingFlags.Instance | BindingFlags.NonPublic); ICryptoTransform desCrypt = (ICryptoTransform)mi.Invoke(des, new object[] { ConvertTool.String2Bytes(key), CipherMode.CBC, ConvertTool.String2Bytes(icv), 0, obj }); byte[] result = desCrypt.TransformFinalBlock(ConvertTool.String2Bytes(cipher), 0, ConvertTool.String2Bytes(cipher).Length); return(BitConverter.ToString(result).Replace("-", "")); /* TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(); * byte[] inputByteArray = ConvertTool.String2Bytes(cipher); * des.Key = ConvertTool.String2Bytes(key); * des.IV = ConvertTool.String2Bytes(icv); * des.Mode = CipherMode.CBC; * des.Padding = System.Security.Cryptography.PaddingMode.None; * MemoryStream ms = new MemoryStream(); * CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); * cs.Write(inputByteArray, 0, inputByteArray.Length); * cs.FlushFinalBlock(); * StringBuilder ret = new StringBuilder(); * foreach (byte b in ms.ToArray()) * ret.AppendFormat("{0:X2}", b); * * return ret.ToString();*/ }
/// <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 string big_num_subtract(string a, string b, int radix) { try { a = ConvertTool.RemoveSpace(a); b = ConvertTool.RemoveSpace(b); BigInteger bignum_a = new BigInteger(a, radix); BigInteger bignum_b = new BigInteger(b, radix); return((bignum_a - bignum_b).ToString(radix)); } catch (Exception ex) { print(error_prefix + ex.Message); return(null); } }
public void externalAuthenticate(string sl) { sl = ConvertTool.RemoveSpace(sl); if (sl.Length == 1) { sl = "0" + sl; } this.securityLevel = sl; this.scp = this.Response.Substring(22, 2); if (this.scp == "02") { string zero12 = "000000000000000000000000"; this.skenc = DES.TriDesCBCEn("0182" + this.Response.Substring(24, 4) + zero12, this.enc); this.skmac = DES.TriDesCBCEn("0101" + this.Response.Substring(24, 4) + zero12, this.mac); this.skdek = DES.TriDesCBCEn("0181" + this.Response.Substring(24, 4) + zero12, this.dek); string hostCipher = DES.TriDesCBCEn(this.Response.Substring(24, 16) + hostRandom + "8000000000000000", this.skenc); hostCipher = hostCipher.Substring(32, 16); string authCmd = "8482" + sl + "0010"; this.macIni = DES.TriDesMAC(Pading80(authCmd + hostCipher), this.skmac); this.send(authCmd + hostCipher + this.macIni); } else if (this.scp == "01") { var tmp_str = this.Response.Substring(32, 8) + hostRandom.Substring(0, 8) + this.Response.Substring(24, 8) + hostRandom.Substring(8, 8); this.skenc = DES.TriDesECBEn(tmp_str, this.enc); this.skmac = DES.TriDesECBEn(tmp_str, this.mac); this.skdek = this.dek; string hostCipher = DES.TriDesCBCEn(this.Response.Substring(24, 16) + hostRandom + "8000000000000000", this.skenc); hostCipher = hostCipher.Substring(32, 16); string authCmd = "8482" + sl + "0010"; this.macIni = DES.TriDesCBCEn(authCmd + hostCipher + "800000", this.skmac).Substring(16, 16); this.send(authCmd + hostCipher + this.macIni); } }
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="plainBytes">被加密的明文</param> /// <param name="key">密钥</param> /// <returns>密文</returns> public static string AesECBEn(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 Data, Not 16*n bytes"); } MemoryStream mStream = new MemoryStream(); RijndaelManaged aes = new RijndaelManaged(); byte[] plainBytes = ConvertTool.String2Bytes(Data); byte[] bKey = ConvertTool.String2Bytes(Key); aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.None; aes.KeySize = Key.Length * 4; aes.Key = bKey; CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write); cryptoStream.Write(plainBytes, 0, plainBytes.Length); cryptoStream.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in mStream.ToArray()) { ret.AppendFormat("{0:X2}", b); } cryptoStream.Close(); mStream.Close(); aes.Clear(); return(ret.ToString()); }
/// <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 DesECBEn(string plaintext, string key) { plaintext = ConvertTool.RemoveSpace(plaintext); key = ConvertTool.RemoveSpace(key); if (key.Length != 16) { throw new Exception("Invalid Key, Not 8 bytes"); } if (plaintext.Length % 16 != 0 || plaintext.Length == 0) { throw new Exception("Invalid Data, Not 8*n bytes"); } DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Padding = PaddingMode.None; Type t = Type.GetType("System.Security.Cryptography.CryptoAPITransformMode"); object obj = t.GetField("Encrypt", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly).GetValue(t); MethodInfo mi = des.GetType().GetMethod("_NewEncryptor", BindingFlags.Instance | BindingFlags.NonPublic); ICryptoTransform desCrypt = (ICryptoTransform)mi.Invoke(des, new object[] { ConvertTool.String2Bytes(key), CipherMode.ECB, null, 0, obj }); byte[] result = desCrypt.TransformFinalBlock(ConvertTool.String2Bytes(plaintext), 0, (ConvertTool.String2Bytes(plaintext)).Length); return(BitConverter.ToString(result).Replace("-", "")); /* * DESCryptoServiceProvider des = new DESCryptoServiceProvider(); * byte[] inputByteArray = ConvertTool.String2Bytes(plaintext); * des.Key = ConvertTool.String2Bytes(key); * des.Mode = CipherMode.ECB; * des.Padding = System.Security.Cryptography.PaddingMode.None; * MemoryStream ms = new MemoryStream(); * CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); * cs.Write(inputByteArray, 0, inputByteArray.Length); * cs.FlushFinalBlock(); * StringBuilder ret = new StringBuilder(); * foreach (byte b in ms.ToArray()) * ret.AppendFormat("{0:X2}", b); * * return ret.ToString();*/ }
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 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 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)); }
//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 gplib(string reader, string enc, string mac, string dek, string channel, string kmc = "", string aid = "", string ver = "", string sl = "") { //if (reader == "") // throw new ArgumentException("No selected Reader or Reader name is NULL"); enc = ConvertTool.RemoveSpace(enc); mac = ConvertTool.RemoveSpace(mac); dek = ConvertTool.RemoveSpace(dek); channel = ConvertTool.RemoveSpace(channel); kmc = ConvertTool.RemoveSpace(kmc); aid = ConvertTool.RemoveSpace(aid); ver = ConvertTool.RemoveSpace(ver); sl = ConvertTool.RemoveSpace(sl); this.isPrint = true; this.isTime = false; this.control = null; this.DMType = 0; this.autoRes = 1; this.enc = enc; this.mac = mac; this.dek = dek; this.macIni = "0000000000000000"; this.channel = channel; this.ReaderName = reader; this.aid = aid; this.ver = ver; this.sl = sl; this.kmc = kmc; this.kmcType = 0; if (reader == "JCOP Debug") { this.debug = new JCOP(); return; } this.Reader = new SCardReader(Context); Context.Establish(SCardScope.System); }
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 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 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 static void GenPQKey(string sp, string sq, string exponent) { sp = ConvertTool.RemoveSpace(sp); sq = ConvertTool.RemoveSpace(sq); exponent = ConvertTool.RemoveSpace(exponent); BigInteger e, n, d, p, q, dp, dq, invq; p = new BigInteger(sp, 16); q = new BigInteger(sq, 16); e = new BigInteger(exponent, 16); n = p * q; BigInteger oula = (p - 1) * (q - 1); try { d = e.modInverse(oula); dp = d % (p - 1); dq = d % (q - 1); invq = q.modInverse(p); RSA_D = d.ToHexString(); RSA_N = n.ToHexString(); RSA_E = e.ToHexString(); RSA_P = p.ToHexString(); RSA_Q = q.ToHexString(); RSA_DP = dp.ToHexString(); RSA_DQ = dq.ToHexString(); RSA_INVQ = invq.ToHexString(); } catch (Exception) { throw new ArgumentException("e and φ(n) are not coprime, change e or p&q."); } }
public void setRSA_d(string rsa_d) { rsa_d = ConvertTool.RemoveSpace(rsa_d); this.d = rsa_d; }
public static void GenKey(int dwKeySize, string exponent) { string info = "key sizes from 16 bits to 16384 bits in increments of 8 bits."; exponent = ConvertTool.RemoveSpace(exponent); int count = 0; if (dwKeySize % 8 != 0 || dwKeySize < 16) { throw new ArgumentException(info); } BigInteger e = 0, n = 0, d = 0, p = 0, q = 0, dp = 0, dq = 0, invq = 0; label1: count++; if (count > 20) { throw new ArgumentException("change E or bits."); } e = new BigInteger(exponent, 16); if (dwKeySize < 384) { Random rand = new Random(); p = BigInteger.genPseudoPrime(dwKeySize / 2, 3, rand); q = BigInteger.genPseudoPrime(dwKeySize / 2, 3, rand); n = p * q; } else { //只支持长度从 384 位至 16384 位(增量为 8 位)的密钥 var rsa = new RSACryptoServiceProvider(dwKeySize); RSAParameters temp = rsa.ExportParameters(true); n = new BigInteger(temp.Modulus); p = new BigInteger(temp.P); q = new BigInteger(temp.Q); } BigInteger oula = (p - 1) * (q - 1); try { d = e.modInverse(oula); dp = d % (p - 1); dq = d % (q - 1); invq = q.modInverse(p); } catch (Exception ex) { if (ex.Message != info) { goto label1; } } RSA_D = d.ToHexString(); RSA_N = n.ToHexString(); RSA_E = e.ToHexString(); RSA_P = p.ToHexString(); RSA_Q = q.ToHexString(); RSA_DP = dp.ToHexString(); RSA_DQ = dq.ToHexString(); RSA_INVQ = invq.ToHexString(); }
public void initUpdate(string ver) { ver = ConvertTool.RemoveSpace(ver); if (ver.Length == 1) { ver = "0" + ver; } if (ver.Length != 2) { throw new ArgumentException("Please check \"ver\" in Security Domain xml file"); } if (this.kmcType == 0) { if (this.enc.Length != 32 || this.mac.Length != 32 || this.dek.Length != 32) { throw new ArgumentException("Please check \"enc or mac or dek\" in Security Domain xml file"); } } else { if (this.kmc.Length != 32) { throw new ArgumentException("Please check \"kmc\" in Security Domain xml file"); } } Random ran = new Random(); string ran8 = (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2") + (ran.Next(0, 0xFF)).ToString("X2"); this.hostRandom = ran8; string init = "8050" + ver + "0008" + ran8; this.send(init); if ((this.Response).Substring((this.Response).Length - 4) == "9000" && this.kmcType == 1)//cpg202 { /* * string data1 = this.Response.Substring(0, 12) + "F001" + this.Response.Substring(16, 4) + "000000000F01"; * string data2 = this.Response.Substring(0, 12) + "F002" + this.Response.Substring(16, 4) + "000000000F02"; * string data3 = this.Response.Substring(0, 12) + "F003" + this.Response.Substring(16, 4) + "000000000F03"; */ string data1 = this.Response.Substring(0, 4) + this.Response.Substring(8, 8) + "F001" + this.Response.Substring(0, 4) + this.Response.Substring(8, 8) + "0F01"; string data2 = this.Response.Substring(0, 4) + this.Response.Substring(8, 8) + "F002" + this.Response.Substring(0, 4) + this.Response.Substring(8, 8) + "0F02"; string data3 = this.Response.Substring(0, 4) + this.Response.Substring(8, 8) + "F003" + this.Response.Substring(0, 4) + this.Response.Substring(8, 8) + "0F03"; this.enc = ALG.DES.TriDesECBEn(data1, this.kmc); this.mac = ALG.DES.TriDesECBEn(data2, this.kmc); this.dek = ALG.DES.TriDesECBEn(data3, this.kmc); } else if ((this.Response).Substring((this.Response).Length - 4) == "9000" && this.kmcType == 2) //cpg212 { /* * string data1 = this.Response.Substring(0, 12) + "F001" + this.Response.Substring(16, 4) + "000000000F01"; * string data2 = this.Response.Substring(0, 12) + "F002" + this.Response.Substring(16, 4) + "000000000F02"; * string data3 = this.Response.Substring(0, 12) + "F003" + this.Response.Substring(16, 4) + "000000000F03"; */ string data1 = this.Response.Substring(8, 12) + "F001" + this.Response.Substring(8, 12) + "0F01"; string data2 = this.Response.Substring(8, 12) + "F002" + this.Response.Substring(8, 12) + "0F02"; string data3 = this.Response.Substring(8, 12) + "F003" + this.Response.Substring(8, 12) + "0F03"; this.enc = ALG.DES.TriDesECBEn(data1, this.kmc); this.mac = ALG.DES.TriDesECBEn(data2, this.kmc); this.dek = ALG.DES.TriDesECBEn(data3, this.kmc); } }
public void secApdu(string sapdu) { sapdu = ConvertTool.RemoveSpace(sapdu); if (sapdu.Length < 9) { this.send(sapdu); return; } int cla_t = System.Convert.ToInt32(sapdu.Substring(0, 2), 16); int cla = System.Convert.ToInt32(sapdu.Substring(0, 2), 16); int ins = System.Convert.ToInt32(sapdu.Substring(2, 2), 16); string p1p2 = sapdu.Substring(4, 4); int lc = System.Convert.ToInt32(sapdu.Substring(8, 2), 16); string data; if (sapdu.Length == 10) { data = ""; } else { data = sapdu.Substring(10, lc * 2); } string le; string mac = ""; if ((10 + lc * 2 + 2) == sapdu.Length) { le = sapdu.Substring(10 + lc * 2, 2); } else { le = ""; } int P1 = System.Convert.ToInt32(sapdu.Substring(4, 2), 16); if (DMType == 1) //gp2.1 { if (ins == 0xe6 && P1 != 0x20) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } lc = lc - 1; string s = calcToken(p1p2 + (lc.ToString("X2")) + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "80" + s; lc = data.Length / 2; } } else if (DMType == 2)//gp2.2 { if (ins == 0xe6 && P1 != 0x20) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } lc = lc - 1; string s = calcToken(p1p2 + (lc.ToString("X2")) + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "80" + s; lc = data.Length / 2; } else if (ins == 0xe4 && data.Length >= 14) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } string s = calcToken(p1p2 + (lc.ToString("X2")) + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "9E8180" + s; lc = data.Length / 2; } } else if (DMType == 3)//cmcc { if (ins == 0xe6 && P1 != 0x20) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } if (this.se_icc_id.Length != 20) { throw new ArgumentException("Please check \"seid\" in Security Domain xml file"); } lc = lc - 1; string s = calcToken(p1p2 + ((lc + se_icc_id.Length / 2 + 1).ToString("X2")) + (se_icc_id.Length / 2).ToString("X2") + se_icc_id + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "80" + s; lc = data.Length / 2; } else if (ins == 0xe4 && data.Length >= 14) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } if (this.se_icc_id.Length != 20) { throw new ArgumentException("Please check \"seid\" in Security Domain xml file"); } string s = calcToken(p1p2 + ((lc + se_icc_id.Length / 2 + 1).ToString("X2")) + (se_icc_id.Length / 2).ToString("X2") + se_icc_id + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "9E8180" + s; lc = data.Length / 2; } } else if (DMType == 4)//cuc { if (ins == 0xe6 && P1 != 0x20) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } if (this.se_icc_id.Length != 20) { throw new ArgumentException("Please check \"iccid\" in Security Domain xml file"); } lc = lc - 1; string s = calcToken(this.se_icc_id + p1p2 + (lc.ToString("X2")) + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "80" + s; lc = data.Length / 2; } else if (ins == 0xe4 && data.Length >= 14) { if (this.n.Length != 256 || this.d.Length != 256) { throw new ArgumentException("Please check \"rsa_n or rsa_d\" in Security Domain xml file"); } if (this.se_icc_id.Length != 20) { throw new ArgumentException("Please check \"iccid\" in Security Domain xml file"); } string s = calcToken(this.se_icc_id + p1p2 + (lc.ToString("X2")) + data.Substring(0, lc * 2), this.n, this.d); data = data.Substring(0, lc * 2) + "9E8180" + s; lc = data.Length / 2; } } int sl; if (ins == 0xA4) { this.securityLevel = "00"; } string apdu_head5 = cla.ToString("X2") + ins.ToString("X2") + p1p2 + lc.ToString("X2"); sl = System.Convert.ToInt32(this.securityLevel, 16); if (sl >= 1 && ins != 0x70) { cla |= 4; lc += 8; apdu_head5 = cla.ToString("X2") + ins.ToString("X2") + p1p2 + lc.ToString("X2"); if (this.scp == "01") { string icv = DES.TriDesCBCEn(this.macIni, this.skmac); mac = DES.TriDesCBCEn(Pading80(apdu_head5 + data, true), this.skmac, icv); mac = mac.Substring(mac.Length - 16, 16); this.macIni = mac; } else { mac = DES.TriDesMAC(Pading80(this.macIni + apdu_head5 + data, true), this.skmac); this.macIni = mac; } } if (sl >= 3 && ins != 0x70) { if (this.scp == "01") { lc -= 8; data = lc.ToString("X2") + data; data = DES.TriDesCBCEn(Pading80(data), this.skenc); lc = data.Length / 2 + 8; apdu_head5 = cla.ToString("X2") + ins.ToString("X2") + p1p2 + lc.ToString("X2"); } else { data = DES.TriDesCBCEn(Pading80(data, true), this.skenc); lc = data.Length / 2 + 8; apdu_head5 = cla.ToString("X2") + ins.ToString("X2") + p1p2 + lc.ToString("X2"); } } if (lc == 0) { this.send(apdu_head5); } else { this.send(apdu_head5 + data + mac + le); } }