c#.net中的简单加密算法

我试图从文件中读取文本nad加密它并将其传递给字符串格式的函数。 后来我也想解密它。

我尝试了以下但它没有做任何加密。任何人都建议任何简单的加密算法?

fileStream = store.OpenFile(strFilePath, FileMode.Open, FileAccess.Read); strEncryptedFileStream = Encoding.Unicode.GetBytes(fileStream.ToString()).ToString(); 

使用AES 。 这是一个帮助类。 使用“简单”,换句话说,容易破解加密是没有意义的。 要么你希望人们能够打破它,要么你不想打破它。 选择众所周知且经过测试的加密标准。

如果不满足,您可以根据需要修改以下示例。

 using System; using System.IO; using System.Security.Cryptography; using System.Text; using System.Diagnostics; namespace Common.Cryptography { ///  /// AES is a symmetric 256-bit encryption algorthm. /// Read more: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard ///  public static class AES { private const string _SALT = "g46dzQ80"; private const string _INITVECTOR = "OFRna74m*aze01xY"; private static byte[] _saltBytes; private static byte[] _initVectorBytes; static AES() { _saltBytes = Encoding.UTF8.GetBytes(_SALT); _initVectorBytes = Encoding.UTF8.GetBytes(_INITVECTOR); } ///  /// Encrypts a string with AES ///  /// Text to be encrypted /// Password to encrypt with /// Salt to encrypt with /// Needs to be 16 ASCII characters long /// An encrypted string public static string Encrypt(string plainText, string password, string salt = null, string initialVector = null) { return Convert.ToBase64String(EncryptToBytes(plainText, password, salt, initialVector)); } ///  /// Encrypts a string with AES ///  /// Text to be encrypted /// Password to encrypt with /// Salt to encrypt with /// Needs to be 16 ASCII characters long /// An encrypted string public static byte[] EncryptToBytes(string plainText, string password, string salt = null, string initialVector = null) { byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText); return EncryptToBytes(plainTextBytes, password, salt, initialVector); } ///  /// Encrypts a string with AES ///  /// Bytes to be encrypted /// Password to encrypt with /// Salt to encrypt with /// Needs to be 16 ASCII characters long /// An encrypted string public static byte[] EncryptToBytes(byte[] plainTextBytes, string password, string salt = null, string initialVector = null) { int keySize = 256; byte[] initialVectorBytes = string.IsNullOrEmpty(initialVector) ? _initVectorBytes : Encoding.UTF8.GetBytes(initialVector); byte[] saltValueBytes = string.IsNullOrEmpty(salt) ? _saltBytes : Encoding.UTF8.GetBytes(salt); byte[] keyBytes = new Rfc2898DeriveBytes(password, saltValueBytes).GetBytes(keySize / 8); using (RijndaelManaged symmetricKey = new RijndaelManaged()) { symmetricKey.Mode = CipherMode.CBC; using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes)) { using (MemoryStream memStream = new MemoryStream()) { using (CryptoStream cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write)) { cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length); cryptoStream.FlushFinalBlock(); return memStream.ToArray(); } } } } } ///  /// Decrypts an AES-encrypted string. ///  /// Text to be decrypted /// Password to decrypt with /// Salt to decrypt with /// Needs to be 16 ASCII characters long /// A decrypted string public static string Decrypt(string cipherText, string password, string salt = null, string initialVector = null) { byte[] cipherTextBytes = Convert.FromBase64String(cipherText.Replace(' ','+')); return Decrypt(cipherTextBytes, password, salt, initialVector).TrimEnd('\0'); } ///  /// Decrypts an AES-encrypted string. ///  /// Text to be decrypted /// Password to decrypt with /// Salt to decrypt with /// Needs to be 16 ASCII characters long /// A decrypted string public static string Decrypt(byte[] cipherTextBytes, string password, string salt = null, string initialVector = null) { int keySize = 256; byte[] initialVectorBytes = string.IsNullOrEmpty(initialVector) ? _initVectorBytes : Encoding.UTF8.GetBytes(initialVector); byte[] saltValueBytes = string.IsNullOrEmpty(salt) ? _saltBytes : Encoding.UTF8.GetBytes(salt); byte[] keyBytes = new Rfc2898DeriveBytes(password, saltValueBytes).GetBytes(keySize / 8); byte[] plainTextBytes = new byte[cipherTextBytes.Length]; using (RijndaelManaged symmetricKey = new RijndaelManaged()) { symmetricKey.Mode = CipherMode.CBC; using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes)) { using (MemoryStream memStream = new MemoryStream(cipherTextBytes)) { using (CryptoStream cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read)) { int byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length); return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount); } } } } } } } 

您的代码中没有任何内容可以加密。 你只是将它转换为Unicode。

你可以看一下这个问题 ,它提供了一个C#加密的好例子。

方法Enconding涉及文本编码(UTF-8,ANSI等),您应该使用加密算法,有几种方法可以做到这一点,一个简单的例子就是使用XOR加密。

看到这个:

 string XorEncryptDecryptText(string stringText, string stringKey){ // Variable that receives the result of processode encryption. string stringNewText = ""; // First we take the difference in size of the two strings. int diff = (stringText.Length - stringKey.Length); // If the size difference that we put stringKey with the same size as the XOR stringText that no errors occur. if (diff > 0){ // We calculate the rest and the amount of times we repeat the stringKey to equal sizes. int cont = (int)diff / stringKey.Length; int resto = diff % stringKey.Length; string stringKeyAux = stringKey; // At this point the stringText concatenate the stringKey to be equal sizes. for (int i = 0; i < cont; i++){ stringKeyAux += stringKey; } for (int i = 0; (i < resto); i++){ stringKeyAux += stringKey[i]; } stringKey = stringKeyAux; } // At this point piece of code is done the process of XOR. for (int i = 0; i < stringText.Length; i++){ int charValue = Convert.ToInt32(stringText[i]); int charKey = Convert.ToInt32(stringKey[i]); charValue ^= charKey; stringNewText += char.ConvertFromUtf32(charValue); } return stringNewText;} string XOREncriptDecriptFile(string FileName, string stringKey){ string stringAux = System.IO.File.ReadAllLines(FileName); return XorEncryptDecryptText(stringAux, stringKey);} 

此代码基于http://julioborges.p.ht/?p=769 。

为了更安全,请使用smdrager的答案