Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

tommedema
Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

John A. De Goes

Yes there are. You should first encode the length, then remove it during decode and ignore any bytes after that.

Regards,

John

On Jul 12, 2010, at 5:39 AM, Tom wrote:

Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

tommedema
How would you encode the length together with some other object you actually are encoding?

- Tom

2010/7/12 John A. De Goes <[hidden email]>

Yes there are. You should first encode the length, then remove it during decode and ignore any bytes after that.

Regards,

John

On Jul 12, 2010, at 5:39 AM, Tom wrote:

Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

John A. De Goes

You reserve 8 bytes for the length. When encrypting, you first write the total number of bytes you will be encrypting. Then when decrypting, you read that number before reading the object byte data.

Regards,

John

On Jul 12, 2010, at 7:19 AM, Tom wrote:

How would you encode the length together with some other object you actually are encoding?

- Tom

2010/7/12 John A. De Goes <[hidden email]>

Yes there are. You should first encode the length, then remove it during decode and ignore any bytes after that.

Regards,

John

On Jul 12, 2010, at 5:39 AM, Tom wrote:

Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

tommedema
How would I write the number of bytes in exactly 8 bytes all the time?

Say I have objectBytesData:BytesData, and I want to add the number of bytes at the start of this bytes data, how would you do this?

2010/7/12 John A. De Goes <[hidden email]>

You reserve 8 bytes for the length. When encrypting, you first write the total number of bytes you will be encrypting. Then when decrypting, you read that number before reading the object byte data.

Regards,

John

On Jul 12, 2010, at 7:19 AM, Tom wrote:

How would you encode the length together with some other object you actually are encoding?

- Tom

2010/7/12 John A. De Goes <[hidden email]>

Yes there are. You should first encode the length, then remove it during decode and ignore any bytes after that.

Regards,

John

On Jul 12, 2010, at 5:39 AM, Tom wrote:

Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

John A. De Goes

You have to create a new BytesData, first write the size of the following bytes (reserving 8 bytes for it), then copy in all the data from the preceding bytes data.

Regards,

John

On Jul 12, 2010, at 11:45 AM, Tom wrote:

How would I write the number of bytes in exactly 8 bytes all the time?

Say I have objectBytesData:BytesData, and I want to add the number of bytes at the start of this bytes data, how would you do this?

2010/7/12 John A. De Goes <[hidden email]>

You reserve 8 bytes for the length. When encrypting, you first write the total number of bytes you will be encrypting. Then when decrypting, you read that number before reading the object byte data.

Regards,

John

On Jul 12, 2010, at 7:19 AM, Tom wrote:

How would you encode the length together with some other object you actually are encoding?

- Tom

2010/7/12 John A. De Goes <[hidden email]>

Yes there are. You should first encode the length, then remove it during decode and ignore any bytes after that.

Regards,

John

On Jul 12, 2010, at 5:39 AM, Tom wrote:

Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: Cafeine AES encryption, filling uncompleted blocks for encryption and decrypt them later

tommedema
My question was how you would write the size of the following bytes. :)

- Tom

2010/7/12 John A. De Goes <[hidden email]>

You have to create a new BytesData, first write the size of the following bytes (reserving 8 bytes for it), then copy in all the data from the preceding bytes data.

Regards,

John

On Jul 12, 2010, at 11:45 AM, Tom wrote:

How would I write the number of bytes in exactly 8 bytes all the time?

Say I have objectBytesData:BytesData, and I want to add the number of bytes at the start of this bytes data, how would you do this?

2010/7/12 John A. De Goes <[hidden email]>

You reserve 8 bytes for the length. When encrypting, you first write the total number of bytes you will be encrypting. Then when decrypting, you read that number before reading the object byte data.

Regards,

John

On Jul 12, 2010, at 7:19 AM, Tom wrote:

How would you encode the length together with some other object you actually are encoding?

- Tom

2010/7/12 John A. De Goes <[hidden email]>

Yes there are. You should first encode the length, then remove it during decode and ignore any bytes after that.

Regards,

John

On Jul 12, 2010, at 5:39 AM, Tom wrote:

Hi there,

I'm working with the Cafeine encryption class: http://caffeine-hx.googlecode.com/svn/trunk/ext2/chx/crypt/Aes.hx

The only limitation this class has is that it only allows you to encrypt/decrypt fixed size blocks of bytes, not just any length of bytes.

I created two methods to overcome this by filling up the last block with 0 bytes. On decryption, these 0 bytes are removed.

Like this:

private static function decryptBytes(bytes:Bytes):Bytes {
if (bytes.length % aes.blockSize != 0) throw("invalid bytes length");
//create blocks
var bytesData:BytesData = bytes.getData();
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...(Std.int(bytesData.length / aes.blockSize))) {
blocks.push(Bytes.ofData(bytesData.splice(0, aes.blockSize)));
}
//decrypt blocks
var decryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
decryptedBytesData = decryptedBytesData.concat(aes.decryptBlock(blocks[i]).getData());
}
//remove 0 bytes
var index:Int = decryptedBytesData.length - 1;
var currentByte:Int = 0;
while (currentByte == 0) {
currentByte = decryptedBytesData[index];
if (currentByte == 0) decryptedBytesData.pop();
index--;
}
return Bytes.ofData(decryptedBytesData);
}
private static function encryptBytes(bytes:Bytes):Bytes {
//fill last block
var data:BytesData = bytes.getData();
while (data.length % aes.blockSize != 0) {
data.push(0);
}
bytes = Bytes.ofData(data);
//encrypt each block
var blocks:Array<Bytes> = new Array<Bytes>();
for (i in 0...Math.ceil(bytes.length / aes.blockSize)) {
blocks.push(aes.encryptBlock(bytes.sub(i * aes.blockSize, aes.blockSize)));
}
//concat blocks
var encryptedBytesData:BytesData = new BytesData();
for (i in 0...blocks.length) {
encryptedBytesData = encryptedBytesData.concat(blocks[i].getData());
}
return Bytes.ofData(encryptedBytesData);
}


Now, this works great for strings. My question is if there are any legit 0 bytes in objects? Because if there are, the above methods would not work since bytes that need to be kept would be removed.

Regards,
Tom
--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org

--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org


--
haXe - an open source web programming language
http://haxe.org