-1

I can encrypt just fine. However, if an encrypted string doesn't end with the equal letter "=" at the end, then it won't decrypt properly and I get an empty string (or so it seems).

What works:

+FmU/RQ5jP86j7F6bPjddA==

YQXBLjwEXEGgYz8xnF10O9QqO/vj5DI8PpZZCvfhG1RGHAYumtWrAEBfdSSOkF79vzVCSQ+ejO3uMIDSmY43bw==

What won't work:

xnyfqPsvrEOK8AoQz2p7AGFHoHncZ/wB/R3qr+scts6nLI2xauWnbmYsXsU3iMoT

encrypt function:

    public string EncryptionKey = "abc123";


    public string encrypt(string input)
    {           
        byte[] clearBytes = Encoding.Unicode.GetBytes(input);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.Close();
                }
                input = Convert.ToBase64String(ms.ToArray());
            }
        }
        return input;
    }

Here's the decrypt function:

   public string decrypt(string cipherText)
    {

        byte[] cipherBytes = Convert.FromBase64String(cipherText);
        using (Aes encryptor = Aes.Create())
        {
            Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
            encryptor.Key = pdb.GetBytes(32);
            encryptor.IV = pdb.GetBytes(16);
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.Close();
                }
                return Encoding.Unicode.GetString(ms.ToArray());
            }
        }
    }

Any idea how to solve this?

EDIT: Added encrypt function

EDIT 2:

This is how I'm calling the encrypt-function (before I send the string via socket.io)

 ASCIIEncoding aEncoding = new ASCIIEncoding();
 byte[] sendMsg = new byte[1500];
 sendMsg = aEncoding.GetBytes((encrypt(txtMsg.Text)));
Muppen
  • 49
  • 6
  • @BrootsWaymb I see, then I guess it's the encrypt-function (I added it above) that makes an invalid base64 string? – Muppen Dec 20 '18 at 20:30
  • just read my answer, but if you can't understand it, your answer is simple - YOU CAN'T DO THIS if encrypted string comes with "=" or "==" chars in the end. – Maciej S. Dec 20 '18 at 20:31
  • What's the exact error you're getting? I'm pretty sure the '=' thing is a red herring -- the failing string is a valid base64-encoded string. – manveti Dec 20 '18 at 20:33
  • is it original text: 鄺ੁ﯍톋螟⢹ⲰⳞ蘮읝赂厚᨟בֿ䔋⊮ ? – Maciej S. Dec 20 '18 at 20:52
  • @MaciejS. No it's not. I can't recall what it was. – Muppen Dec 20 '18 at 21:12
  • was your encrypted string in any Asian dialect? maybe your decrypting function is OK, but I don't have installed some components... generally I got exception while running your decrypt function and had to delete `cs.Close();` line (this is known bug discussed in MSDN forums and solved by custom overriden function). but my output wasn't empty string... – Maciej S. Dec 20 '18 at 21:46

1 Answers1

2

I will not give you full answer and make any lecture of cryptography, because it's huge topic. Answer for Your question is simple - it will not work without "=" or "==" characters on the end as long as you are using Convert.FromBase64String function, becuase those chars are characteristic to this encoding. More information under this link in "Output padding" section: Base64 on Wikipedia Conclusion: if encrypted string comes with mentioned chars, it can't be decrypted without it.

Maciej S.
  • 752
  • 10
  • 22