riv
riv

Reputation: 93

c# AES decryption Hebrew letters display as question marks

I have a string containing hebrew letters,
after enctyption when I'm tring to decrypt the encrypted string, all the hebrew letters are shown as question marks (like -> ??? ?? ??????)

those are the 2 methods I'm using to encrypt and decrypt

 public static string Encrypt(string dectypted)
    {
        byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(dectypted);
        AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
        encdec.BlockSize = 128;
        encdec.KeySize = 256;
        encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
        encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
        encdec.Padding = PaddingMode.PKCS7;
        encdec.Mode = CipherMode.CBC;

        ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);

        byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
        icrypt.Dispose();

        return Convert.ToBase64String(enc) + Key;
    }

    public static string Decrypt(string enctypted)
    {
        byte[] encbytes = Convert.FromBase64String(enctypted);
        AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
        encdec.BlockSize = 128;
        encdec.KeySize = 256;
        encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
        encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
        encdec.Padding = PaddingMode.PKCS7;
        encdec.Mode = CipherMode.CBC;

        ICryptoTransform icrypt = encdec.CreateDecryptor(encdec.Key, encdec.IV);

        byte[] dec = icrypt.TransformFinalBlock(encbytes, 0, encbytes.Length);
        icrypt.Dispose();

        return ASCIIEncoding.ASCII.GetString(dec);
    }

can someone please tell me what is wrong and why I get question marks insted of hebrew letters?
thanks in advance

Upvotes: 0

Views: 423

Answers (1)

Greg
Greg

Reputation: 23463

ASCII cannot represent Hebrew characters. It can only represent a limited set of Latin characters and symbols. UTF8 is probably the encoding that you want to use. Replace your use of ASCIIEncoding.ASCII with Encoding.UTF8.

Upvotes: 7

Related Questions