/// <summary> /// The Encryption method. /// </summary> /// <param name="plainText">The string to encrypt.</param> /// <param name="password">The password.</param> /// <returns>The encrypted string.</returns> public string EncryptString(string plainText, string password) { // 3Rijndael RijndaelProvider rp = RijndaelProvider.Instance; string rp1 = rp.EncryptString(plainText, password); string rp2 = rp.EncryptString(rp1, password); string rp3 = rp.EncryptString(rp2, password); // 3RC2 RC2Provider rc2 = RC2Provider.Instance; string rc2_1 = rc2.EncryptString(rp3, password); string rc2_2 = rc2.EncryptString(rc2_1, password); string rc2_3 = rc2.EncryptString(rc2_2, password); // 3DES DESProvider des = DESProvider.Instance; string des1 = des.EncryptString(rc2_3, password); string des2 = des.EncryptString(des1, password); string des3 = des.EncryptString(des2, password); // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.EncryptString(des3, password); string aes2 = aes.EncryptString(aes1, password); string aes3 = aes.EncryptString(aes2, password); return(aes3); }
/// <summary> /// The Encryption method. /// </summary> /// <param name="plainText">The string to encrypt.</param> /// <param name="password">The password.</param> /// <returns>The encrypted string.</returns> public ReturnStruct EncryptString(string plainText, string partPassword) { List <int> ril = new List <int>(); Random r = new Random(); for (int i = 0; i < 6; i++) { ril.Add(r.Next(0, 10)); } List <string> sl = NumberConverter.IntCollectionToStringList(ril); // 3DES DESProvider des = DESProvider.Instance; string des1 = des.EncryptString(plainText, partPassword + sl[0]); string des2 = des.EncryptString(des1, partPassword + sl[1]); string des3 = des.EncryptString(des2, partPassword + sl[2]); // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.EncryptString(des3, partPassword + sl[3]); string aes2 = aes.EncryptString(aes1, partPassword + sl[4]); string aes3 = aes.EncryptString(aes2, partPassword + sl[5]); ReturnStruct rs = new ReturnStruct(); rs.Result = aes3; rs.RandomKeys = ril.ToArray(); return(rs); // Use Casts: aes(aes(aes(des(des(des($content)))))); }
/// <summary> /// The Decryption method. /// </summary> /// <param name="Source">The string to decrypt.</param> /// <param name="password">The password.</param> /// <returns>The decrypted string.</returns> public string DecryptString(string Source, string password) { // string plain = testEncrypt.DecryptString(encText, password); // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.DecryptString(Source, password); string aes2 = aes.DecryptString(aes1, password); string aes3 = aes.DecryptString(aes2, password); // 3DES DESProvider des = DESProvider.Instance; string des1 = des.DecryptString(aes3, password); string des2 = des.DecryptString(des1, password); string des3 = des.DecryptString(des2, password); // 3RC2 RC2Provider rc2 = RC2Provider.Instance; string rc2_1 = rc2.DecryptString(des3, password); string rc2_2 = rc2.DecryptString(rc2_1, password); string rc2_3 = rc2.DecryptString(rc2_2, password); // 3Rijndael RijndaelProvider rp = RijndaelProvider.Instance; string rp1 = rp.DecryptString(rc2_3, password); string rp2 = rp.DecryptString(rp1, password); string rp3 = rp.DecryptString(rp2, password); return(rp3); }
/// <summary> /// The Decryption method. /// </summary> /// <param name="Source">The string to decrypt.</param> /// <param name="password">The password.</param> /// <returns>The decrypted string.</returns> public string DecryptString(string Source, string password) { // string plain = testEncrypt.DecryptString(encText, password); // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.DecryptString(Source, password); string aes2 = aes.DecryptString(aes1, password); string aes3 = aes.DecryptString(aes2, password); return(aes3); }
/// <summary> /// The Encryption method. /// </summary> /// <param name="plainText">The string to encrypt.</param> /// <param name="password">The password.</param> /// <returns>The encrypted string.</returns> public string EncryptString(string plainText, string password) { // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.EncryptString(plainText, password); string aes2 = aes.EncryptString(aes1, password); string aes3 = aes.EncryptString(aes2, password); return(aes3); // Use Casts: aes(aes(aes($content))); }
public static string Open(string filename, EncryptionType et) { BinaryReader br = new BinaryReader(new FileStream(filename, FileMode.Open)); List <byte> _in = new List <byte>(); while (br.BaseStream.Position < br.BaseStream.Length) { _in.Add(br.ReadByte()); } br.Close(); if (et == EncryptionType.AES) { return(Utilities.ByteToString(AESProvider.Decrypt(_in.ToArray()))); } if (et == EncryptionType.ASCII) { return(ASCIIProvider.Decrypt(Utilities.ByteToString(_in.ToArray()))); } if (et == EncryptionType.DES) { return(Utilities.ByteToString(DESProvider.Decrypt(_in.ToArray()))); } if (et == EncryptionType.L1F3) { return(L1F3Provider.Decrypt(_in.ToArray())); } if (et == EncryptionType.RC2) { return(Utilities.ByteToString(RC2Provider.Decrypt(_in.ToArray()))); } if (et == EncryptionType.Rijndael) { return(Utilities.ByteToString(RijndaelProvider.Decrypt(_in.ToArray()))); } if (et == EncryptionType.RSA) { return(Utilities.ByteToString(RSAProvider.Decrypt(_in.ToArray()))); } if (et == EncryptionType.TripleDES) { return(Utilities.ByteToString(TripleDESProvider.Decrypt(_in.ToArray()))); } if (et == EncryptionType.Xor) { return(AESProvider.Decrypt(Utilities.ByteToString(_in.ToArray()))); } throw new Exception("Invalid decryption type!"); }
/// <summary> /// The Decryption method. /// </summary> /// <param name="Source">The string to decrypt.</param> /// <param name="password">The password.</param> /// <returns>The decrypted string.</returns> public string DecryptString(string Source, string password, int[] rKeys) { // string plain = testEncrypt.DecryptString(encText, password); // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.DecryptString(Source, password + rKeys[5]); string aes2 = aes.DecryptString(aes1, password + rKeys[4]); string aes3 = aes.DecryptString(aes2, password + rKeys[3]); // 3DES DESProvider des = DESProvider.Instance; string des1 = des.DecryptString(aes3, password + rKeys[2]); string des2 = des.DecryptString(des1, password + rKeys[1]); string des3 = des.DecryptString(des2, password + rKeys[0]); return(des3); }
/// <summary> /// Does the actual saving. /// </summary> public override void Close() { // save the file to the filestream FileStream fs = new FileStream(Filename, FileMode.Create); if (Encryption == EncryptionType.AES) { fs.Write(AESProvider.Encrypt(Text.ToArray()), 0, Text.Count); } if (Encryption == EncryptionType.ASCII) { fs.Write(Utilities.StringToByte(ASCIIProvider.Encrypt(Utilities.ByteToString(Text.ToArray()))), 0, Text.Count); } if (Encryption == EncryptionType.DES) { fs.Write(DESProvider.Encrypt(Text.ToArray()), 0, Text.Count); } if (Encryption == EncryptionType.L1F3) { fs.Write(L1F3Provider.Encrypt(Utilities.ByteToString(Text.ToArray())), 0, Text.Count); } if (Encryption == EncryptionType.RC2) { fs.Write(RC2Provider.Encrypt(Text.ToArray()), 0, Text.Count); } if (Encryption == EncryptionType.Rijndael) { fs.Write(RijndaelProvider.Encrypt(Text.ToArray()), 0, Text.Count); } if (Encryption == EncryptionType.RSA) { fs.Write(RSAProvider.Encrypt(Text.ToArray()), 0, Text.Count); } if (Encryption == EncryptionType.TripleDES) { fs.Write(TripleDESProvider.Encrypt(Text.ToArray()), 0, Text.Count); } if (Encryption == EncryptionType.Xor) { fs.Write(Utilities.StringToByte(XorProvider.Encrypt(Utilities.ByteToString(Text.ToArray()))), 0, Text.Count); } fs.Close(); base.Close(); }
public static LuaValue Encrypt(LuaValue[] args) { string encType = (args[0] as LuaString).Text.ToLower(); string _in = args[1].ToString(); if (encType == "aes") { return(new LuaString(AESProvider.Encrypt(_in))); } else if (encType == "ascii") { // encrypt with first byte of key return(new LuaString(ASCIIProvider.Encrypt(_in))); } if (encType == "des") { return(new LuaString(DESProvider.Encrypt(_in))); } if (encType == "rc2") { return(new LuaString(RC2Provider.Encrypt(_in))); } if (encType == "rijndael") { return(new LuaString(RijndaelProvider.Encrypt(_in))); } if (encType == "rsa") { return(new LuaString(RSAProvider.Encrypt(_in))); } if (encType == "tripledes") { return(new LuaString(TripleDESProvider.Encrypt(_in))); } if (encType == "xor") { return(new LuaString(XorProvider.Encrypt(_in))); } throw new Exception("Unsuported encryption '" + encType + "'!"); }
/// <summary> /// 解密文本 /// </summary> /// <param name="AuthKey">登录授权码</param> /// <param name="EncryptText">密文</param> /// <param name="DecryptText">解密后的明文</param> /// <returns>0:成功 -1:未找到缓存的密钥 -2:解密失败 -100:用户未登录 </returns> public static int AESDecryptText(string AuthKey, string EncryptText, out string DecryptText) { DecryptText = ""; if (string.IsNullOrEmpty(EncryptText)) { return(0); } //null字符不解密 if (EncryptText.ToLower() == "null") { DecryptText = "null"; return(0); } string AESKey = "", AESIV = ""; int ret = GetAESEncryptKey(AuthKey, out AESKey, out AESIV); if (ret < 0) { if (ConfigHelper.GetConfigBool("DebugMode")) { DecryptText = EncryptText; } LogWriter.WriteLog("CryptHelper.AESDecryptText Error1! Ret=" + ret.ToString() + ",AuthKey=" + AuthKey + ",EncryptText=" + EncryptText); return(ret); } ret = AESProvider.DecryptText(EncryptText, AESKey, AESIV, out DecryptText); if (ret < 0) { LogWriter.WriteLog("CryptHelper.AESDecryptText Error2! Ret=" + ret.ToString() + ",AuthKey=" + AuthKey + ",EncryptText=" + EncryptText); return(-2); } return(0); }
/// <summary> /// The Encryption method. /// </summary> /// <param name="plainText">The string to encrypt.</param> /// <param name="password">The password.</param> /// <returns>The encrypted string.</returns> public string EncryptString(string plainText, string password) { // 3RC2 RC2Provider rc2 = RC2Provider.Instance; string rc2_1 = rc2.EncryptString(plainText, password); string rc2_2 = rc2.EncryptString(rc2_1, password); string rc2_3 = rc2.EncryptString(rc2_2, password); // 3DES DESProvider des = DESProvider.Instance; string des1 = des.EncryptString(rc2_3, password); string des2 = des.EncryptString(des1, password); string des3 = des.EncryptString(des2, password); // 3AES AESProvider aes = AESProvider.Instance; string aes1 = aes.EncryptString(des3, password); string aes2 = aes.EncryptString(aes1, password); string aes3 = aes.EncryptString(aes2, password); return(aes3); // Use Casts: rc2(rc2(rc2(aes(aes(aes(des(des(des($content))))))))); }
public void Encrypt(string key) { Data = AESProvider.Encrypt(Data, key); }
private ICipherService GetAESService(string aesKey, string aesIv) { return(new AESService(AESProvider.CBCPkcs5(aesKey, aesIv), _cryptoEncoding)); }
public static void Main(string[] args) { const int rsaKeySize = 728; //Smaller key sizes are easier to generate while testing var prsa = new PowerRSA(rsaKeySize, PowerRSA.PHashAlgorithm.SHA256); const string p = "this is n"; var c = prsa.EncryptStringWithPublicKey(p); Console.WriteLine(c); var d = prsa.DecryptStringWithPrivateKey(c); Console.WriteLine(d); var x = prsa.PublicKey; Console.WriteLine("RSAProvider Data: " + prsa.PrivateKey); Console.WriteLine("Exporting Private key to PKCS format:"); var priPemKey = RSAExtensions.ConvertPrivateKeyToPKCS(prsa); Console.WriteLine(priPemKey); Console.Write("PKCS Signing..."); const string signData = "Hello, World!"; var signature = RSAExtensions.SignWithPKCSPrivateKey(signData, prsa); Console.WriteLine(signature); Console.Write("Verifying..."); var verification = RSAExtensions.VerifyWithPKCSPublicKey(signData, signature, prsa); Console.WriteLine(verification); prsa.Dispose(); var pub = new PowerRSA(x, rsaKeySize, PowerRSA.PHashAlgorithm.SHA256); var e = pub.EncryptStringWithPublicKey(p); var d2 = prsa.DecryptStringWithPrivateKey(e); Console.WriteLine(d2); pub.Dispose(); Console.WriteLine(e); const string k = "1234"; var a1 = PowerAES.Encrypt(p, k); Console.WriteLine(a1); var d1 = PowerAES.Decrypt(a1, k); Console.WriteLine(d1); Console.WriteLine(PowerAES.SHA512Hash(p)); Console.WriteLine("Testing AES encryption on strings..."); var plaintextString = "Hi i like pie"; var password = "******"; var encryptedString = PowerAES.Encrypt(plaintextString, password); var decryptedString = PowerAES.Decrypt(encryptedString, password); Debug.Assert(decryptedString == plaintextString); Console.WriteLine("Testing AES encryption directly on bytes..."); var aesProvider = new AESProvider(); var salt = aesProvider.GenerateRandomBytes(24); var key = aesProvider.DeriveKeyFromPassphrase("monkey", salt); var iv = aesProvider.GenerateRandomBytes(16); //128-bit IV var plaintextBytes = Encoding.UTF8.GetBytes("Hi I am a monkey"); var encryptedBytes = aesProvider.EncryptBytes(plaintextBytes, key, iv); var decryptedBytes = aesProvider.DecryptBytes(iv, salt, encryptedBytes, key); Debug.Assert(decryptedBytes.SequenceEqual(plaintextBytes)); Console.WriteLine("Hash Test"); var hash = HashUtils.SHA512(k); Console.WriteLine(hash); Console.WriteLine("Demo completed"); Console.ReadKey(); }
public void when_key_is_invalid_should_throw_ArgumentException() { var keyBytes = new byte[] { 1, 2 }; Assert.Throws <ArgumentException>(() => AESProvider.New(keyBytes)); }
/// <summary> /// 用户登录 /// </summary> /// <param name="UserName">用户名</param> /// <param name="EncryptPassword">加密后的登录密码</param> /// <param name="DeviceCode">设备识别号</param> /// <param name="AuthKey">输出:授权码</param> /// <param name="ExtParams">扩展登录参数Json格式,包括AppCode、AppVersion、DeviceModel、DeviceOS、OSVersion、NetworkType /// 如:{"AppCode":"YSLRMAPP","AppVersion":43,"DeviceModel":"SM-G7108V","DeviceOS":"Android","OSVersion":"4.3","NetworkType":"ChinaMobile TD-SCDMA"} /// </param> /// <returns>0:登录成功 /// -1001:用户名或密码错误,登录失败 /// -1002:未能获取到对称加密密钥 /// -1003:设备号未在可登录的列表中登记 /// -1004:当前用户不允许从该设备号登录 /// -1005:登录失败 /// -1009:APP版本过低必须更新 /// </returns> public static int Login(string UserName, string EncryptPassword, string DeviceCode, string ExtParams, out string AuthKey) { LogWriter.WriteLog("UserLogin.LoginEx2:UserName="******",EncryptPassword="******",DeviceCode=" + DeviceCode + ",ExtParams=" + ExtParams); AuthKey = ""; Hashtable hs = string.IsNullOrEmpty(ExtParams) ? new Hashtable() : JsonConvert.DeserializeObject <Hashtable>(ExtParams); #region 判断是否符合最新版本要求 if (hs["AppCode"] != null) { int MinAppVersion = 0; if (hs["AppCode"].ToString() == "PBMSAPP") { MinAppVersion = ConfigHelper.GetConfigInt("MinAppVersion"); } else if (hs["AppCode"].ToString() == "PBMSAPP-iOS") { MinAppVersion = ConfigHelper.GetConfigInt("MinAppVersion-iOS"); } if (MinAppVersion > 0 && hs["AppVersion"] != null) { int AppVersion = 0; if (int.TryParse(hs["AppVersion"].ToString(), out AppVersion) && AppVersion < MinAppVersion) { LogWriter.WriteLog("UserLogin.LoginEx2: AppVersion too lower! UserName="******",DeviceCode=" + DeviceCode + ",AppVersion=" + AppVersion.ToString()); return(-1009); //APP版本过低必须更新 } } } #endregion #region 组织登录扩展属性 string ExtPropertys = ""; try { IList <UD_TableList> tables = UD_TableListBLL.GetModelList("Name='MCS_SYS.dbo.User_Online'"); if (tables.Count > 0) { IList <UD_ModelFields> models = UD_ModelFieldsBLL.GetModelList("Tableid='" + tables[0].ID.ToString() + "' AND Flag='N'"); foreach (UD_ModelFields item in models.OrderBy(p => p.Position)) { if (hs.ContainsKey(item.FieldName)) { ExtPropertys += hs[item.FieldName].ToString(); } ExtPropertys += "|"; } } } catch { } #endregion string cachekey = "EBMIF_DeviceCryptKey-" + DeviceCode; DeviceCryptKey key = null; #region 从数据库中加载保存的密钥 if (key == null) { string _keystr = ""; if (UserBLL.AppCryptKey_LoadKey(DeviceCode, out _keystr) == 0 && !string.IsNullOrEmpty(_keystr)) { try { key = JsonConvert.DeserializeObject <DeviceCryptKey>(_keystr); if (key != null) { DataCache.SetCache(cachekey, key, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration); } } catch { } } } #endregion int ret = 0; string Password = EncryptPassword; if (key == null) { LogWriter.WriteLog("UserLogin.LoginEx: Get DeviceCrytKey Null! UserName="******",DeviceCode=" + DeviceCode); return(-1002); //未能获取到对称加密密钥 } else { ret = AESProvider.DecryptText(EncryptPassword, key.AESKey, key.AESIV, out Password); if (ret < 0) { LogWriter.WriteLog("UserLogin.LoginEx! AESProvider.DecryptText Ret=" + ret.ToString() + ",DeviceCode=" + DeviceCode + ",EncryptPassword="******",AESKey=" + key.AESKey + ",AESIV=" + key.AESIV); return(-1002); } } ret = userlogin(UserName, Password, DeviceCode, ExtPropertys, out AuthKey); if (ConfigHelper.GetConfigBool("DebugMode") && key != null) { LogWriter.WriteLog("UserLogin.LoginEx:Login Return ret=" + ret.ToString() + ",DeviceCode=" + DeviceCode + ",AESKey=" + key.AESKey + ",AESIV=" + key.AESIV + ",AuthKey=" + AuthKey); } switch (ret) { case -1003: //设备号未在可登录的列表中登记 return(-1003); case -3: case -5: case -10: //当前用户不允许从该设备号登录 return(-1004); case -2: case -11: case -12: case -13: //用户名或密码错误,登录失败 return(-1001); case 1: //登录成功 return(0); default: //登录失败 return(-1005); } }
public void Encrypt(byte[] key) { Data = AESProvider.Encrypt(Data, key); }
public void Decrypt(byte[] key) { Data = AESProvider.Decrypt(Data, key); }
/// <summary> /// AES解密[ECB,Zeros] /// </summary> /// <param name="encryptedBytes">被加密的byte数组</param> /// <param name="key">Key,长度只能是[16,24,32]</param> /// <returns>解密后的byte数组</returns> // ReSharper disable once InconsistentNaming public static byte[] AESDecrypt(this byte[] encryptedBytes, byte[] key) { return(AESProvider.New(key).Decrypt(encryptedBytes)); }
/// <summary> /// AES加密[ECB,Zeros] /// </summary> /// <param name="bytes">原始byte数组</param> /// <param name="key">Key,长度只能是[16,24,32]</param> /// <returns>加密后的byte数组</returns> // ReSharper disable once InconsistentNaming public static byte[] AESEncrypt(this byte[] bytes, byte[] key) { return(AESProvider.New(key).Encrypt(bytes)); }
public static string EncryptwithAppaesobj(string metaStr) { var aesobj = new AESProvider(_appiv, _appkey); return(aesobj.Encrypt(metaStr)); }
public AESProvider BuildAESProvider(string key) { var keyBytes = key.GetBytes().GetMD5(); return(AESProvider.New(keyBytes)); }
public void when_key_is_empty_should_throw_ArgumentNullException() { byte[] keyBytes = new byte[0]; Assert.Throws <ArgumentNullException>(() => AESProvider.New(keyBytes)); }
public void Decrypt(string key) { Data = AESProvider.Decrypt(Data, key); }