[Crypto Library] Unable to encrypt Chinese characters

Forge Component
(1)
Published on 2016-11-08 by André Vieira
1 vote
Published on 2016-11-08 by André Vieira

Hello 

I use this plugin to encrypt Chinese, but decrypting it is not correct, I hope you can help me.

Thanks

Solution

Hi Mars,

This is a common garbled/corrupted Chinese wording issue, which can be resolved by the use of UTF-8 conversion.

Checking in the plugin itself, the encryption and decryption of wordings in English and numbers are working perfectly fine, while characters in Chinese and Japanese (I have only tested these 2) are going into garbled characters, just as what you are showing in screenshots above. In order to deal with this issue, I looked back into the source code of the plugin, and among all the discussion, I am able to find out this post. Inside the post and comparing it with the function call in the plugin, I am able to find out that the existing way of calling in the plugin has not yet been taking UTF-8 into consideration. And therefore, characters like Chinese or Japanese could result in garbled characters.

Encrypt

image.png

Decrypt

image.png

In regard of this, I have made changes to the call such that it also now takes UTF-8 into consideration.

Encrypt

var cipher = forge.cipher.createCipher('AES-CBC', $parameters.Key);
cipher.start({iv: $parameters.IV});
cipher.update(forge.util.createBuffer($parameters.Value,'utf8'));
cipher.finish();
$parameters.Encrypted = forge.util.encode64(cipher.output.getBytes());

Decrypt


var decipher = forge.cipher.createDecipher('AES-CBC', $parameters.Key);
decipher.start({iv: $parameters.IV});
decipher.update(forge.util.createBuffer(forge.util.decode64($parameters.Encrypted)));
decipher.finish();
$parameters.Value = decipher.output.toString();

Testing with Chinese, it works. Switched back to English, working as well. Problem solved!

Hope that helps :)


Cheers,

Hillman


Solution

Hillman Hui wrote: Thanks you very much!

Hi Mars,

This is a common garbled/corrupted Chinese wording issue, which can be resolved by the use of UTF-8 conversion.

Checking in the plugin itself, the encryption and decryption of wordings in English and numbers are working perfectly fine, while characters in Chinese and Japanese (I have only tested these 2) are going into garbled characters, just as what you are showing in screenshots above. In order to deal with this issue, I looked back into the source code of the plugin, and among all the discussion, I am able to find out this post. Inside the post and comparing it with the function call in the plugin, I am able to find out that the existing way of calling in the plugin has not yet been taking UTF-8 into consideration. And therefore, characters like Chinese or Japanese could result in garbled characters.

Encrypt

image.png

Decrypt

image.png

In regard of this, I have made changes to the call such that it also now takes UTF-8 into consideration.

Encrypt

var cipher = forge.cipher.createCipher('AES-CBC', $parameters.Key);
cipher.start({iv: $parameters.IV});
cipher.update(forge.util.createBuffer($parameters.Value,'utf8'));
cipher.finish();
$parameters.Encrypted = forge.util.encode64(cipher.output.getBytes());

Decrypt


var decipher = forge.cipher.createDecipher('AES-CBC', $parameters.Key);
decipher.start({iv: $parameters.IV});
decipher.update(forge.util.createBuffer(forge.util.decode64($parameters.Encrypted)));
decipher.finish();
$parameters.Value = decipher.output.toString();

Testing with Chinese, it works. Switched back to English, working as well. Problem solved!

Hope that helps :)


Cheers,

Hillman