From 739e3494b9cca604e42d91d19ca04f3c4147f1f6 Mon Sep 17 00:00:00 2001 From: ybanezmarjune Date: Wed, 27 Apr 2016 15:35:17 +0800 Subject: [PATCH] =?UTF-8?q?C#=20Fix=20Error=20in=20"=C3=91=20or=20=C3=B1"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I just found the bug in your c#. I have added UTF encoding to your encryption. This issue will be found if you encrypt a "Ñ or ñ" characters and decrypt it. The return decrypted letter will be cut. Example: {"lname": "ybañez"} ,then encrypt and decrypt it. Output will: {"lname": "ybañe This will make an error in deserializing JSON. --- C-Sharp/CryptLib.cs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/C-Sharp/CryptLib.cs b/C-Sharp/CryptLib.cs index 007ae19..d18391a 100644 --- a/C-Sharp/CryptLib.cs +++ b/C-Sharp/CryptLib.cs @@ -22,7 +22,7 @@ namespace com.pakhee.common *****************************************************************/ public class CryptLib { - UTF8Encoding _enc; + UTF8Encoding _enc = new UTF8Encoding(true, true); RijndaelManaged _rcipher; byte[] _key, _pwd, _ivBytes, _iv; @@ -117,13 +117,16 @@ private String encryptDecrypt (string _inputText, string _encryptionKey, Encrypt if (_mode.Equals (EncryptMode.ENCRYPT)) { //encrypt - byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText) , 0, _inputText.Length); + + byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(_inputText), 0, _enc.GetBytes(_inputText).Length); _out = Convert.ToBase64String(plainText); + } if (_mode.Equals (EncryptMode.DECRYPT)) { //decrypt byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(Convert.FromBase64String(_inputText), 0, Convert.FromBase64String(_inputText).Length); _out = _enc.GetString(plainText); + } _rcipher.Dispose(); return _out;// return encrypted/decrypted string