public void DeserializeUseMd5Test() { var crypt = CryptographyFactory.Create("md5"); var serializer = new BinaryCryptoSerializer(crypt); #region var bytes = new byte[] { 64, 110, 111, 194, 121, 248, 123, 58, 12, 137, 116, 110, 166, 232, 5, 110 }; #endregion var obj = serializer.Deserialize <BinaryCryptoData>(bytes); Assert.IsNotNull(obj); }
public void TestDES() { var c = CryptographyFactory.Create(CryptoAlgorithm.DES); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("NcnuOjI/5dI=", Convert.ToBase64String(bytes)); }
public void TestTDES() { var c = CryptographyFactory.Create(CryptoAlgorithm.TripleDES); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("whjkbt+wvN8=", Convert.ToBase64String(bytes)); }
public void TestAES() { var c = CryptographyFactory.Create(CryptoAlgorithm.AES); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("LA+IcPGDn5F1/e5oHXfRBw==", Convert.ToBase64String(bytes)); }
public void TestRC4() { var c = CryptographyFactory.Create(CryptoAlgorithm.RC4); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("hUWUuUSlmg==", Convert.ToBase64String(bytes)); }
public void TestSHA384() { var c = CryptographyFactory.Create(CryptoAlgorithm.SHA384); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("JIojBzlQO8t8h/BmKwHHbKhV5l1it/tRuPnRvJC2KMXBv8rvTSpiQLSOc63zJ4vW", Convert.ToBase64String(bytes)); }
public void TestSHA512() { var c = CryptographyFactory.Create(CryptoAlgorithm.SHA512); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("zdRjlmz3FE4LLBlcsaOBrPwTDQ4SBnbIlTXfTiSlB16+GDegxsq9xpNUgwv9oetpnmOWUjs+7vWWupEXNmfkXQ==", Convert.ToBase64String(bytes)); }
public void TestSHA1() { var c = CryptographyFactory.Create(CryptoAlgorithm.SHA1); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("Ce+S8twtqAqw3IunLaJwGJsbA0k=", Convert.ToBase64String(bytes)); }
public void TestSHA256() { var c = CryptographyFactory.Create(CryptoAlgorithm.SHA256); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("ajYzysdQ8Dt4CGBSHAQVf0jSO62d8A8rsUEXAYkWhA8=", Convert.ToBase64String(bytes)); }
public void TestMD5() { var c = CryptographyFactory.Create(CryptoAlgorithm.MD5); var bytes = c.Encrypt("fireasy", Encoding.UTF8); Assert.AreEqual("P2OjkZ/a9Oe0Yss1rRnlzw==", Convert.ToBase64String(bytes)); }
public void TestDSAGenerateKey() { var encrypt = CryptographyFactory.Create(CryptoAlgorithm.DSA) as AsymmetricCrypto; Console.WriteLine(encrypt.GeneratePrivateKey()); Console.WriteLine(encrypt.GeneratePublicKey()); }
public void BinaryCryptoSerializerTest() { var crypt = CryptographyFactory.Create("des"); var serializer = new BinaryCryptoSerializer(crypt); Assert.IsNotNull(serializer); }
private ICryptoProvider CreateCryptoProvider() { var keybuffer = new byte[] { 45, 66, 124, 187, 22, 56, 90, 242 }; var crypt = CryptographyFactory.Create(CryptoAlgorithm.DES); (crypt as SymmetricCrypto).CryptKey = keybuffer; return(crypt); }
protected override byte[] EncodeDataToBytes(CodedData data) { var des = CryptographyFactory.Create(CryptoAlgorithm.DES) as SymmetricCrypto; des.SetKey(KEY); return(des.Encrypt(data.Data)); }
/// <summary> /// 验证授权码是否正确。 /// </summary> /// <param name="option"></param> /// <returns></returns> public bool VerifyLicenseKey(LicenceVerifyOption option) { Guard.ArgumentNull(option.PublicKey, nameof(option.PublicKey)); Guard.ArgumentNull(option.LicenseKey, nameof(option.LicenseKey)); if (option.VerifyLocalKey) { Guard.ArgumentNull(option.AppKey, nameof(option.AppKey)); Guard.ArgumentNull(option.LocalKey, nameof(option.LocalKey)); } var crypt = CryptographyFactory.Create(option.EncryptKind.ToString()) as AsymmetricCrypto; crypt.PublicKey = option.PublicKey; if (option.VerifyLocalKey) { var bytes = FromBase64String(option.LocalKey); if (bytes == null) { return(false); } var data = new byte[bytes.Length - 2]; Array.Copy(bytes, 2, data, 0, data.Length); if (!Enum.IsDefined(typeof(MachineKeyKinds), (int)bytes[0]) || !Enum.IsDefined(typeof(LocalKeyEncryptKinds), (int)bytes[1])) { return(false); } var mkKind = (MachineKeyKinds)bytes[0]; var mkEncryptKind = (LocalKeyEncryptKinds)bytes[1]; var localKey = GetLocalKey(new LocalKeyOption { AppKey = option.AppKey, MachineKeyKind = mkKind, EncryptKind = mkEncryptKind }); if (localKey != option.LocalKey) { return(false); } } var licenseBytes = FromBase64String(option.LicenseKey); if (licenseBytes == null) { return(false); } return(crypt.VerifySignature(Encoding.ASCII.GetBytes(option.LocalKey), licenseBytes)); }
/// <summary> /// 获取授权码。 /// </summary> /// <param name="option"></param> /// <returns></returns> public string GetLicenseKey(LicenceGenerateOption option) { Guard.ArgumentNull(option.PrivateKey, nameof(option.PrivateKey)); Guard.ArgumentNull(option.LocalKey, nameof(option.LocalKey)); var crypt = CryptographyFactory.Create(option.EncryptKind.ToString()) as AsymmetricCrypto; crypt.PrivateKey = option.PrivateKey; return(Convert.ToBase64String(crypt.CreateSignature(Encoding.ASCII.GetBytes(option.LocalKey)))); }
public void TestDSASign() { var encrypt = CryptographyFactory.Create(CryptoAlgorithm.DSA) as AsymmetricCrypto; encrypt.PublicKey = DSA_publicKey; encrypt.PrivateKey = DSA_privateKey; var bytes = encrypt.CreateSignature(Encoding.GetEncoding(0).GetBytes("1234")); var ver = encrypt.VerifySignature(Encoding.GetEncoding(0).GetBytes("1234"), bytes); Assert.IsTrue(ver); }
public void TestRijndaelEncrypt() { var encrypt = CryptographyFactory.Create(CryptoAlgorithm.Rijndael) as SymmetricCrypto; encrypt.SetKey("faib studio"); var bytes = encrypt.Encrypt("1234", Encoding.GetEncoding(0)); Console.WriteLine(bytes.Length); var s = encrypt.Decrypt(bytes, Encoding.GetEncoding(0)); Assert.AreEqual("1234", s); }
protected override CodedData DecodeDataFromBytes(byte[] data) { if (data.Length == 0) { return(null); } var des = CryptographyFactory.Create(CryptoAlgorithm.DES) as SymmetricCrypto; des.SetKey(KEY); return(new CodedData(des.Decrypt(data))); }
public void TestRSAEncrypt() { var encrypt = CryptographyFactory.Create(CryptoAlgorithm.RSA) as AsymmetricCrypto; encrypt.PublicKey = RSA_publicKey; encrypt.PrivateKey = RSA_privateKey; var bytes = encrypt.Encrypt("1234", Encoding.GetEncoding(0)); Console.WriteLine(bytes.Length); var s = encrypt.Decrypt(bytes, Encoding.GetEncoding(0)); Assert.AreEqual("1234", s); }
/// <summary> /// 从数据中解密出连接字符串。 /// </summary> /// <param name="data"></param> /// <returns></returns> internal static string Decrypt(string data) { var bytes = ToBytes(data); var keys = new[] { bytes[2], bytes[0], bytes[bytes.Length - 3], bytes[1], bytes[bytes.Length - 1], bytes[bytes.Length - 2], bytes[3], bytes[bytes.Length - 4] }; var encrypt = CryptographyFactory.Create(CryptoAlgorithm.RC2) as SymmetricCrypto; encrypt.CryptKey = keys; var bd = new byte[bytes.Length - 8]; Array.Copy(bytes, 4, bd, 0, bd.Length); return(encrypt.Decrypt(bd, Encoding.GetEncoding(0))); }
public void SerializeUseMd5Test() { var crypt = CryptographyFactory.Create("md5"); var serializer = new BinaryCryptoSerializer(crypt); var obj = new BinaryCryptoData { Age = 12, Name = "huangxd", Birthday = DateTime.Parse("1982-9-20") }; var bytes = serializer.Serialize(obj); Assert.IsNotNull(bytes); }
public void TestMD5Encrypt() { var str = @"<xml> <head> <requestCode>0104</requestCode> <authticCode>001</authticCode> <sign></sign> <randomStr>16c8924a58ad40a09997d91f30d7dc2d</randomStr> </head> <msg> <row EmpCode=""""></row> </msg> </xml>0eb4c393-3e66-44e0-88cf-d5cad6ed0da0"; var encrypt = CryptographyFactory.Create(CryptoAlgorithm.MD5); Console.WriteLine(encrypt.Encrypt(str.Replace("\r", ""), Encoding.ASCII).ToHex()); }
/// <summary> /// 对连接字符串进行加密。 /// </summary> /// <param name="connectionString"></param> /// <returns></returns> internal static string Encrypt(string connectionString) { var encrypt = CryptographyFactory.Create(CryptoAlgorithm.RC2) as SymmetricCrypto; var keys = new byte[8]; var rnd = new Random(); rnd.NextBytes(keys); encrypt.CryptKey = keys; var data = encrypt.Encrypt(connectionString, Encoding.GetEncoding(0)); var array = new List <byte>(); array.AddRange(new [] { keys[1], keys[3], keys[0], keys[6] }); array.AddRange(data); array.AddRange(new[] { keys[7], keys[2], keys[5], keys[4] }); return(ToHex(array.ToArray())); }
private static string DencryptConnection(string connectionStr) { if (connectionStr.StartsWith("XE/")) { var encryptStr = connectionStr.Substring(2); var bytes = new byte[encryptStr.Length / 2]; for (var i = 0; i < encryptStr.Length; i += 2) { bytes[i / 2] = byte.Parse(encryptStr.Substring(i, 2), NumberStyles.HexNumber); } var encrypt = CryptographyFactory.Create(CryptoAlgorithm.RC2) as SymmetricCrypto; return(Encoding.GetEncoding(0).GetString(encrypt.Decrypt(bytes))); } return(connectionStr); }
/// <summary> /// 获取本地请求码。 /// </summary> /// <param name="option"></param> /// <returns></returns> public string GetLocalKey(LocalKeyOption option) { var machineKey = MachineKeyHelper.GetMachineKey(option.MachineKeyKind); if (string.IsNullOrEmpty(machineKey)) { throw new AppLicenseException($"未能获取 {option.MachineKeyKind} 的相关信息。", null); } Guard.ArgumentNull(option.AppKey, nameof(option.AppKey)); var crypt = CryptographyFactory.Create(option.EncryptKind.ToString()); var bytes = crypt.Encrypt($"_uY9bd{machineKey}-9b83X{option.AppKey}_5Y946Z", Encoding.ASCII); var data = new byte[bytes.Length + 2]; bytes.CopyTo(data, 2); data[0] = (byte)option.MachineKeyKind; data[1] = (byte)option.EncryptKind; return(Convert.ToBase64String(data)); }
/// <summary> /// 创建密文。 /// </summary> /// <param name="password"></param> /// <returns></returns> public string Create(string password) { var md5 = CryptographyFactory.Create("MD5"); return(md5.Encrypt(string.Format(MD5_SALT, password), Encoding.UTF8).ToHex()); }
public void TestSHA512Encrypt() { var encrypt = CryptographyFactory.Create(CryptoAlgorithm.SHA512); Console.WriteLine(encrypt.Encrypt("1234", Encoding.GetEncoding(0)).ToHex()); }
private static string EncryptKey(string key) { var crypt = CryptographyFactory.Create(CryptoAlgorithm.SHA1); return(crypt.Encrypt(key, Encoding.UTF8).ToHex()); }
public void DeserializeTest() { var crypt = CryptographyFactory.Create("des"); var serializer = new BinaryCryptoSerializer(crypt); #region var bytes = new byte[] { 117, 95, 97, 41, 160, 1, 189, 29, 162, 14, 160, 175, 84, 107, 87, 247, 155, 87, 129, 58, 66, 191, 30, 90, 1, 137, 109, 118, 182, 214, 144, 40, 164, 169, 148, 64, 193, 58, 136, 13, 192, 106, 55, 252, 182, 139, 33, 12, 87, 85, 131, 181, 116, 164, 36, 36, 52, 243, 199, 197, 237, 213, 74, 73, 78, 180, 205, 186, 58, 59, 211, 47, 122, 162, 156, 22, 230, 174, 15, 210, 108, 225, 183, 76, 160, 176, 210, 88, 168, 7, 210, 179, 14, 97, 131, 5, 56, 214, 77, 20, 9, 2, 209, 166, 1, 137, 109, 118, 182, 214, 144, 40, 82, 251, 42, 118, 31, 182, 151, 87, 0, 72, 127, 33, 192, 235, 90, 196, 241, 19, 70, 11, 9, 138, 214, 54, 38, 209, 252, 192, 99, 48, 82, 74, 150, 247, 109, 228, 218, 40, 94, 190, 97, 38, 96, 158, 70, 239, 112, 40, 12, 144, 223, 161, 20, 38, 247, 236, 249, 98, 118, 39, 51, 174, 148, 137, 58, 13, 64, 16, 195, 162, 174, 211, 73, 72, 169, 153, 102, 147, 227, 191, 80, 62, 20, 172, 28, 17, 82, 12, 217, 173, 240, 143, 114, 39, 218, 7, 32, 165, 143, 23, 230, 217, 182, 94, 185, 196, 207, 114, 175, 154, 225, 22, 90, 40, 204, 187, 133, 39, 98, 38, 169, 80, 104, 94, 117, 95, 187, 187, 152, 107, 125, 125, 205, 14, 154, 138, 20, 238, 89, 97, 151, 244, 96, 238, 219, 249, 140, 149, 170, 90, 175, 236, 254, 98, 91, 173, 71, 209, 87, 122, 47, 155, 174, 91, 62, 141, 88, 92, 156, 114, 93, 174, 217, 28, 118, 42, 154, 190, 242, 135, 98, 129, 129, 95, 166, 107, 223, 191, 2, 252, 151, 187, 145, 63, 109, 138, 153, 154, 246, 180 }; #endregion var obj = serializer.Deserialize <BinaryCryptoData>(bytes); Assert.IsNotNull(obj); }