简体   繁体   中英

Remove ASCII or Unicode characters from encrypted data

I'm trying to get an encrypted code stored in a MySQL database using C#. Sometimes the code is fine but sometimes I get some ASCII and Unicode characters at the end of the decrypted code.

I understand, because of the encryption/decryption process, you get some extra characters; the problem is that those characters are not consistent (is not like I always get a bunch of \\0 at the end).

codeA\0\0\0\0\0\0\0\0\0
codeB\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010\u0010
codeC\b\b\b\b\b\b\b\b\b
codeD\u0003\u0003\u0003\u0003\u0003\u0003\u0003\u0003\u0003

I get different ending characters and to be honest it would be difficult to add a "Replace" method for every character that happens to appear.

Is there a way to remove those "special" characters at the end? Or maybe am I just storing/reading the code wrong?

I write the process for PHP (web page utf-8, the writer of the code) and C# (console app, the reader of the code)

PHP

// Get $code from html form in utf-8

$code = $_POST['code'];
$encrypted_code = base64_encode(openssl_encrypt($code, 'aes-256-ecb', 'keykeykeykeykeykeykeykey', OPENSSL_RAW_DATA)));

// Store $encrypted_code into database

C#

// Read encrypted_code from database using MySqlConnection

Aes aes = Aes.Create();
aes.Key = Encoding.UTF8.GetBytes("keykeykeykeykeykeykeykey");
aes.Mode = CipherMode.ECB;
aes.Padding = PaddingMode.None;
aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
string code = null;
using (aes)
{
            byte[] encrypted_code = Convert.FromBase64String(encrypted_code);
            using (var encrypted_stream = new MemoryStream(encrypted_code))
            using (var decrypter_stream = new CryptoStream(encrypted_stream, decryptor, CryptoStreamMode.Read))
            using (var decrypted_stream = new StreamReader(decrypter_stream))
            code = decrypted_stream.ReadLine().
                        Replace("\0", string.Empty).
                        Replace("\b", string.Empty).
                        Replace("\v", string.Empty).
                        Replace("\t", string.Empty).
                        Replace("\u0010", string.Empty).
                        Replace("\u0003", string.Empty).
                        Replace("\u0006", string.Empty);
}

//Use code for whatever

By default the PHP encryption method you chose adds padding which returns data results like "CodeA\\0\\0\\0\\0\\0\\0\\0\\0\\0". In order to get decrypted data returned without the padding simply change the PaddingMode in the C# code to:

aes.Padding = PaddingMode.PKCS7;


BTW: This code: $encrypted_code = openssl_encrypt($code, 'aes-256-ecb', 'keykeykeykeykeykeykeykey');

produces the same as this code: $encrypted_code = base64_encode(openssl_encrypt($code, 'aes-256-ecb', 'keykeykeykeykeykeykeykey', OPENSSL_RAW_DATA));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM