- #region Image Encryption
- <summary>
- Encrypt local files
- </Summary>
- <param name="InputName"> Path to read files </param>
- <param name="outname"> File path to output </param>
- <param name="key"> key </param>
- <returns></returns>
- public bool Encrptfile (string inputname, String outname, string key = "r4yqham%")
- {
- Through DES encryption
- DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
- Open a file through a stream
- FileStream fs = System.IO.File.OpenRead (inputname);
- Get file binary characters
- byte[] Inputbytearray = new Byte[fs. Length];
- Read Stream file
- Fs. Read (Inputbytearray, 0, (int) fs. Length);
- Close the stream
- Fs. Close ();
- Get cryptographic string binary characters
- byte[] Keybytearray = Encoding.Default.GetBytes (key);
- Calculates the specified range hash value for a given byte group
- SHA1 ha = new sha1managed ();
- byte[] HB = Ha.computehash (keybytearray);
- An array of cryptographic keys
- byte[] SKey = new byte[8];
- Cryptographic variables
- byte[] SIV = new byte[8];
- for (int i = 0; I < 8; i++)
- Skey[i] = Hb[i];
- for (int i = 8; I < ; i++)
- SIV[I-8] = Hb[i];
- Get encryption Key
- des. Key = SKey;
- Set the cryptographic initialization vector
- des.iv = SIV;
- MemoryStream ms = new MemoryStream ();
- CryptoStream cs = New CryptoStream (MS, Des. CreateEncryptor (), cryptostreammode.write);
- Cs. Write (Inputbytearray, 0, inputbytearray.length);
- Cs. FlushFinalBlock ();
- fs = System.IO.File.OpenWrite (outname);
- foreach (Byte b in Ms. ToArray ())
- {
- Fs. WriteByte (b);
- }
- Fs. Close ();
- Cs. Close ();
- Ms. Close ();
- return true;
- }
- #endregion
- #region Image Decryption
- <summary>
- Decrypting the picture returns a Base64
- </Summary>
- <param name="filename"></param>
- <param name="key"></param>
- <returns></returns>
- public string Readenimage (string filename, string key = "r4yqham%")
- {
- Through des decryption
- DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
- Read a file through a stream
- FileStream fs = File.openread (this. Filepath.text);
- Get file binary characters
- byte[] Inputbytearray = new Byte[fs. Length];
- Reading stream files
- Fs. Read (Inputbytearray, 0, (int) fs. Length);
- Close the stream
- Fs. Close ();
- Key array
- byte[] Keybytearray = Encoding.Default.GetBytes ("r4yqham%");
- Defining a Hash variable
- SHA1 ha = new sha1managed ();
- Calculates the specified range hash value for a given byte group
- byte[] HB = Ha.computehash (keybytearray);
- An array of cryptographic keys
- byte[] SKey = new byte[8];
- Cryptographic variables
- byte[] SIV = new byte[8];
- for (int i = 0; I < 8; i++)
- Skey[i] = Hb[i];
- for (int i = 8; I < ; i++)
- SIV[I-8] = Hb[i];
- Get encryption Key
- des. Key = SKey;
- Cryptographic variables
- des.iv = SIV;
- MemoryStream ms = new MemoryStream ();
- CryptoStream cs = New CryptoStream (MS, Des. CreateDecryptor (), cryptostreammode.write);
- Cs. Write (Inputbytearray, 0, inputbytearray.length);
- Cs. FlushFinalBlock ();
- Byte[] by = Ms. ToArray ();
- String base64 = convert.tobase64string (by);
- Fs. Close ();
- Cs. Close ();
- Ms. Close ();
- Return "data:image/png;base64," + base64;
- }
- #endregion
Description: The encryption code for the image encryption after storing a copy, if you need to delete the image before the encryption to modify the code itself
The decryption part is returned after decryption is not a picture but a base64 this is what I have encountered in the Web development project required. Hope to help you, thank you for your support!
. NET images decrypted to BASE64