Skip to content

Commit

Permalink
feat: support encrypt/decrypt UTF-8 text
Browse files Browse the repository at this point in the history
  • Loading branch information
NexZhu committed Jun 5, 2021
1 parent 95fdef1 commit 7e23a03
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 58 deletions.
32 changes: 27 additions & 5 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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])
Expand Down
63 changes: 10 additions & 53 deletions test/test.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script language="JavaScript" type="text/javascript" src="../dist/cryptico.umd.js"></script>

<script>

function print(string)
Expand All @@ -26,7 +23,7 @@
print("Matt's public key string:");
print(MattsPublicKeyString);

var PlainText = "Matt, I need you to help me with my Starcraft strategy.";
var PlainText = "Matt, I need you to encrypt UTF-8 strings which may include 中文字符.";

print("Sam's message: " + PlainText);

Expand All @@ -46,7 +43,7 @@
var PassPhrase = "There Ain't No Such Thing As A Free Lunch.";
var Bits = 512;
var SamsRSAkey = cryptico.generateRSAKey(PassPhrase, Bits);
var PlainText = "Matt, I need you to help me with my Starcraft strategy.";
var PlainText = "Matt, I need you to encrypt UTF-8 strings which may include 中文字符.";
var EncryptionResult = cryptico.encrypt(PlainText, MattsPublicKeyString, SamsRSAkey);

print("Sam's public key ID: " + cryptico.publicKeyID(cryptico.publicKeyString(SamsRSAkey)));
Expand All @@ -69,17 +66,15 @@



/* TODO: fix forged signature test */
print("<h1>Signed, forged signature:</h1>");

EncryptionResult.cipher = "FrD9P9pbSuCpaMExcHI/6WHbrOgLlIWWegHrWRLN027+DekkaVzumh8QbCS7\n\
6BZJpfQ0H0b/pEvPCnE9RNqFFQ==?h7W8J7KrqDd7TCDlOolSUPwRNxoJYoQ\n\
o7h62SDsfLTfKcdzi6DUTfEq7DgsIKIZd8nYYrDmn3F1utFlgVja2mXSD7FY\n\
RRNvYbmpmu3WBozG77hyFup3IlEQeOkKLBk9G1uEYGcrXiIktJiYBvn8ltVP\n\
Qdo6cViIkwYjEdNoCIanYsSO+YB20EyuKfDj0p62QW9sAVx8jeQmY+f7cvWj\n\
/3evPfZ2D3gaXXT+QY2mu+0ap8P89rPFmrlMgMVFRye4FEWHSkSiKtrddt1y\n\
DZoMxwxFytKA2QciN7MHgZRZ16kcO1KjpPlb9jSXDbzllCWDhigN+kvBog4L\n\
GvhTe0CEn5HKGpWx1+TGbC7pim6/KOFo34DScLOrclUNGl0VY8W+/+EBXhin\n\
dthvRRcjy+0BRn4tDpC4QJjdJoXCqDmT3NRU="
var ForgersRSAkey = cryptico.generateRSAKey(PassPhrase, Bits);
var ForgedEncryptionResult = cryptico.encrypt(PlainText, MattsPublicKeyString, ForgersRSAkey);

print("Forger's public key ID: " + cryptico.publicKeyID(cryptico.publicKeyString(ForgersRSAkey)));

EncryptionResult.cipher = ForgedEncryptionResult.cipher

print("The encrypted message:");
print(EncryptionResult.cipher);
Expand All @@ -97,46 +92,8 @@
print("The signature public key ID:");
print(cryptico.publicKeyID(DecryptionResult.publicKeyString));



</script>

</head>

<body style="font-family: monospace; white-space:pre;">

</body>

</html>
































0 comments on commit 7e23a03

Please sign in to comment.