Api.Encryption.StringCipher.Encrypt C# (CSharp) Метод

Encrypt() публичный статический Метод

public static Encrypt ( string plainText, string passPhrase ) : string
plainText string
passPhrase string
Результат string
        public static string Encrypt(string plainText, string passPhrase)
        {
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
            using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
            {
                byte[] keyBytes = password.GetBytes(keysize / 8);
                using (RijndaelManaged symmetricKey = new RijndaelManaged())
                {
                    symmetricKey.Mode = CipherMode.CBC;
                    using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
                    {
                        using (MemoryStream memoryStream = new MemoryStream())
                        {
                            using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                            {
                                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                                cryptoStream.FlushFinalBlock();
                                byte[] cipherTextBytes = memoryStream.ToArray();
                                return Convert.ToBase64String(cipherTextBytes);
                            }
                        }
                    }
                }
            }
        }

Usage Example

Пример #1
0
        public static AuthorizationViewModel EncryptAuthorization(AuthorizationViewModel auth)
        {
            if (auth.GuId != null)
            {
                auth.GuId = StringCipher.Encrypt(auth.GuId, EncryptKey);
            }

            return(auth);
        }
All Usage Examples Of Api.Encryption.StringCipher::Encrypt
StringCipher