public static class ImageEncryption
{
static string FILENAME = @"D:DocumentsWatermarkEBCDocumentEBC021700725665test.pdfPage1.jpg";
static string ENCFILENAME = @"D:DocumentsWatermarkEBCDocumentEBC021700725665test.pdfPage1.jpg";
public static void ImageTripleDESCrypto()
{
//Create instance of DES
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
//Generate IV and Key
des.GenerateIV();
des.GenerateKey();
//Set Encryption mode
des.Mode = CipherMode.ECB;
//Read
FileStream fileStream = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
MemoryStream ms = new MemoryStream();
fileStream.CopyTo(ms);
//Store header in byte array (we will used this after encryption)
var header = ms.ToArray().Take(54).ToArray();
//Take rest from stream
var imageArray = ms.ToArray().Skip(54).ToArray();
//Create encryptor
var enc = des.CreateEncryptor();
//Encrypt image
var encimg = enc.TransformFinalBlock(imageArray, 0, imageArray.Length);
//Combine header and encrypted image
var image = Combine(header, encimg);
//Write encrypted image to disk
fileStream.Close();
File.WriteAllBytes(ENCFILENAME, image);
}
public static byte[] Combine(byte[] first, byte[] second)
{
byte[] ret = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
return ret;
}
}
我能映像的唯一方法是,如果客户端和服务器已经对唯一的键进行了预先安排,以便我成为解密它的客户端。否则,对其进行加密是没有意义的,因为它对有能力访问该页面的任何人都将是公开的。这只是意见。