Amr*_*har 0 asp.net encryption
任何人都可以给我一个使用.net和asp.net加密和解密图像的例子
当我将它作为二进制数据保存到sql server中时,我希望对图像进行加密.
包括这些名称空间
using System.IO;
using System.Security.Cryptography;
Run Code Online (Sandbox Code Playgroud)
对于加密创建以下功能:
private void EncryptFile(string inputFile, string outputFile)
{
try
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch
{
MessageBox.Show("Encryption failed!", "Error");
}
}
Run Code Online (Sandbox Code Playgroud)
对于解密创建以下功能:
private void DecryptFile(string inputFile, string outputFile)
{
{
string password = @"myKey123"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,
RMCrypto.CreateDecryptor(key, key),
CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
你可以这样打电话
EncryptFile(@"D:\OriginalImage.png", @"D:\VizioEncrypted.png"); //To Encrypt
DecryptFile(@"D:\VizioEncrypted.png", @"D:\VizioDecrypted.png"); //To Decrypt
Run Code Online (Sandbox Code Playgroud)
这会有所帮助