Data encryption and wx Topic is solved
-
- Experienced Solver
- Posts: 77
- Joined: Wed Jul 27, 2005 2:52 pm
- Location: Ostrava, Czech Republic
Data encryption and wx
Is there any way to encrypt/decrypt data using wx? Let's start with CBC/ECB modes of AES. I know there are possibilities to do this in multiplatform way but I haven't seen any using wxWidgets.
-
- Moderator
- Posts: 3971
- Joined: Fri Aug 27, 2004 9:38 pm
- Location: Delft, Netherlands
I don't think there is. However you could write an wxInputStream or wxOutputStream derived class that handles encryption. I would surely be interested in it .. Using the streams has the huge advantage you can chain them together to first compress your data with a wxZlibOutputStream and then send it through a crypt stream.
- Jorgen
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
-
- Experienced Solver
- Posts: 77
- Joined: Wed Jul 27, 2005 2:52 pm
- Location: Ostrava, Czech Republic
To be honest, I don't like streams (something that often has neither beginning nor end is scaring). I code software for proprietary hardware and I can't accept any form of 'smart' buffering in any way, so streams are not exactely what I need. But of course, encrypted and compressed stream is very neat solution and to be honest, I will think of it.
-
- Experienced Solver
- Posts: 77
- Joined: Wed Jul 27, 2005 2:52 pm
- Location: Ostrava, Czech Republic
-
- Moderator
- Posts: 3971
- Joined: Fri Aug 27, 2004 9:38 pm
- Location: Delft, Netherlands
Ofcourse a stream has a beginning and an end. When it's destroyed it is ended. When it begins, is the first time you are writing or reading from it. You simply have to keep an internal state of your data. In case of seekable streams, that will be hard with encryption and archiving.
A very simple encryption stream can be (override wxOutputStream):
Which is a VERY LAME xor based crypter that takes the previous byte and xors it with the current. When you keep state and you know the last byte you read you can continue decrypting it later. It is even seekable, because when the position > 0 the byte before the current can always be consulted for the last seed. This is ofcourse by no means encryption, but only to illustrate how easy a stream can be.
And you construct the stream giving it an wxOutputStream as argument, which can be a file. Like;
The big advantage is you can write to memory, sockets, files, zlib streams, into strings etc. When you are ysing a raw buffer approach you are always converting from one to the other.
- Jorgen
A very simple encryption stream can be (override wxOutputStream):
Code: Select all
YourCryptOutputStream::Write(void *ptr, size_t size)
{
if(size > 0)
{
unsigned char *tmpbuf = new unsigned char[size];
if(m_firstTimeWrite)
{
m_lastByte = 0xabcd;
m_firstTimeWrite = false;
}
for(size_t i = 0; i < size; i++)
{
tmpbuf[i] ^= m_lastbyte;
m_lastbyte = tmpbuf[i];
}
m_ostr.Write(tmpbuf, size);
delete[] tmpbuf;
}
}
And you construct the stream giving it an wxOutputStream as argument, which can be a file. Like;
Code: Select all
wxFileOutputStream outfile("somename.txt");
YourCryptOutputStream crypter(outfile);
crypter.Write(your_buffer, your_size);
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
-
- Moderator
- Posts: 3971
- Joined: Fri Aug 27, 2004 9:38 pm
- Location: Delft, Netherlands
Addition to my post, it would ofcourse be better to give a start passphrase to the stream create function like;
Which ofcourse you do not hard code like this in your software or use an MD5 algoritm to create a hash code that you use.
Code: Select all
wxFileOutputStream outfile("somename.txt");
YourCryptOutputStream crypter(outfile, _T("your start passphrase");
crypter.Write(your_buffer, your_size);
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
-
- Experienced Solver
- Posts: 77
- Joined: Wed Jul 27, 2005 2:52 pm
- Location: Ostrava, Czech Republic
Well, thanks Jorg for your ideas, I'll consider everything you wrote.
Just curious about performance - when writing e.g. into string, how can be stringbuffer aware of amount of incoming data? Is it allocated with certain granularity and resized if needed? Is there any 'hint' for stream to tell it 'here are 10 bytes for you, 4kB will follow'? I've noticed you are experienced stream user, so I hope you know the truth
Just curious about performance - when writing e.g. into string, how can be stringbuffer aware of amount of incoming data? Is it allocated with certain granularity and resized if needed? Is there any 'hint' for stream to tell it 'here are 10 bytes for you, 4kB will follow'? I've noticed you are experienced stream user, so I hope you know the truth
-
- Moderator
- Posts: 3971
- Joined: Fri Aug 27, 2004 9:38 pm
- Location: Delft, Netherlands
Yep wxString works with granularity. It allocates a certain number of bytes. Not on the first go (I think the footprint is 32 bytes for an empty string) but with subsequent calls it will allocate a certain amount.
For a stream that works the same. You are in no way obliged to send all the data you get right away. For example if your encryption algoritm works on blocks of 32 bytes, you can keep them in your internal buffer, and write when there is more on the way.
Just remember to write the last bits when you are destroyed or closed. You can even make it an adaptive learning stream and remember the last block size and keep that pre-allocated in mem for subsequent calls.
Regards,
- Jorgen
For a stream that works the same. You are in no way obliged to send all the data you get right away. For example if your encryption algoritm works on blocks of 32 bytes, you can keep them in your internal buffer, and write when there is more on the way.
Just remember to write the last bits when you are destroyed or closed. You can even make it an adaptive learning stream and remember the last block size and keep that pre-allocated in mem for subsequent calls.
Regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
-
- Experienced Solver
- Posts: 70
- Joined: Sat Oct 25, 2008 2:38 am
-
- Filthy Rich wx Solver
- Posts: 232
- Joined: Sun Nov 29, 2009 10:35 am
Is it possible to do this to a database? For example, say I wanted to encrypt a database so the end-user cannot read it (done for copyright purposes) but the application can still decrypt the database and read it as normal. Sure I have the extra step of decrypting but hopefully that's not a large performance hit.
Thanks!
Thanks!
-
- Moderator
- Posts: 3971
- Joined: Fri Aug 27, 2004 9:38 pm
- Location: Delft, Netherlands
I think if you use SQLite you can (as suggested before somewhere on this forum) specify it to be encryptd automatically.
http://www.hwaci.com/sw/sqlite/see.html
Too funny how it is called "SEE" while it's purpose is that you can't see a bloody thing afterwards
With regards,
- Jorgen
http://www.hwaci.com/sw/sqlite/see.html
Too funny how it is called "SEE" while it's purpose is that you can't see a bloody thing afterwards
With regards,
- Jorgen
Forensic Software Engineer
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb
Netherlands Forensic Insitute
http://english.forensischinstituut.nl/
-------------------------------------
Jorg's WasteBucket
http://www.xs4all.nl/~jorgb/wb