Implement ASP. NET data encryption using symmetric encryption algorithms

Source: Internet
Author: User

How does symmetric encryption algorithm implement ASP. NET data encryption? The following is a detailed description:

The following is the code for implementing ASP. NET data encryption in C # of symmetric encryption algorithm. You can change different algorithms as needed. The Rijndael algorithm is used as an example in this article:

 
 
  1.  
  2. UsingSystem;
  3. UsingSystem. IO;
  4. UsingSystem. Security. Cryptography;
  5. UsingSystem. Text;
  6.  
  7. NamespaceDataCrypto
  8. {
  9. /// <Summary> 
  10. /// ASP. NET symmetric encryption algorithm 
  11. /// </Summary> 
  12. Public ClassSymmetricMethod
  13. {
  14.  
  15. PrivateSymmetricAlgorithm mobjCryptoService;
  16. Private StringKey;
  17. /// <Summary> 
  18. /// ASP. NET constructor for symmetric encryption 
  19. /// </Summary> 
  20. PublicSymmetricMethod ()
  21. {
  22. MobjCryptoService =NewRijndaelManaged ();
  23. Key ="Guz (% & hj7x89H $ yuBI0456FtmaT5 & fvHUFCy76 * h % (HilJ $ lhj! Y6 & (* jkP87jH7";
  24. }
  25. /// <Summary> 
  26. /// Accesskey for ASP. NET Data Encryption 
  27. /// </Summary> 
  28. /// <Returns> key </returns> 
  29. Private Byte[] GetLegalKey ()
  30. {
  31. StringSTemp = Key;
  32. MobjCryptoService. GenerateKey ();
  33. Byte[] BytTemp = mobjCryptoService. Key;
  34. IntKeyLength = bytTemp. Length;
  35. If(STemp. Length> KeyLength)
  36. STemp = sTemp. Substring (0, KeyLength );
  37. Else If(STemp. Length <KeyLength)
  38. STemp = sTemp. PadRight (KeyLength,'');
  39. ReturnASCIIEncoding. ASCII. GetBytes (sTemp );
  40. }
  41. /// <Summary> 
  42. /// ASP. NET data encryption to obtain the initial vector IV 
  43. /// </Summary> 
  44. /// <Returns> initial test vector IV </returns> 
  45. Private Byte[] GetLegalIV ()
  46. {
  47. StringSTemp ="E4ghj * Ghg7! RNIfb & 95GUY86GfghUb # er57HBh (u % g6HJ ($ jhWk7 &! Hg4ui % $ hjk";
  48. MobjCryptoService. GenerateIV ();
  49. Byte[] BytTemp = mobjCryptoService. IV;
  50. IntIVLength = bytTemp. Length;
  51. If(STemp. Length> IVLength)
  52. STemp = sTemp. Substring (0, IVLength );
  53. Else If(STemp. Length <IVLength)
  54. STemp = sTemp. PadRight (IVLength,'');
  55. ReturnASCIIEncoding. ASCII. GetBytes (sTemp );
  56. }
  57. /// <Summary> 
  58. /// ASP. NET Data Encryption Method 
  59. /// </Summary> 
  60. /// <Param name = "Source"> string to be encrypted </param> 
  61. /// <Returns> encrypted string </returns> 
  62. Public StringEncrypto (StringSource)
  63. {
  64. Byte[] BytIn = UTF8Encoding. UTF8.GetBytes (Source );
  65. MemoryStream MS =NewMemoryStream ();
  66. MobjCryptoService. Key = GetLegalKey ();
  67. MobjCryptoService. IV = GetLegalIV ();
  68. ICryptoTransform encrypto = mobjCryptoService. CreateEncryptor ();
  69. CryptoStream cs =NewCryptoStream (MS, encrypto, CryptoStreamMode. Write );
  70. Cs. Write (bytIn, 0, bytIn. Length );
  71. Cs. FlushFinalBlock ();
  72. Ms. Close ();
  73. Byte[] BytOut = ms. ToArray ();
  74. ReturnConvert. ToBase64String (bytOut );
  75. }
  76. /// <Summary> 
  77. /// ASP. NET data encryption and decryption method 
  78. /// </Summary> 
  79. /// <Param name = "Source"> string to be decrypted </param> 
  80. /// <Returns> decrypted string </returns> 
  81. Public StringDecrypto (StringSource)
  82. {
  83. Byte[] BytIn = Convert. FromBase64String (Source );
  84. MemoryStream MS =NewMemoryStream (bytIn, 0, bytIn. Length );
  85. MobjCryptoService. Key = GetLegalKey ();
  86. MobjCryptoService. IV = GetLegalIV ();
  87. ICryptoTransform encrypto = mobjCryptoService. CreateDecryptor ();
  88. CryptoStream cs =NewCryptoStream (MS, encrypto, CryptoStreamMode. Read );
  89. StreamReader sr =NewStreamReader (cs );
  90. ReturnSr. ReadToEnd ();
  91. }
  92. }
  93. }

This section describes how to implement ASP. NET Data Encryption By Using symmetric encryption algorithms.

  1. Analysis of ASP. NET data cache mechanism
  2. Introduction to data caching in ASP. NET data cache
  3. Implementation of ASP. NET data collection
  4. Use of SqlHelperSqlHelper in ASP. NET data access layer
  5. Hash Algorithm for ASP. NET Data Encryption

Related Article

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.