2017-10-06 23 views
0
public static class ImageEncryption 
    { 
     static string FILENAME = @"D:\Documents\Watermark\EBCDocument\EBC021700725665\test.pdf\Page1.jpg"; 
     static string ENCFILENAME = @"D:\Documents\Watermark\EBCDocument\EBC021700725665\test.pdf\Page1.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; 
    } 
} 
+0

你試過把它放在一個文件夾中並使用url來顯示。如果是圖像,即使加密,也會顯示。 – Amit

+1

您真正想要實現的是什麼?如果您需要顯示加密功能,那麼加密的用途是什麼? –

+0

我試圖在我想要的時候將我的加密圖像呈現給瀏覽器,如果任何機構下載該圖像,它應該將其作爲加密圖像下載。 –

回答

0

我可以成像的唯一方法是如果客戶端和服務器預先安排了唯一的密鑰,以便我可以解密它的客戶端。否則,加密它是沒有意義的,因爲任何人都可以訪問該頁面。但這只是意見。

+0

點是任何機構從網頁上看到的圖像,但下載該圖像時,它應該下載爲加密圖像 –

+0

好吧我想我得到你說的,你想能夠顯示圖像,但如果有人試圖下載它只會得到不可用的加密版本。在這種情況下,我不確定是否有可能,因爲爲了使瀏覽器顯示圖像,它必須是圖像而不是加密的。這不是我的專業知識,所以拿一點鹽就說吧。也許檢查複製安全網站,看起來很有希望。對不起,我無法提供更多幫助 – user8156140

相關問題