我试图加密文本并将加密文本保存为字符串 变量和文本文件。C#加密字符串变量
但令人惊讶的是,字符串中的加密文本与文本文件中的加密文本不一样,看起来不像 。
E.g串= bpAz1pcidPuCXbpO + 5RYvQ ==
textfile = n3Ö—"tû‚]ºNû”X½
我真的很喜欢我的字符串变量具有相同的数据,我 TextFile.txt的。我的意图是加密字符串变量而不是文本文件。 那么我如何加密我的字符串变量以与我的文本文件数据相同的方式进行加密?
这里是我下面的代码:
byte[] plainTextBytes = Encoding.UTF8.GetBytes("my name is calito");
MemoryStream memoryStream = new MemoryStream();
TripleDESCryptoServiceProvider cryptAlgorithm = new TripleDESCryptoServiceProvider();
CryptoStream csEncrypt = new CryptoStream(memoryStream, cryptAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
csEncrypt.Write(plainTextBytes, 0, plainTextBytes.Length);
//copying encrypted text to string
byte[] cipherTextBytes = memoryStream.ToArray();
string cipherText = Convert.ToBase64String(cipherTextBytes);
//copying incrypted text to text file
File.WriteAllBytes(@"G:\New Text Document.txt", memoryStream.GetBuffer());
而且,如果您想要内存流中的字节,请改用.ToArray()。 GetBuffer()可能会返回比.Length指示的更大的字节数。 – 2012-02-24 07:36:10