Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid encryption in c# with a string "ñ" #43

Open
ybanezmarjune opened this issue Apr 12, 2016 · 1 comment
Open

Invalid encryption in c# with a string "ñ" #43

ybanezmarjune opened this issue Apr 12, 2016 · 1 comment

Comments

@ybanezmarjune
Copy link

{
"Encrypted":"vQpHZRu60M6SK0Yxjm9HfAy86e8UeaVztyB5GQdMblJ7aOkSO4LwgFlIAIX5WtEuF0CgBZO4fOZJTkAH5KtuIpxB6g7f4bwMj/XiNjD9eXJcwN2h9r02Yv5s4TIG7RsWXvNIGVPrege6B0t5tSIXeAHYEM5+JiqdtBgC74a+ogFK+0MUToj3shEsG0itUVq0hACAAz11HzBnhNSpc9jTKxGkmBPpCBAs9OOJ83IAOm1Gh0XmwBXyKVd50ueiHCuEpjoJ0FEK5Dnl73+XitdS/HJUWH2QleEPZs7HejFNRvb7u/RSViLkzRhWnRAbBTY9",
"Iv":"xwp5H7XV95ekvF6m"
}

This is response from c# wcf service. We figure out that when the data have "ñ" , the encryption is incomplete.

@ybanezmarjune ybanezmarjune changed the title Invalid encrypt. Invalid encryption in c# with a string "ñ" Apr 12, 2016
@ghost
Copy link

ghost commented Feb 2, 2017

I don't know it you are still needing a fix after all that time but here it is.
C# implementation does not work for non English cahracters.
After little bit of testing I have found the problem.
Code uses length of the string to encrypt data lenghth of the sting is expressed in characters which is not same as the array size generated by GetBytes method (it is the same using ASCII character only).

replace if statements in method EncryptDecrypt
....
if (mode.Equals(EncryptMode.Encrypt))
{
var encbytes = _enc.GetBytes(inputText);
//encrypt
byte[] plainText = _rcipher.CreateEncryptor().TransformFinalBlock(_enc.GetBytes(inputText), 0, encbytes.Length);
_out = Convert.ToBase64String(plainText);
}
if (mode.Equals(EncryptMode.Decrypt))
{
//decrypt
var decText = Convert.FromBase64String(inputText);
byte[] plainText = _rcipher.CreateDecryptor().TransformFinalBlock(decText, 0, decText.Length);
_out = _enc.GetString(plainText);
}
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant