C\#간단 한 암호 화 클래스 인 스 턴 스


public static class EncryptAndDecrypt
     {
         //
         public static string Encrypt(string input)
         {
             //
             string saltValue = "saltValue";
             //
             string pwdValue = "pwdValue";
             byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(input);
             byte[] salt = System.Text.UTF8Encoding.UTF8.GetBytes(saltValue);
             // AesManaged - (AES)
             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
             // Rfc2898DeriveBytes - HMACSHA1 , (PBKDF2 - )
             // salt
             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
             /**/
             /*
          * AesManaged.BlockSize - ( :bit)
          * AesManaged.LegalBlockSizes - ( :bit)
          * AesManaged.KeySize - ( :bit)
          * AesManaged.LegalKeySizes - ( :bit)
          * AesManaged.Key -
          * AesManaged.IV -
          * Rfc2898DeriveBytes.GetBytes(int ) -
          */
             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
             aes.Key = rfc.GetBytes(aes.KeySize / 8);
             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
             // Key IV
             System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor();
             //
             System.IO.MemoryStream encryptStream = new System.IO.MemoryStream();
             // (encryptStream) (encryptTransform)
             System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
                 (encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
             // CryptoStream ( )
             encryptor.Write(data, 0, data.Length);
             encryptor.Close();
             // , Base64
             string encryptedString = Convert.ToBase64String(encryptStream.ToArray());
             return encryptedString;
         }

 
         #region silverlight
         /**/
         /// <summary>
         ///
         /// </summary>
         /// <param name="input"> </param>
         /// <returns> </returns>
         public static string Decrypt(string input)
         {
             // ( )
             string saltValue = "saltValue";
             // ( )
             string pwdValue = "pwdValue";
             byte[] encryptBytes = Convert.FromBase64String(input);
             byte[] salt = Encoding.UTF8.GetBytes(saltValue);
             System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged();
             System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes(pwdValue, salt);
             aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
             aes.KeySize = aes.LegalKeySizes[0].MaxSize;
             aes.Key = rfc.GetBytes(aes.KeySize / 8);
             aes.IV = rfc.GetBytes(aes.BlockSize / 8);
             // Key IV
             System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor();
             //
             MemoryStream decryptStream = new MemoryStream();
             // (decryptStream) (decryptTransform)
             System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
                 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write);
             // CryptoStream ( )
             decryptor.Write(encryptBytes, 0, encryptBytes.Length);
             decryptor.Close();
             //
             byte[] decryptBytes = decryptStream.ToArray();
             string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
             return decryptedString;
         }
         #endregion
     }

좋은 웹페이지 즐겨찾기