Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

C# equivalent of VB Convert.ToBase64String(Algo.IV.Concat(MemStream.ToArray()).ToArray())

$
0
0

HI

I have the following VB code for encrypting (works well) plain text.

    Private Function AESEncryptStringToBase64(strPlainText As String, EncryptionKey As String) As String
        Dim Algo As RijndaelManaged = RijndaelManaged.Create()


        With Algo
            .BlockSize = 128
            .FeedbackSize = 128
            .KeySize = 256
            .Mode = CipherMode.CBC
            .IV = Guid.NewGuid().ToByteArray()
            .Key = Encoding.ASCII.GetBytes(EncryptionKey)
        End With


        Using Encryptor As ICryptoTransform = Algo.CreateEncryptor()
            Using MemStream As New MemoryStream
                Using CryptStream As New CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write)
                    Using Writer As New StreamWriter(CryptStream)
                        Writer.Write(strPlainText)
                    End Using


                    AESEncryptStringToBase64 = Convert.ToBase64String(Algo.IV.Concat(MemStream.ToArray()).ToArray())
                End Using
            End Using
        End Using
    End Function

I am trying to convert to C# and I think I have the code correct except for the highlighted line above. See the highlighted line below.

Thanks for any help.

My C# code follows.

        protected string Encrypt(string strPlainText, string encryptionKey)
        {
            string AESEncryptStringToBase64;
            RijndaelManaged Algo = new RijndaelManaged();
            Algo.BlockSize = 128;
            Algo.FeedbackSize = 128;
            Algo.KeySize = 256;
            Algo.Mode = CipherMode.CBC;
            Algo.IV = Guid.NewGuid().ToByteArray();
            Algo.Key = Encoding.ASCII.GetBytes(encryptionKey);


            using (ICryptoTransform Encryptor = Algo.CreateEncryptor())
            {
                using (MemoryStream MemStream = new MemoryStream())
                {
                    using (CryptoStream CryptStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter Writer = new StreamWriter(CryptStream))
                        {
                            Writer.Write(strPlainText);
                        }   //using (StreamWriter Writer = new StreamWriter(CryptStream))


                        byte [] temp = MemStream.ToArray();
                        AESEncryptStringToBase64 = BitConverter.ToString(temp);


                    }   //using (CryptoStream CryptStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                }   //using (MemoryStream MemStream = new MemoryStream())
            }   //using (ICryptoTransform Encryptor = Algo.CreateEncryptor())


            return AESEncryptStringToBase64;
        }


Viewing all articles
Browse latest Browse all 3509

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>