Snack-X / rijndael-js

Pure JS implementation of Rijndael algorithm
MIT License
18 stars 10 forks source link

What if i have to change the padding mode #9

Open nasyx-rakeeb opened 2 years ago

nasyx-rakeeb commented 2 years ago

can you please help me out, i am trying to convert below c#/dotnet piece of code ot javascript using rijndael-js package but i can't make it work.

public string EncryptString(string input)
        {
            RijndaelManaged objrij = new RijndaelManaged();
            objrij.Mode = CipherMode.CBC;
            objrij.Padding = PaddingMode.PKCS7;
            byte[] passBytes = Encoding.UTF8.GetBytes(this.key);
            objrij.Key = passBytes;
            byte[] IV = new byte[16];
            objrij.IV = IV;

            byte[] encrypted;
            ICryptoTransform encryptor = objrij.CreateEncryptor(objrij.Key, objrij.IV);

            // Create the streams used for encryption.
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                       swEncrypt.Write(input);
                    }
                    encrypted = msEncrypt.ToArray();
                }
            }
            return Convert.ToBase64String(encrypted);
        }

        public string DecryptString(string encryptedText)
        {
            RijndaelManaged objrij = new RijndaelManaged();
            objrij.Mode = CipherMode.CBC;
            objrij.Padding = PaddingMode.PKCS7;
            byte[] encryptedTextByte = Convert.FromBase64String(encryptedText);
            byte[] passBytes = Encoding.UTF8.GetBytes(this.key);
            byte[] IV = new byte[16];
            objrij.Key = passBytes;
            objrij.IV = IV;

            string plaintext = null;
            ICryptoTransform decryptor = objrij.CreateDecryptor(objrij.Key, objrij.IV);

            using (MemoryStream msDecrypt = new MemoryStream(encryptedTextByte))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }
            return plaintext;

        }
mina68 commented 1 year ago

@nasyx-rakeeb Did you figure it out? I am having the same scenario

nasyx-rakeeb commented 1 year ago

@mina68 i then used a built-in nodejs module crypto and it was just few lines of code in nodejs than csharp. Crypto module has many options to configure you can check it out on npm

nasyx-rakeeb commented 1 year ago

you can also check out this repository , i wrote this code when i was trying to convert the csharp RijndaelManaged encryption method to Javascript, its written in pure js, but i really dont know if its working as expected, i did check it when i found crypto

mina68 commented 1 year ago

@nasyx-rakeeb such a huge effort there! I tried to implement it with crypto but with no luck so far, will be so grateful if you can show me a snippet for the code

nasyx-rakeeb commented 1 year ago

i don't know if its still in my pc, i cant seem to find it but i ll take a look and let you know

On Sun, 11 Dec 2022, 9:06 pm mina68, @.***> wrote:

@nasyx-rakeeb https://github.com/nasyx-rakeeb such a huge effort there! I tried to implement it with crypto but with no luck so far, will be so grateful if you can show me a snippet for the code

— Reply to this email directly, view it on GitHub https://github.com/Snack-X/rijndael-js/issues/9#issuecomment-1345585075, or unsubscribe https://github.com/notifications/unsubscribe-auth/AVMRHW33URKPEXQKUAKLZVTWMXYGRANCNFSM6AAAAAARL4GQ4Y . You are receiving this because you were mentioned.Message ID: @.***>

cwadrupldijjit commented 2 months ago

@nasyx-rakeeb such a huge effort there! I tried to implement it with crypto but with no luck so far, will be so grateful if you can show me a snippet for the code

I know this is farther into the future than is likely relevant to you anymore, but (for posterity's sake) Rijndael 128 is apparently equivalent to AES 128, due to the fact that the block and key size for that algorithm is 128, but other byte strengths of Rijndael (e.g., 192 and 256) has block sizes that match the key sizes. Different byte strengths of AES have the key being that byte strength, but AES locks the block size to 128, rendering the built-in crypto unable to make it a straight-across replacement if using anything other than 128.

This link is a StackOverflow answer that I came across that explains it perhaps a little better than I could: https://stackoverflow.com/a/32703858/5294492