Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What if i have to change the padding mode #9

Open
nasyx-rakeeb opened this issue Oct 22, 2022 · 6 comments
Open

What if i have to change the padding mode #9

nasyx-rakeeb opened this issue Oct 22, 2022 · 6 comments

Comments

@nasyx-rakeeb
Copy link

nasyx-rakeeb commented Oct 22, 2022

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
Copy link

mina68 commented Dec 11, 2022

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

@nasyx-rakeeb
Copy link
Author

@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
Copy link
Author

nasyx-rakeeb commented Dec 11, 2022

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
Copy link

mina68 commented Dec 11, 2022

@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
Copy link
Author

nasyx-rakeeb commented Dec 11, 2022 via email

@cwadrupldijjit
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants