0

In swift I wrote this code to encrypt a string

func Encrypt2(_ input:String)->String?{
    let key = "123456abcdefcdhs"
    do{
        let encrypted: Array<UInt8> = try AES(key: key, iv: key, padding: .pkcs5).encrypt(Array(input.utf8))
        return encrypted.toBase64()?.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
    }catch{

    }
    return nil
}

And in c# the code for decryption is this

string key = "123456abcdefcdhs";
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            rijndaelCipher.Mode = CipherMode.CBC;
            rijndaelCipher.Padding = PaddingMode.PKCS7;

            rijndaelCipher.KeySize = 0x80;
            rijndaelCipher.BlockSize = 0x80;
            byte[] encryptedData = Convert.FromBase64String(textToDecrypt);
            byte[] pwdBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyBytes = new byte[0x10];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
            {
                len = keyBytes.Length;
            }
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            rijndaelCipher.IV = keyBytes;
            byte[] plainText = rijndaelCipher.CreateDecryptor().TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return Encoding.UTF8.GetString(plainText);

Encrytion works fine but when it comes to c# decryption...give me an error saying base64 length. I tried changing the .pkcs5 to .pkcs7 but still doesn't work. whats wrong with the code?

The base64 string is --> aMtLgvWQOguK+GPbGT/Jxw== sometimes the string is --> aMtLgvWQOguK GPbGT/Jxw==

Joakim Danielson
  • 43,251
  • 5
  • 22
  • 52
  • At a guess Swift uses a modified base64 encoding. If you showed the base64 it might help. C# expects base64 using `+`, `/` and, for padding, `=`. Modified base64 might instead use `-` and `_` and not include any padding, for example. See the [base64 wiki page](https://en.wikipedia.org/wiki/Base64#Implementations_and_history) for more info. – ProgrammingLlama Apr 13 '20 at 02:25
  • just added the base64 – BongJae Jeong Apr 13 '20 at 02:27
  • That's the base64 as output from Swift, right? Because I have no issues converting that to `byte[]` in C# using the `Convert.FromBase64String` method. – ProgrammingLlama Apr 13 '20 at 02:28
  • yes its from swift code..hmm...is it my c# code thats wrong? it keeps giving me Invalid length for a Base-64 char array or string.' this as an error – BongJae Jeong Apr 13 '20 at 02:30
  • The base64 you've provided [doesn't repro the problem though](https://rextester.com/CSHM94929). That's the only place that could throw that exception, as far as I'm aware. – ProgrammingLlama Apr 13 '20 at 02:33
  • the base64 sometimes changes removing + – BongJae Jeong Apr 13 '20 at 02:40
  • 1
    OK, I'm going to go out on a limb here and say that you're posting this to a web server, since `+` can be interpreted the same as space (also encoded as `%20`). You should URL encode your base64 before sending it to a server in the URL. Specifically, `+` should be `%2B`, `/` should be `%2F`, and `=` should be `%3D`. It seems there is a [Swift method](https://stackoverflow.com/questions/24551816/swift-encode-url) for this. – ProgrammingLlama Apr 13 '20 at 02:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211522/discussion-between-bongjae-jeong-and-john). – BongJae Jeong Apr 13 '20 at 02:53

0 Answers0