Re: mcrypt ncfb mode problem.



Thnx for your answer.

I know that this is a block cipher, but when i command the device to
send encrypted command "show_list", all the device sends is a 9 bytes
of data and not 16 as it should.



Peter Pearson je napisal:
On Mon, 04 Jun 2007 05:50:06 -0700, Mario Munda <mario.munda@xxxxxxxxx> wrote:
[snip]
When the device sends data which is less than a block size, the data
gets corrupted.

This is my code:

#include <stdio.h>
#include <mcrypt.h>

int main()
{
char Key[32] =
{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,
0x0E,0x0F,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08$
char IV[16] = {0x8F,0x83,0x28,0xDF,0xC1,0x3A,0x5A,
0x96,0xF3,0xF1,0x8C,0x4D,0x25,0xA9,0xBE,0x04};

char Data[16] = {0x12,0x76,0xDE,
0x19,0x36,0xF9,0xD5,0x88,0x86,0x8E,0x84,0xE0,0x30,0x3E,0xB6,0x2E};
char Data2[6] = {0xB9,0x39,0x88,0x1A,0xAB,0x00};
char Data3[16] = {0xFE,0xB2,0xB4,0x60,0x9E,0xEE,
0x25,0x8C,0x18,0xB8,0xB4,0xB6,0x2A,0x91,0x0A,0xAE};
char Data4[16] = {0xF2,0x19,0xAB,0x89,0xBF,0x5D,
0x33,0x40,0x4F,0xE3,0x71,0xEB,0x5B,0x4F,0xB5,0xC3};

MCRYPT crypt_handle =
mcrypt_module_open(MCRYPT_RIJNDAEL_128,NULL,"ncfb",NULL);

mcrypt_generic_init(crypt_handle,Key,32,IV);

// First string, output should be abcdefghijklmnop
mdecrypt_generic(crypt_handle,Data,16);

for(int i=0;i<16;i++)
printf("%c",Data[i]);

printf("\n");

// Second string, output should be ABCDEF
mdecrypt_generic(crypt_handle,Data2,6);

for(int i=0;i<6;i++)
printf("%c",Data2[i]);

printf("\n");

// Third string, output should be klmnopqrstuvwxyz
mdecrypt_generic(crypt_handle,Data3,16);

for(int i=0;i<16;i++)
printf("%c",Data3[i]);

printf("\n");

// Fourth string, output should be klmnopqrstuvwxyz
mdecrypt_generic(crypt_handle,Data4,16);

for(int i=0;i<16;i++)
printf("%c",Data4[i]);

printf("\n");

mcrypt_generic_end(crypt_handle);

return 0;
}


[snip]
command, and the output of the binary is:

abcdefghijklmnop
ABCDEF
klmnopqrstt¸G
ߺx×£E­®.Ê$g

I don't know mcrypt, but I believe you're printing the results
before the decryption is complete. Remember that Rijndael works
on 16-byte blocks. If the decryption subroutine accepts ciphertexts
that aren't multiples of 16 bytes, then you should expect to
issue some kind of call that says, "That's all the ciphertext; go
ahead and finish the decryption" before having all the plaintext.
Note that the transition between plaintext and nonsense occurs
on a 16-byte boundary:

abcdefghijklmnop ABCDEFklmnopqrst t¸Gߺx×£E­®.Ê$g
................ ................ |..........
0123456789abcdef 0123456789abcdef 0123456789a

--
To email me, substitute nowhere->spamcop, invalid->net.

.



Relevant Pages

  • Re: Mathematics of the Enigma cipher?
    ... string Enigma::Encrypt ... A character is mapped to an integer which becomes ... stationary until the clear character is converted to a cipher ... char val3=R2.GetCharacterIndex; ...
    (comp.programming)
  • Re: How to add thousand separators
    ... First, this code is obsolete as written, because char is a dead data type and should not ... Note that both of these should be stored as string resources since they might need to be ... 18 digits for any reason. ... you have made a VERY SERIOUS DESIGN ERROR. ...
    (microsoft.public.vc.mfc)
  • Re: what is the best way of passing floats into a string
    ... I do not null-terminate as snprintf takes care of this (according to ... But the easiest way to determine the size needed to format a number, ... int length_of_representation(double n,const char* format){ ... I get a nice result of -10.000000 in my char * string. ...
    (comp.unix.programmer)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: [OT] My First C# (warning - long post)
    ... cut me some slack - show me a COBOL program with less than 15 working-storage variables! ... Yes, the Trimis probably extra now, but that was my attempt to get it to quit griping at me that I had given it a "String", when it wanted a "char". ... public string IBreturn ...
    (comp.lang.cobol)