-1

I'm using a DES algorithm I found on the web. It works fine but I have a problem. As you know, DES encrypts/decrypts blocks of 64 bytes each. But what happens if in a big file the last block doesn't end at a 64 byte block boundary? I know, there would be errors.

I'm using the following code:

Des d1;
char *plaintext=new char[64];
char *chyphertext=new char[64];
h.open("requisiti.txt",ios::in|ios::binary);.
k.open("requisiti2.txt",ios::out|ios::binary);
while(!h.eof())
{
    h.read(plaintext,64);
    chyphertext=d1.Encrypt(plaintext);
    //decryption is the same.just change Encrypt to Decrypt
    k.write(chyphertext,64);
}
h.close();
k.close();
remove("requisiti.txt");
rename("requisiti2.txt","requisiti.txt");

So I need a solution like "padding", but I don't know a simple algorithm for it. Please help me to encrypt/decrypt file in a good way.

djf
  • 6,592
  • 6
  • 44
  • 62
gAeT
  • 21
  • 1
  • 5
  • 1
    It's not clear what you're asking... is it that you don't understand what padding is? (Just add 00 bytes to take you to a 64 byte boundary prior to encrypting, and remember they need to be removed after decrypting -- so keep an "original size" value with the encrypted data), or is it that you're looking for someone to write your code for you (in which case, stack overflow is not the place to find that)? – mah Jul 08 '13 at 10:49
  • i know what padding is..but i dont know any algorithm to pad. Before posting my question i have looked for "an answer" on stackoverflow.com but nothing helped me.so i asked directly. I am a beginner, so i know the foundamentals more or less..also tried to Google pad algorithm, but usually they send me to crypto++ or something like that. hope u got it. thank you anyway – gAeT Jul 08 '13 at 10:56
  • The block size of DES is 64 *bits*, not bytes. – President James K. Polk Jul 08 '13 at 10:56
  • You need to identify the encryption library you are using if want help. – President James K. Polk Jul 08 '13 at 10:57
  • @gAeT you don't need an algorithm to fill a buffer with zeros and make sure the entire buffer is provided to your encryption function... don't over-complicate the problem. – mah Jul 08 '13 at 10:58
  • sorry for "bits". by the way...i'm not sure, but i guess i m not using any library.i just have a source (.cpp) without padding algorithm – gAeT Jul 08 '13 at 10:59
  • @mah so what do you suggest me? – gAeT Jul 08 '13 at 11:00
  • @gAeT read my first comment; suggestion is already in it. Based on your sample code, you could get away will filling `plaintext` with all zeros prior to the read (which strictly speaking isnt even necessary)... all you need to add is the original length to your output so that when you decrypt later, you know how much data to return. – mah Jul 08 '13 at 11:01
  • @mah can you please be more specific?should i check for the last block, if it isnt 64 bits long i should add "64-lastblocksize" 0,then when decrypt i should remember the "64-lastblocksize"? – gAeT Jul 08 '13 at 11:04
  • Do not use zero (0x00) padding as it cannot reliably be removed. Use PKCS#7 padding, which can always be removed. Is your question about cypher modes? Those allow a long plaintext to be encrypted when the plaintext exceeds the block size. Use CBC or CTR modes. Do not use ECB mode, it is insecure. See http://en.wikipedia.org/wiki/Padding_(cryptography) for padding and http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation for modes. – rossum Jul 08 '13 at 11:04
  • @rossum it's just a project.don't need the security at all.just wanna make it working PS. i have already read something like CBC CTR etc..but it's too specific for me..maybe i just need the PKCS#7 algorithm..i will check your links later. – gAeT Jul 08 '13 at 11:08

4 Answers4

3

First, I'd like to point that DES works on 64bits chunks (making it 8bytes, not 64), as you can see in http://en.wikipedia.org/wiki/Data_Encryption_Standard (check data block size).

Now you're looking for some padding (and unpadding when deciphering). You can look at http://en.wikipedia.org/wiki/Padding_(cryptography)

I personnally like PKCS#7 because it's easy and usually adds a little overhead compared to standard size.

For encryption:

  • check the size of the chunk you just read from file
  • if it's 64bits, add a new chunk [8,8,8,...8], otherwise, pad it with the number of missing bytes (see example below)
  • encrypt
  • note that LAST packet is always containing padding with that algorithm (worst case is 8 bytes of padding)

Example:

  • read 0a 0b 0c, missing 5 bytes to fit in 8 bytes
  • padded packet :0a 0b 0c 05 05 05 05 05

For decryption :

  • read packet
  • decrypt
  • if it's the last packet, check value of the last byte (say it's n)
  • remove n bytes at the end of your packet

Hope this makes it more clear and helps you

EDIT

If your input file is pure text, you can pad with 0, if it's binary (and it must be since you're opening it as binary), PKCS#7 is better

Think about a file created like that : dd if=/dev/zero of=temp.zero count=100 a few of hundred bytes of zeros, what is padding and what ain't ?

Implementation is really easy :

  • think memset
  • don't forget to add last chunk if ile is a multiple of 8

