Open GoogleCodeExporter opened 8 years ago
Hi there,
It is strange. I ran piece of code in Windows Phone 7.5 (Mango) project and it
seems to be problem of framework itself. Nothing in common with sqlite as far
for me.
Try this sample code (paste in new WP7 Mango project):
byte[] bIn = new byte[1024];
for (int i = 0; i < 1024; ++i)
{
bIn[i] = (byte)(i % 256);
}
byte[] bOut = new byte[1024];
AesManaged Aes = new AesManaged();
ICryptoTransform ict = Aes.CreateEncryptor(
new byte[]{191, 43, 134, 145, 128, 154, 235, 192, 76, 83, 56,
97, 29, 40, 219, 152, 136, 43, 30, 104, 200, 197, 243,
254, 58, 190, 183, 143, 216, 142, 47, 247},
new byte[]{0x53, 0x51, 0x4c, 0x69, 0x74, 0x65, 0x20, 0x66,
0x6f, 0x72, 0x6d, 0x61, 0x74, 0x20, 0x33, 0x00});
MemoryStream dataStream = new MemoryStream();
CryptoStream encryptionStream;
encryptionStream = new CryptoStream(dataStream, ict, CryptoStreamMode.Write);
encryptionStream.Write(bIn, 0, size);
encryptionStream.FlushFinalBlock();
dataStream.Position = 0;
dataStream.Read(bOut, 0, (int)dataStream.Length);
encryptionStream.Close();
dataStream.Close();
This piece of code causes the same problem of encryption byte[1024] into
byte[1040] array.
Sounds interesting, doesn't it?
Original comment by bWeg...@gmail.com
on 27 Jan 2012 at 7:22
It is because of extra bytes due to AesManaged padding.
Original comment by bWeg...@gmail.com
on 27 Jan 2012 at 10:13
The encrypted form is 16 bytes bigger, and the IV is 16 bytes...co-incidence?
I think not. Maybe AESManaged adds the IV by default?
Original comment by p...@inalysis.com
on 27 Jan 2012 at 10:16
I have found solution.
Must replace original encrypting algorithm used in sqlite3 because sqlite3
requires that encrypted data buffer is the same size as the incoming
unencrypted data buffer.
I used an implementation from
http://www.codeproject.com/Articles/7546/A-CBC-Stream-Cipher-in-C-With-wrappers-
for-two-ope (by author: vcepa) and changed it.
Changed immplementation such that padding is disabled (as required by sqlite3).
Size of data buffer passed to encryption algorithm must be multiplicity of AES
block size (in this case 16B). That is not an issue in sqlite3 because it is
based on 1024B pages and 1024 % 16 = 0.
I hope it will be done in future releases of sqlite3 implementations.
Fell free to contact me if you need details (by posting a comment).
Original comment by bWeg...@gmail.com
on 2 Mar 2012 at 10:30
bWeg just found the same error, tried to use the solution you provide but cant
find CreateEncryptor/CreateDecryptor.
Can you attach the solution?
Original comment by jvi...@gmail.com
on 5 Mar 2012 at 10:05
I started with http://viciproject.com/wiki/Projects/CoolStorage/WindowsPhone.
Really nice sqlite3 wrapper based on MIT licence. I ran some tests and figured
out that cipher implementation in sqlite3 port is broken for Windows Phone. It
is based on Silverlight framework's AesManaged class which makes endcoded data
bigger than the original data. An author of that port made an assumption that
size of both incoming and outgoing buffers are the same. Unfortunately
AesManaged class in opposition to 'full' .NET Framework's AesManaged class does
not have enum property called Padding. This switcher determines to add or not
an extra padding bytes to outgoing encrypted stream. Padding bytes are
responsible for difference in size. In fact it makes this port to Mango not
working with db encryption. I managed to work around this issue by using custom
encryption algorithm based on publicly accessible code of AES encryption posted
at
http://www.codeproject.com/Articles/7546/A-CBC-Stream-Cipher-in-C-With-wrappers-
for-two-ope. I used mentioned algorithm and change it to not add padding bytes
to achieve either incoming and outgoing cipher byte buffers to be the same size.
Cannot attach my source because it was done as a part of my full time job and I
did not receive permission from my employee to do so. Sorry.
Original comment by bWeg...@gmail.com
on 19 Mar 2012 at 10:33
I have just fixed it as I need it in my current project.
https://code.google.com/r/ivankirkorau-csharp-sqlite-encrypted/source/detail?r=e
8c2c0ea82569f8c79d29fc724e58fbe97af7fef
Check it out. It works as it applies the fix around AesManaged that adds IV by
default and requires one already in place to decrypt. Also pageSize change with
reserved space should be enabled when you set a key or hexkey.
Original comment by ivan.kirkorau
on 11 Jul 2013 at 1:17
Original issue reported on code.google.com by
p...@inalysis.com
on 16 Jan 2012 at 5:29