Skip to content
This repository has been archived by the owner on Aug 5, 2022. It is now read-only.

Added UTF8 support #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,33 @@ var cryptico = (function() {
}
return string;
}

// Converts a utf8 string to ascii string.
my.utf82string = function(string)
{
return unescape(encodeURIComponent(string));
}

// Converts ascii string to a utf8 string.
my.string2utf8 = function(uriencoded)
{
return decodeURIComponent(escape(uriencoded));
}

// Converts a utf8 string to a byte array.
my.utf82bytes = function(string)
{
var uriencoded = unescape(encodeURIComponent(string));
return my.string2bytes(uriencoded);
}

// Converts a byte array to a utf8 string.
my.bytes2utf8 = function(bytes)
{
var uriencoded = my.bytes2string(bytes);
var string = decodeURIComponent(escape(uriencoded));
return string;
}

// Returns a XOR b, where a and b are 16-byte byte arrays.
my.blockXOR = function(a, b)
Expand Down Expand Up @@ -199,7 +226,7 @@ var cryptico = (function() {
return newBytes;
}

// AES CBC Encryption.
// AES CBC Encryption for ascii string.
my.encryptAESCBC = function(plaintext, key)
{
var exkey = key.slice(0);
Expand All @@ -219,7 +246,14 @@ var cryptico = (function() {
return my.b256to64(ciphertext)
}

// AES CBC Decryption.
// AES CBC Encryption for utf8 string.
my.encryptUTF8AESCBC = function(plaintext, key)
{
var string = my.utf82string(plaintext);
return my.encryptAESCBC(string, key);
}

// AES CBC Decryption for ascii string.
my.decryptAESCBC = function(encryptedText, key)
{
var exkey = key.slice(0);
Expand All @@ -238,6 +272,13 @@ var cryptico = (function() {
decryptedBlocks = my.depad(decryptedBlocks);
return my.bytes2string(decryptedBlocks);
}

// AES CBC Decryption for utf8 string.
my.decryptUTF8AESCBC = function(encryptedText, key)
{
var string = my.decryptAESCBC(encryptedText, key);
return my.string2utf8(string);
}

// Wraps a string to 60 characters.
my.wrap60 = function(string)
Expand Down
48 changes: 45 additions & 3 deletions cryptico.js
Original file line number Diff line number Diff line change
Expand Up @@ -2696,7 +2696,7 @@ RSAKey.prototype.doPublic = RSADoPublic;
// public
RSAKey.prototype.setPublic = RSASetPublic;
RSAKey.prototype.encrypt = RSAEncrypt;

RSAKey.prototype.encryptUTF8 = function(text){RSAEncrypt(cryptico.utf82string(text))};
// Version 1.1: support utf-8 decoding in pkcs1unpad2
// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext

Expand Down Expand Up @@ -2830,6 +2830,7 @@ RSAKey.prototype.setPrivate = RSASetPrivate;
RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
RSAKey.prototype.generate = RSAGenerate;
RSAKey.prototype.decrypt = RSADecrypt;
RSAKey.prototype.decryptUTF8 = function(ctext){return cryptico.string2utf8(RSADecrypt(ctext))}


//
Expand Down Expand Up @@ -3334,6 +3335,33 @@ var cryptico = (function() {
}
return string;
}

// Converts a utf8 string to ascii string.
my.utf82string = function(string)
{
return unescape(encodeURIComponent(string));
}

// Converts ascii string to a utf8 string.
my.string2utf8 = function(uriencoded)
{
return decodeURIComponent(escape(uriencoded));
}

// Converts a utf8 string to a byte array.
my.utf82bytes = function(string)
{
var uriencoded = unescape(encodeURIComponent(string));
return my.string2bytes(uriencoded);
}

// Converts a byte array to a utf8 string.
my.bytes2utf8 = function(bytes)
{
var uriencoded = my.bytes2string(bytes);
var string = decodeURIComponent(escape(uriencoded));
return string;
}

// Returns a XOR b, where a and b are 16-byte byte arrays.
my.blockXOR = function(a, b)
Expand Down Expand Up @@ -3379,7 +3407,7 @@ var cryptico = (function() {
return newBytes;
}

// AES CBC Encryption.
// AES CBC Encryption for ascii string.
my.encryptAESCBC = function(plaintext, key)
{
var exkey = key.slice(0);
Expand All @@ -3399,7 +3427,14 @@ var cryptico = (function() {
return my.b256to64(ciphertext)
}

// AES CBC Decryption.
// AES CBC Encryption for utf8 string.
my.encryptUTF8AESCBC = function(plaintext, key)
{
var string = my.utf82string(plaintext);
return my.encryptAESCBC(string, key);
}

// AES CBC Decryption for ascii string.
my.decryptAESCBC = function(encryptedText, key)
{
var exkey = key.slice(0);
Expand All @@ -3418,6 +3453,13 @@ var cryptico = (function() {
decryptedBlocks = my.depad(decryptedBlocks);
return my.bytes2string(decryptedBlocks);
}

// AES CBC Decryption for utf8 string.
my.decryptUTF8AESCBC = function(encryptedText, key)
{
var string = my.decryptAESCBC(encryptedText, key);
return my.string2utf8(string);
}

// Wraps a string to 60 characters.
my.wrap60 = function(string)
Expand Down
16 changes: 9 additions & 7 deletions cryptico.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion rsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ RSAKey.prototype.doPublic = RSADoPublic;
// public
RSAKey.prototype.setPublic = RSASetPublic;
RSAKey.prototype.encrypt = RSAEncrypt;

RSAKey.prototype.encryptUTF8 = function(text){RSAEncrypt(cryptico.utf82string(text))};
// Version 1.1: support utf-8 decoding in pkcs1unpad2
// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext

Expand Down Expand Up @@ -266,6 +266,7 @@ RSAKey.prototype.setPrivate = RSASetPrivate;
RSAKey.prototype.setPrivateEx = RSASetPrivateEx;
RSAKey.prototype.generate = RSAGenerate;
RSAKey.prototype.decrypt = RSADecrypt;
RSAKey.prototype.decryptUTF8 = function(ctext){return cryptico.string2utf8(RSADecrypt(ctext))}


//
Expand Down