By the way, DES is nowadays seriously broken, you should think about using a decent cipher if concerned with security (thinking AES at least, check http://en.wikipedia.org/wiki/Data_Encryption_Standard#Replacement_algorithms )

Bruce
  • 7,094
  • 1
  • 25
  • 42
  • i will check it.but it looks more complicated than the 0 padding. – gAeT Jul 08 '13 at 12:38
  • @gAeT, it is just an example – fatihk Jul 08 '13 at 12:39
  • 0-padding is always dangerous if your actual binary file can contains 0 values. How are you sure that you restore the exact same file ? To be sure, you need to store size somewhere. – Bruce Jul 08 '13 at 12:53
  • to test, you can think of a file like that `dd if=/dev/zero of=temp.zero count=100` – Bruce Jul 08 '13 at 12:54
  • i got it.and 1.binary isnt correct.the file should not be binary! 2.too many people telling different things!i dont know what to do! – gAeT Jul 08 '13 at 13:20
  • @Bruce there is no danger in using 0 for padding, however regardless of what you pad with, you need to store the original size. After decrypting, you will end up with whatever you padded with in the first place and you need to be aware of the number of bytes that are of interest (that is, everything that isn't padding). It isn't relevant if your padding bytes cannot be distinguished from normal data by inspection, because you have the size to go on. – mah Jul 08 '13 at 13:26
  • 1
    @gAeT surely by now you realize you're well beyond your abilities with this project. There's nothing wrong with taking on a challenge, but such a challenge doesn't mix well with encryption code -- you can end up causing more harm than good. – mah Jul 08 '13 at 13:27
  • it isnt a challange.i just need the ready made code to implement in a project. i have tried doing it by myself and i have a problem with padding.it would be very helpful if someone would post the right code so i can study it. – gAeT Jul 08 '13 at 13:36
  • 1
    Stack Overflow is _not_ about people providing code for you, but there are other sites where you can arrange to pay people for such work. Stack Overflow is about helping others to understand things, but those others (you in this case) need to have a certain minimal level of understanding to grasp the answer and, no disrespect intended, you are not yet at that minimum level required for the questions you are asking. – mah Jul 08 '13 at 13:42
  • @mah PKCS#7 provides implicit storage of size inside padding, which I like (cause padding is number of bytes used for padding). However, true, if you store size, you're free to use whatever padding you like, like the c0febabe or 00f00ba4 (or even b1971175) – Bruce Jul 08 '13 at 13:54
0

First of all: never use eof() to check whether the end of file is reached since it doesn't predict the end of file.

while(h.read(plaintext,64))
{
    if (std::h.gcount() < 64)   // gcount returns the number of characters extracted by the last unformatted input operation.
        for (int i=std::h.gcount(); i<64; i++)
            paintext[i] = 0; // pad the last block         
    chyphertext=d1.Encrypt(plaintext);
    //decryption is the same.just change Encrypt to Decrypt
    k.write(chyphertext,64);
}
Alexandru Barbarosie
  • 2,952
  • 3
  • 24
  • 46
  • what about the decryption?i have tried to just change Encrypt to Decrypt...but it still gives me errors:in the last part of the file previous character are overwritten to character at the end.. – gAeT Jul 08 '13 at 11:51
  • Basicly you decript all the block the standart way and for the last one you just remove all the trailing zeroes after the decryption. Just use a for loop **starting from the end** of the block and while your `block[i]` is `0` replace it with the NULL character. – Alexandru Barbarosie Jul 08 '13 at 11:58
  • what is the command to start from the end? and replace with NULL character?should i use just =NULL.can you show me?:) – gAeT Jul 08 '13 at 12:03
  • for (int i=0; i<64; i++) if(plaintext[i]==0)plaintext[i]=NULL; I have tried this but still doesnt work.. – gAeT Jul 08 '13 at 12:10
  • 1
    **starting from the end**!! are u just copying the code from other sources without understanding it?? `for (int i= 63; i>0; i--) if (plaintext[i]==0) plaintext[i] = '\0';` – Alexandru Barbarosie Jul 08 '13 at 12:17
  • @gAeT if you have some other issues I advice you to open a separate Q&A specifying the question. – Alexandru Barbarosie Jul 08 '13 at 12:30
  • i have tried with i=63;i>0!!but it didnt work!!in fact now it still doesnt work!it still continue to overwrite data in the last block..and i dont know how to fix it!i have read load of document but still dont get it.if you want to help me i would be glad to you.thank you anyway – gAeT Jul 08 '13 at 12:37
  • "Doesn't work" doesn't say me anything. Get your code together and post a qustion, explaining what doesn't work and mb give an exemple of the output you have with one particular input. – Alexandru Barbarosie Jul 08 '13 at 12:48
0

I'm not sure what you are using DES for, but you should really use something else if you are actually trying to protect the data you are encrypting. DES is not secure anymore.

Also, I would imagine that a good library would do the padding for you.

Richard Fung
  • 1,020
  • 6
  • 14
  • I use DES because it's just a school project..i dont need the security at all..what i am looking for is a library or better a quick code to make padding and unpaddig and discuss about it. so it would be helpful for me and other people who will fall in this trouble. PS. I know its 64 bits. – gAeT Jul 11 '13 at 07:46
0

First of all do not use DES! DES is broken and can be brute forced quite fast. Secondly you are using ECB mode you can read on wiki why you should avoid this one. Your data can be tampered with and you will not know about this - use AE mode like GCM. Like someone mentioned earlier DES have 64 bits not bytes block size which is 8 bytes.

Maciej S
  • 586
  • 4
  • 8