Search code examples
javascriptencryptioninternationalizationjcryption

Encryption of foreign characters with javascript library does not work


Has anyone had good luck encrypting foreign chars with any javascript library?

I am encrypting foreign characters using jCryption v. 1.4 library (RSA algo). Looks like the library does not work well with foreign characters (I am using cp1251). The encryption works well with ASCII/english chars, but I get garbled chars after decryption of foreign ones.

I believe the problem comes from the way jCryption converts char string to bytes. It shifts the char by 8 bits only and &'s 2 char together. The foreign chars occupy > 8 bits for sure.

Any ideas would be appreciated!
Dmitriy


Solution

  • You could first try to base64 encode your message and then encrypting. This will make sure the input only has ascii characters.

    So your algorithm would probably be something like this pseudo code.

    Encryption
    input = "text";
    input = base64Encode(input); //there's lots of implementations of this online
    cipher = encodeWithJcrypt(input);
    
    Decryption
    text = decodeWithJcrypt(cipher);
    text = base64Decode(text);