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:
-
- UsingSystem;
- UsingSystem. IO;
- UsingSystem. Security. Cryptography;
- UsingSystem. Text;
-
- NamespaceDataCrypto
- {
- /// <Summary>
- /// ASP. NET symmetric encryption algorithm
- /// </Summary>
- Public ClassSymmetricMethod
- {
-
- PrivateSymmetricAlgorithm mobjCryptoService;
- Private StringKey;
- /// <Summary>
- /// ASP. NET constructor for symmetric encryption
- /// </Summary>
- PublicSymmetricMethod ()
- {
- MobjCryptoService =NewRijndaelManaged ();
- Key ="Guz (% & hj7x89H $ yuBI0456FtmaT5 & fvHUFCy76 * h % (HilJ $ lhj! Y6 & (* jkP87jH7";
- }
- /// <Summary>
- /// Accesskey for ASP. NET Data Encryption
- /// </Summary>
- /// <Returns> key </returns>
- Private Byte[] GetLegalKey ()
- {
- StringSTemp = Key;
- MobjCryptoService. GenerateKey ();
- Byte[] BytTemp = mobjCryptoService. Key;
- IntKeyLength = bytTemp. Length;
- If(STemp. Length> KeyLength)
- STemp = sTemp. Substring (0, KeyLength );
- Else If(STemp. Length <KeyLength)
- STemp = sTemp. PadRight (KeyLength,'');
- ReturnASCIIEncoding. ASCII. GetBytes (sTemp );
- }
- /// <Summary>
- /// ASP. NET data encryption to obtain the initial vector IV
- /// </Summary>
- /// <Returns> initial test vector IV </returns>
- Private Byte[] GetLegalIV ()
- {
- StringSTemp ="E4ghj * Ghg7! RNIfb & 95GUY86GfghUb # er57HBh (u % g6HJ ($ jhWk7 &! Hg4ui % $ hjk";
- MobjCryptoService. GenerateIV ();
- Byte[] BytTemp = mobjCryptoService. IV;
- IntIVLength = bytTemp. Length;
- If(STemp. Length> IVLength)
- STemp = sTemp. Substring (0, IVLength );
- Else If(STemp. Length <IVLength)
- STemp = sTemp. PadRight (IVLength,'');
- ReturnASCIIEncoding. ASCII. GetBytes (sTemp );
- }
- /// <Summary>
- /// ASP. NET Data Encryption Method
- /// </Summary>
- /// <Param name = "Source"> string to be encrypted </param>
- /// <Returns> encrypted string </returns>
- Public StringEncrypto (StringSource)
- {
- Byte[] BytIn = UTF8Encoding. UTF8.GetBytes (Source );
- MemoryStream MS =NewMemoryStream ();
- MobjCryptoService. Key = GetLegalKey ();
- MobjCryptoService. IV = GetLegalIV ();
- ICryptoTransform encrypto = mobjCryptoService. CreateEncryptor ();
- CryptoStream cs =NewCryptoStream (MS, encrypto, CryptoStreamMode. Write );
- Cs. Write (bytIn, 0, bytIn. Length );
- Cs. FlushFinalBlock ();
- Ms. Close ();
- Byte[] BytOut = ms. ToArray ();
- ReturnConvert. ToBase64String (bytOut );
- }
- /// <Summary>
- /// ASP. NET data encryption and decryption method
- /// </Summary>
- /// <Param name = "Source"> string to be decrypted </param>
- /// <Returns> decrypted string </returns>
- Public StringDecrypto (StringSource)
- {
- Byte[] BytIn = Convert. FromBase64String (Source );
- MemoryStream MS =NewMemoryStream (bytIn, 0, bytIn. Length );
- MobjCryptoService. Key = GetLegalKey ();
- MobjCryptoService. IV = GetLegalIV ();
- ICryptoTransform encrypto = mobjCryptoService. CreateDecryptor ();
- CryptoStream cs =NewCryptoStream (MS, encrypto, CryptoStreamMode. Read );
- StreamReader sr =NewStreamReader (cs );
- ReturnSr. ReadToEnd ();
- }
- }
- }
This section describes how to implement ASP. NET Data Encryption By Using symmetric encryption algorithms.
- Analysis of ASP. NET data cache mechanism
- Introduction to data caching in ASP. NET data cache
- Implementation of ASP. NET data collection
- Use of SqlHelperSqlHelper in ASP. NET data access layer
- Hash Algorithm for ASP. NET Data Encryption