/// <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 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); }
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!"); }
public void RijndaelTest() { ISymmetricCrypto codec = new RijndaelProvider("test"); string org = "hello"; string res = codec.Encrypt(org); Assert.IsTrue(res != org); string org1 = codec.Decrypt(res); Assert.IsTrue(org == org1); codec.Dispose(); }
/// <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> /// Unpacks the response from bytes. /// </summary> /// <param name="requestBytes">The request bytes.</param> /// <param name="rsaProvider">The RSA provider.</param> /// <param name="aesProvider">The aes provider.</param> /// <returns></returns> public static VirtualSecuredResponseRawMessage UnpackResponseFromBytes(this byte[] requestBytes, RSACryptoServiceProvider rsaProvider, RijndaelProvider aesProvider) { // Byte[] composition: [Schema Version]{1}[UTC Stamp]{4}[Encrypted Body]{N}. try { rsaProvider.CheckNullObject(nameof(rsaProvider)); aesProvider.CheckNullObject(nameof(aesProvider)); requestBytes.CheckNullOrEmptyCollection(nameof(requestBytes)); var result = new VirtualSecuredResponseRawMessage { // Index1 SchemaVersion = Convert.ToInt32(requestBytes[0]) }; var currentIndex = 1; // Index2 var stampBytes = requestBytes.Read(_stampIndicationByteLength, ref currentIndex); result.Stamp = GetUtcStampFromOffsetBytes(stampBytes); ValidateStamp(result.Stamp); // Index5 var dataBytes = requestBytes.SubArray(currentIndex); result.Data = aesProvider.DecryptAes(dataBytes); return(result); } catch (Exception ex) { throw ex.Handle(); } }
/// <summary> /// Packs to bytes. /// </summary> /// <param name="responseMessage">The response message.</param> /// <param name="rsaProvider">The RSA provider.</param> /// <param name="aesProvider">The aes provider.</param> /// <returns></returns> public static byte[] PackToBytes(this VirtualSecuredResponseRawMessage responseMessage, RSACryptoServiceProvider rsaProvider, RijndaelProvider aesProvider) { // Byte[] composition: [Schema Version]{1}[UTC Stamp]{4}[Encrypted Body]{N}. try { rsaProvider.CheckNullObject(nameof(rsaProvider)); aesProvider.CheckNullObject(nameof(aesProvider)); responseMessage.CheckNullObject(nameof(responseMessage)); responseMessage.SchemaVersion = _schemaVersion; List <byte> result = new List <byte>(_packInitialCapacity) { // index1: version. Convert.ToByte(responseMessage.SchemaVersion) }; // index2: Stamp. result.AddRange(GetStampBytes(responseMessage.Stamp ?? DateTime.UtcNow)); // index3: Encrypted content result.AddRange(aesProvider.EncryptAes(responseMessage.Data)); return(result.ToArray()); } catch (Exception ex) { throw ex.Handle(); } }
/// <summary> /// Unpacks from bytes. /// </summary> /// <param name="requestBytes">The request bytes.</param> /// <param name="rsaProvider">The RSA provider.</param> /// <param name="aesProvider">The aes provider.</param> /// <returns></returns> public static VirtualSecuredRequestRawMessage UnpackRequestFromBytes(this byte[] requestBytes, RSACryptoServiceProvider rsaProvider, out RijndaelProvider aesProvider) { // Byte[] composition: [Schema Version]{1}[UTC Stamp]{4}[Encrypted Security Key Length Indication]{2+2}[Encrypted Security Key]{M+N}[Encrypted Body]{L}. aesProvider = null; try { rsaProvider.CheckNullObject(nameof(rsaProvider)); requestBytes.CheckNullOrEmptyCollection(nameof(requestBytes)); var result = new VirtualSecuredRequestRawMessage { // Index1 SchemaVersion = Convert.ToInt32(requestBytes[0]) }; var currentIndex = 1; // Index2 var stampBytes = requestBytes.Read(_stampIndicationByteLength, ref currentIndex); result.Stamp = GetUtcStampFromOffsetBytes(stampBytes); ValidateStamp(result.Stamp); // Index3 var primaryKeyLengthBytes = requestBytes.Read(_securityKeyIndicationByteLength, ref currentIndex); var primaryKeyLength = BitConverter.ToUInt16(primaryKeyLengthBytes, 0); var secondaryKeyLengthBytes = requestBytes.Read(_securityKeyIndicationByteLength, ref currentIndex); var secondaryKeyLength = BitConverter.ToUInt16(secondaryKeyLengthBytes, 0); // Index4 var primaryKeyBytes = requestBytes.Read(primaryKeyLength, ref currentIndex); result.SymmetricPrimaryKey = rsaProvider.Decrypt(primaryKeyBytes, true); var secondaryKeyBytes = requestBytes.Read(secondaryKeyLength, ref currentIndex); result.SymmetricSecondaryKey = secondaryKeyBytes == null ? null : rsaProvider.Decrypt(secondaryKeyBytes, true); aesProvider = new AesKeys { KeySize = dwKeySize, InitializationVector = result.SymmetricSecondaryKey, Key = result.SymmetricPrimaryKey }.CreateAesProvider(); // Index5 var dataBytes = requestBytes.SubArray(currentIndex); result.Data = aesProvider.DecryptAes(dataBytes); return(result); } catch (Exception ex) { throw ex.Handle(); } }
/// <summary> /// Packs to bytes. /// </summary> /// <param name="requestMessage">The request message.</param> /// <param name="rsaPublicKey">The RSA public key.</param> /// <param name="aesProvider">The aes provider.</param> /// <returns></returns> public static byte[] PackToBytes(this VirtualSecuredRequestRawMessage requestMessage, CryptoKey rsaPublicKey, RijndaelProvider aesProvider) { // Byte[] composition: [Schema Version]{1}[UTC Stamp]{4}[Encrypted Security Key Length Indication]{2+2}[Encrypted Security Key]{M+N}[Encrypted Body]{L}. try { rsaPublicKey.CheckNullObject(nameof(rsaPublicKey)); aesProvider.CheckNullObject(nameof(aesProvider)); requestMessage.CheckNullObject(nameof(requestMessage)); requestMessage.SymmetricPrimaryKey.CheckNullObject(nameof(requestMessage.SymmetricPrimaryKey)); requestMessage.Stamp.CheckNullObject(nameof(requestMessage.Stamp)); List <byte> result = new List <byte>(_packInitialCapacity) { // index1: version. Convert.ToByte(requestMessage.SchemaVersion) }; // index2: Stamp. result.AddRange(GetStampBytes(requestMessage.Stamp ?? DateTime.UtcNow)); // index3: Encrypted Security Key Length Indication. var encryptedSymmetricPrimaryKey = EncodingOrSecurityExtension.RsaEncrypt(requestMessage.SymmetricPrimaryKey, rsaPublicKey); var encryptedSymmetricSecondaryKey = (requestMessage.SymmetricSecondaryKey.ByteValue == null) ? null : EncodingOrSecurityExtension.RsaEncrypt(requestMessage.SymmetricSecondaryKey, rsaPublicKey); result.AddRange(BitConverter.GetBytes((UInt16)encryptedSymmetricPrimaryKey.Length)); result.AddRange(BitConverter.GetBytes((UInt16)encryptedSymmetricSecondaryKey.Length)); // index4: Encrypted Security Key result.AddRange(encryptedSymmetricPrimaryKey); if (encryptedSymmetricSecondaryKey.HasItem()) { result.AddRange(encryptedSymmetricSecondaryKey); } // index5: Encrypted content result.AddRange(aesProvider.EncryptAes(requestMessage.Data)); return(result.ToArray()); } catch (Exception ex) { throw ex.Handle(); } }