Forum > Windows (32/64)
Error: Project raised exception class 'External:SIGSEGV'.
rvk:
--- Quote from: karmacomposer on December 08, 2020, 10:04:39 pm ---
--- Code: --- Cipher.EncryptCBC(Data[1], Buffer[1], varDataLen);
--- End code ---
--- End quote ---
You still need to pass Length(Data) as third parameter.
Third parameter is the length of data you want to encrypt. Not the size of the buffer.
But looking at your example, your Data is already a multiple of 16 (i.e. 16 exactly). So it should work fine. There would be no need to round the buffer to a 16 block (128 bits) size because it's already at that size. This would be needed if your string is not exactly multiple of 16 characters. See https://stackoverflow.com/a/41290878
But what happens if you use 128 in the Cipher init (like in the original example)?
You passed 256 (why?) which would indicate a key of 32 characters (while your key is just 16 characters).
2nd param to init needs to be keysize in bit (so * 8 ).
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Cipher := TDCP_rijndael.Create(nil);Cipher.Init(Key1, Sizeof(Key1) * 8, nil);Cipher.EncryptECB(InData, Block);https://wiki.freepascal.org/DCPcrypt
karmacomposer:
--- Quote from: rvk on December 08, 2020, 11:26:40 pm ---
--- Quote from: karmacomposer on December 08, 2020, 10:04:39 pm ---
--- Code: --- Cipher.EncryptCBC(Data[1], Buffer[1], varDataLen);
--- End code ---
--- End quote ---
You still need to pass Length(Data) as third parameter.
Third parameter is the length of data you want to encrypt. Not the size of the buffer.
But looking at your example, your Data is already a multiple of 16 (i.e. 16 exactly). So it should work fine. There would be no need to round the buffer to a 16 block (128 bits) size because it's already at that size. This would be needed if your string is not exactly multiple of 16 characters. See https://stackoverflow.com/a/41290878
But what happens if you use 128 in the Cipher init (like in the original example)?
You passed 256 (why?) which would indicate a key of 32 characters (while your key is just 16 characters).
2nd param to init needs to be keysize in bit (so * 8 ).
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Cipher := TDCP_rijndael.Create(nil);Cipher.Init(Key1, Sizeof(Key1) * 8, nil);Cipher.EncryptECB(InData, Block);https://wiki.freepascal.org/DCPcrypt
--- End quote ---
I need to use AES 256, so how would I achieve that with a 32 character key? I already have the ability to create/trim 32 character keys.
Mike
rvk:
--- Quote from: karmacomposer on December 09, 2020, 03:50:18 pm ---I need to use AES 256, so how would I achieve that with a 32 character key? I already have the ability to create/trim 32 character keys.
--- End quote ---
Make your Key 32 bytes long.
Also... the Data string should be a multiple of 16.
In your case it happens to be that size but if not you can add the following to make sure.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (Length(Data) mod 16 > 0) then for I := 1 to (16 - (Length(Data) mod 16)) do Data := Data + AnsiChar(0);
If you still get an error you'll need to provide compilable code and input for reproducing this error.
karmacomposer:
--- Quote from: rvk on December 09, 2020, 04:16:40 pm ---
--- Quote from: karmacomposer on December 09, 2020, 03:50:18 pm ---I need to use AES 256, so how would I achieve that with a 32 character key? I already have the ability to create/trim 32 character keys.
--- End quote ---
Make your Key 32 bytes long.
Also... the Data string should be a multiple of 16.
In your case it happens to be that size but if not you can add the following to make sure.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (Length(Data) mod 16 > 0) then for I := 1 to (16 - (Length(Data) mod 16)) do Data := Data + AnsiChar(0);
If you still get an error you'll need to provide compilable code and input for reproducing this error.
--- End quote ---
Thank you for this. So, since the data could be any size, from as small as a first name to several paragraphs in size, the above code will simply make sure
it is divisible by 16, correct? As far as making the key 32 bytes long, won't a 32 character key be 32 bytes long?
Thank you very much. Your help is invaluable.
Mike
rvk:
--- Quote from: karmacomposer on December 09, 2020, 04:57:58 pm ---Thank you for this. So, since the data could be any size, from as small as a first name to several paragraphs in size, the above code will simply make sure
it is divisible by 16, correct?
--- End quote ---
Yes, the Data should be divisible by Cipher.BlockSize div 8 (which is 128 div 8 = 16).
TDCP_rijndael is inherited from TDCP_blockcipher128 and for that one the standard blocksize is 16, 128 bits.
So you could use Cipher.BlockSize div 8 in the code but because for rijndael it's always 128 bits (16 bytes) you can hardcode the padding to a multiple of 16 characters.
Also... if you look in TDCP_blockcipher128.EncryptCBC() you can see the encoding is done in blocks of 16.
(Set your cursor on .EncryptCBC( and press Alt+Arrow Up and then Shift+Ctrl+Arrow Down. You get right into the source there)
You can see it does blocks of 16 bytes from the for i:= 1 to (Size div 16) and Move(p1^,p2^,16);
--- Quote from: karmacomposer on December 09, 2020, 04:57:58 pm ---As far as making the key 32 bytes long, won't a 32 character key be 32 bytes long?
--- End quote ---
Yes, a 32 character long ansistring is 32 bytes long. But make sure it's an ansistring and not a utf8 string (which can be longer due to character encoding with special characters).
You could also padd the key if it's shorter than 32 characters. But that might weaken your AES encryption.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---if (Length(Key) < 32) then while Length(Key) < 32 do Key := Key + AnsiChar(0);Key := Copy(Key, 1, 32); // for the instance Length(Key) > 32
Navigation
[0] Message Index
[#] Next page
[*] Previous page