From 7e23a030227115053244f4975d06e1f2c31c16ad Mon Sep 17 00:00:00 2001 From: Nex Zhu <4370605+NexZhu@users.noreply.github.com> Date: Sat, 5 Jun 2021 14:52:19 +0800 Subject: [PATCH] feat: support encrypt/decrypt UTF-8 text See: - https://github.com/wwwtyro/cryptico/issues/12 - https://github.com/jpfox/cryptico/commit/73db107f8e1e0d59696e1aece9e7a2bc82268707 - https://github.com/wwwtyro/cryptico/pull/35 --- src/api.js | 32 +++++++++++++++++++++---- test/test.html | 63 ++++++++------------------------------------------ 2 files changed, 37 insertions(+), 58 deletions(-) diff --git a/src/api.js b/src/api.js index 2531cf0..0a6ef32 100644 --- a/src/api.js +++ b/src/api.js @@ -134,6 +134,28 @@ export default { return str }, + // Converts a UTF-8 string to ASCII string. + utf82string(string) { + return unescape(encodeURIComponent(string)) + }, + + // Converts ascii string to a UTF-8 string. + string2utf8(uriencoded) { + return decodeURIComponent(escape(uriencoded)) + }, + + // Converts a UTF-8 string to a byte array. + utf82bytes(string) { + const uriencoded = unescape(encodeURIComponent(string)) + return this.string2bytes(uriencoded) + }, + + // Converts a byte array to a UTF-8 string. + bytes2utf8(bytes) { + const uriencoded = this.bytes2string(bytes) + return decodeURIComponent(escape(uriencoded)) + }, + // Returns a XOR b, where a and b are 16-byte byte arrays. blockXOR(a, b) { const xor = new Array(16) @@ -175,7 +197,7 @@ export default { encryptAESCBC(plaintext, key) { const exkey = key.slice(0) aes.ExpandKey(exkey) - let blocks = this.string2bytes(plaintext) + let blocks = this.utf82bytes(plaintext) blocks = this.pad16(blocks) let encryptedBlocks = this.blockIV() for (let i = 0; i < blocks.length / 16; i++) { @@ -204,7 +226,7 @@ export default { decryptedBlocks = decryptedBlocks.concat(tempBlock) } decryptedBlocks = this.depad(decryptedBlocks) - return this.bytes2string(decryptedBlocks) + return this.bytes2utf8(decryptedBlocks) }, // Wraps a str to 60 characters. @@ -283,9 +305,9 @@ export default { return { status: 'failure' } } aeskey = this.string2bytes(aeskey) - const plaintext = this - .decryptAESCBC(cipherblock[1], aeskey) - .split('::52cee64bb3a38f6403386519a39ac91c::') + const plaintext = this.decryptAESCBC(cipherblock[1], aeskey).split( + '::52cee64bb3a38f6403386519a39ac91c::', + ) if (plaintext.length === 3) { const publickey = this.publicKeyFromString(plaintext[1]) const signature = this.b64to16(plaintext[2]) diff --git a/test/test.html b/test/test.html index 5514727..4efe312 100644 --- a/test/test.html +++ b/test/test.html @@ -1,10 +1,7 @@ - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -