public byte[] Crypt(byte[] Source) { using (RSACryptoServiceProvider R = new RSACryptoServiceProvider()) { R.PersistKeyInCsp = false; R.ImportParameters(Params); using (RijndaelManaged RM = new RijndaelManaged()) { RM.GenerateIV(); RM.GenerateKey(); byte[] EIV = R.Encrypt(RM.IV, true); byte[] EPW = R.Encrypt(RM.Key, true); using (RijndaelManagedTransform Enc = (RijndaelManagedTransform)RM.CreateEncryptor()) { using (MemoryStream MS = new MemoryStream()) { using (BinaryWriter BW = new BinaryWriter(MS)) { WB(BW, EIV); WB(BW, EPW); BW.Flush(); using (CryptoStream CS = new CryptoStream(MS, Enc, CryptoStreamMode.Write)) { CS.Write(Source, 0, Source.Length); CS.FlushFinalBlock(); } return(MS.ToArray()); } } } } } }
public byte[] Decrypt(byte[] Data) { if (!HasPrivate) { throw new Exception("Can't decrypt without private key!"); } using (RSACryptoServiceProvider R = new RSACryptoServiceProvider()) { R.PersistKeyInCsp = false; R.ImportParameters(Params); using (RijndaelManaged RM = new RijndaelManaged()) { using (MemoryStream MS = new MemoryStream(Data, false)) { using (BinaryReader BR = new BinaryReader(MS)) { RM.IV = R.Decrypt(BR.ReadBytes(BR.ReadInt32()), true); RM.Key = R.Decrypt(BR.ReadBytes(BR.ReadInt32()), true); using (RijndaelManagedTransform Dec = (RijndaelManagedTransform)RM.CreateDecryptor()) { using (CryptoStream CS = new CryptoStream(MS, Dec, CryptoStreamMode.Read)) { return(CS.ReadToEnd()); } } } } } } }
public static string Decrypt(string text) { if (string.IsNullOrEmpty(text)) { return(string.Empty); } string result = string.Empty; List <byte> encryptedBytes = new List <byte>(Convert.FromBase64String(text)); byte[] iv = encryptedBytes.GetRange(3, 16).ToArray(); encryptedBytes.RemoveRange(3, 16); byte[] key = encryptedBytes.GetRange(2, 32).ToArray(); encryptedBytes.RemoveRange(2, 32); int length = encryptedBytes.Count; byte[] decryptedBytes = new byte[length]; using (RijndaelManaged provider = new RijndaelManaged()) { using (MemoryStream memoryStream = new MemoryStream(encryptedBytes.ToArray())) { encryptedBytes.Clear(); using (RijndaelManagedTransform transform = provider.CreateDecryptor((byte[])key.Clone(), (byte[])iv.Clone()) as RijndaelManagedTransform) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read)) { cryptoStream.Read(decryptedBytes, 0, length); result = Encoding.ASCII.GetString(decryptedBytes).Trim(new char[] { '\0' }); result = result.Replace("567@vcxz_", string.Empty); result = result.Replace("=C#I$Th3L4nG", string.Empty); } } } } return(result); }
public static string Encrypt(string text) { if (string.IsNullOrEmpty(text)) { return(string.Empty); } string result = string.Empty; text = string.Format("567@vcxz_{0}=C#I$Th3L4nG", text); byte[] encodedBytes = Encoding.ASCII.GetBytes(text); int length = encodedBytes.Length; using (RijndaelManaged provider = new RijndaelManaged()) { using (RijndaelManagedTransform transform = provider.CreateEncryptor() as RijndaelManagedTransform) { using (MemoryStream memoryStream = new MemoryStream(length)) { using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write)) { cryptoStream.Write(encodedBytes, 0, length); cryptoStream.FlushFinalBlock(); List <byte> encryptedBytes = new List <byte>(); encryptedBytes.AddRange(memoryStream.ToArray()); encryptedBytes.InsertRange(2, (byte[])provider.Key.Clone()); encryptedBytes.InsertRange(3, (byte[])provider.IV.Clone()); result = Convert.ToBase64String(encryptedBytes.ToArray()); encryptedBytes.Clear(); }; }; }; } return(result); }
/// <summary> /// Extends TransformFinalBlock so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// rijndaelmanagedtransform.TransformFinalBlock(inputBuffer); /// </example> /// </summary> public static Byte[] TransformFinalBlock(this RijndaelManagedTransform rijndaelmanagedtransform, Byte[] inputBuffer) { if (rijndaelmanagedtransform == null) { throw new ArgumentNullException("rijndaelmanagedtransform"); } if (inputBuffer == null) { throw new ArgumentNullException("inputBuffer"); } return(rijndaelmanagedtransform.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length)); }
/// <summary> /// Extends TransformBlock so that buffer offset of 0 and call to Array.Length are not needed. /// <example> /// rijndaelmanagedtransform.TransformBlock(inputBuffer, outputBuffer, outputOffset); /// </example> /// </summary> public static Int32 TransformBlock(this RijndaelManagedTransform rijndaelmanagedtransform, Byte[] inputBuffer, Byte[] outputBuffer, Int32 outputOffset) { if (rijndaelmanagedtransform == null) { throw new ArgumentNullException("rijndaelmanagedtransform"); } if (inputBuffer == null) { throw new ArgumentNullException("inputBuffer"); } return(rijndaelmanagedtransform.TransformBlock(inputBuffer, 0, inputBuffer.Length, outputBuffer, outputOffset)); }