. NET images decrypted to BASE64

Source: Internet
Author: User
Tags base64 sha1

  1. #region Image Encryption
  2. <summary>
  3. Encrypt local files
  4. </Summary>
  5. <param name="InputName"> Path to read files </param>
  6. <param name="outname"> File path to output </param>
  7. <param name="key"> key </param>
  8. <returns></returns>
  9. public bool Encrptfile (string inputname, String outname, string key = "r4yqham%")
  10. {
  11. Through DES encryption
  12. DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
  13. Open a file through a stream
  14. FileStream fs = System.IO.File.OpenRead (inputname);
  15. Get file binary characters
  16. byte[] Inputbytearray = new Byte[fs.  Length];
  17. Read Stream file
  18. Fs. Read (Inputbytearray, 0, (int) fs. Length);
  19. Close the stream
  20. Fs. Close ();
  21. Get cryptographic string binary characters
  22. byte[] Keybytearray = Encoding.Default.GetBytes (key);
  23. Calculates the specified range hash value for a given byte group
  24. SHA1 ha = new sha1managed ();
  25. byte[] HB = Ha.computehash (keybytearray);
  26. An array of cryptographic keys
  27. byte[] SKey = new byte[8];
  28. Cryptographic variables
  29. byte[] SIV = new byte[8];
  30. for (int i = 0; I < 8; i++)
  31. Skey[i] = Hb[i];
  32. for (int i = 8; I < ; i++)
  33. SIV[I-8] = Hb[i];
  34. Get encryption Key
  35. des.  Key = SKey;
  36. Set the cryptographic initialization vector
  37. des.iv = SIV;
  38. MemoryStream ms = new MemoryStream ();
  39. CryptoStream cs = New CryptoStream (MS, Des.  CreateEncryptor (), cryptostreammode.write);
  40. Cs. Write (Inputbytearray, 0, inputbytearray.length);
  41. Cs. FlushFinalBlock ();
  42. fs = System.IO.File.OpenWrite (outname);
  43. foreach (Byte b in Ms. ToArray ())
  44. {
  45. Fs. WriteByte (b);
  46. }
  47. Fs. Close ();
  48. Cs. Close ();
  49. Ms. Close ();
  50. return true;
  51. }
  52. #endregion
  53. #region Image Decryption
  54. <summary>
  55. Decrypting the picture returns a Base64
  56. </Summary>
  57. <param name="filename"></param>
  58. <param name="key"></param>
  59. <returns></returns>
  60. public string Readenimage (string filename, string key = "r4yqham%")
  61. {
  62. Through des decryption
  63. DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
  64. Read a file through a stream
  65. FileStream fs = File.openread (this.  Filepath.text);
  66. Get file binary characters
  67. byte[] Inputbytearray = new Byte[fs.  Length];
  68. Reading stream files
  69. Fs. Read (Inputbytearray, 0, (int) fs. Length);
  70. Close the stream
  71. Fs. Close ();
  72. Key array
  73. byte[] Keybytearray = Encoding.Default.GetBytes ("r4yqham%");
  74. Defining a Hash variable
  75. SHA1 ha = new sha1managed ();
  76. Calculates the specified range hash value for a given byte group
  77. byte[] HB = Ha.computehash (keybytearray);
  78. An array of cryptographic keys
  79. byte[] SKey = new byte[8];
  80. Cryptographic variables
  81. byte[] SIV = new byte[8];
  82. for (int i = 0; I < 8; i++)
  83. Skey[i] = Hb[i];
  84. for (int i = 8; I < ; i++)
  85. SIV[I-8] = Hb[i];
  86. Get encryption Key
  87. des.  Key = SKey;
  88. Cryptographic variables
  89. des.iv = SIV;
  90. MemoryStream ms = new MemoryStream ();
  91. CryptoStream cs = New CryptoStream (MS, Des.  CreateDecryptor (), cryptostreammode.write);
  92. Cs. Write (Inputbytearray, 0, inputbytearray.length);
  93. Cs. FlushFinalBlock ();
  94. Byte[] by = Ms.  ToArray ();
  95. String base64 = convert.tobase64string (by);
  96. Fs. Close ();
  97. Cs. Close ();
  98. Ms. Close ();
  99. Return "data:image/png;base64," + base64;
  100. }
  101. #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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.