forge icon indicating copy to clipboard operation
forge copied to clipboard

Chinese is not supported RSA sign

Open a1031101978 opened this issue 3 years ago • 4 comments

Hopefully can work it out

a1031101978 avatar Sep 02 '22 15:09 a1031101978

PKCS1

a1031101978 avatar Sep 02 '22 15:09 a1031101978

Can you provide a minimal code example?

davidlehn avatar Sep 02 '22 20:09 davidlehn

Can you provide a minimal code example?

The SIGN I returned with JavaWeb does not contain Chinese and can be passed 屏幕截图 2022-09-03 125248 angular: 屏幕截图 2022-09-03 125303 屏幕截图 2022-09-03 125202 屏幕截图 2022-09-03 125317 Returns false after using Chinese

a1031101978 avatar Sep 03 '22 04:09 a1031101978

Can you provide a minimal code example?

This is what happens when use Chinese 中文 屏幕截图 2022-09-03 125918 屏幕截图 2022-09-03 130014 屏幕截图 2022-09-03 130032

a1031101978 avatar Sep 03 '22 05:09 a1031101978

Also in?

a1031101978 avatar Oct 15 '22 01:10 a1031101978

@a1031101978,

Please take a look at these issues: https://github.com/digitalbazaar/forge/issues?q=chinese

You need to convert your character strings into binary-encoded strings (so each character uses a character code of 0-255, i.e. a "string of bytes") when using forge. This is because forge was written before Uint8Array existed. Therefore, use the UTF-8 encoding APIs to convert a string of characters into a string of bytes before passing the string to the various crypto APIs.

dlongley avatar Oct 15 '22 17:10 dlongley

@a1031101978 As @dlongley said,you should encode those Chinese characters to utf-8 style first. for encryption

    const publickeyStr = "<public key...>";
    const plaintextStr = "我爱你";
    const publickey = forge.pki.publicKeyFromPem(publickeyStr);
    const buffer = forge.util.createBuffer();
    buffer.data = forge.util.encodeUtf8(plaintextStr)
    const encryptedStr = forge.util.encode64(publickey.encrypt(buffer.bytes()))
    console.log(encryptedStr);

for decryption

    const privatekeyStr = "<private key...>"
    const encryptedStr = "<encrypted string ...>";
    const privateKey = forge.pki.privateKeyFromPem(privatekeyStr);
    const decrypted = privateKey.decrypt(forge.util.decode64(encryptedStr))
    const plaintextStr = forge.util.decodeUtf8(decrypted)
    console.log(plaintextStr);

You can find more detail on this project: https://github.com/akino512/forge-web

I think this issue should be closed.

akino512 avatar Jul 26 '23 07:07 akino512

Thanks! I agree -- closing.

dlongley avatar Jul 26 '23 16:07 dlongley