Thread: my first cipher

  1. #1
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299

    Post my first cipher

    this is my first try at making a stream cipher so im not keeping my hopes up.
    i
    m trying to create a one time pad based on a given key.

    i take the users (char*) key and loadit into an unsigned long[256] array and a unsigned char array[1024]

    then do some math than can't be reversed - atleast im hoping this is the case - on the key to make a hash value.
    and store it.
    Code:
        }
        for( int N = 0; N < 16; N++){//repeat everything 16 times
            //cipher the key. uses 'p' the last bite stored from above
            for( i = 0; i < 255; i+=2){
                c->key[i] = c->key[i] ^ (c->key[i+1] + 1);    //xor from the front
                c->key[255-i] = c->key[255-i] ^ c->key[254-i];//xor from the back
                            
                p = 0xFF000000 & (c->key[255-i] << p%24);
                p >>= 24;
                
                //placement and value.
                c->key[i] = c->key[p] + c->key[i];
                
            }
    from this i do some more maths and create a hash for the pad.
    i use the bites from the key to access whatever element it happends to be pointing to and xor it with some stuff
    one of the values i xor it with gets replaced by whatever (g^d) ^ (e^f) happens to be at the time.

    Code:
            //compute the pad
            j = 0;
            for( i = 0; i < 1024; i++){
                p = c->key[j];
                //load key bits 
                g = p & 0x00FF;    p >>= 8;            
                f = p & 0x00FF;    p >>= 8;        
                e = p & 0x00FF;    p >>= 8;
                d = p & 0x00FF;
                
                c->pad[i] = c->pad[i] ^ (d + (e^f));            //basic xor w/ addition
                c->pad[g] = c->pad[d] ^ (c->pad[e] + c->pad[f]);//basic xor w/ addition using pad[f]
                c->pad[f] = (g^d) ^ (e^f);                      //destroying pad[f]
                //please correct me if im wrong but this is basicly a one way hash.
                //i see no way to recover the data at pad[e]. without that then there is no way to decipher?
                
                //and some simple bit shifting.                        
                c->pad[i] = c->pad[i] ^ (c->pad[g] << 4);
                c->pad[i] = c->pad[i] ^ (c->pad[d] >> 4);
                                                                         
                j++;
                if (j >= 256)
                    j = 0;
            }
        }
    the way i figure every 1 or 0 in the key has a chance to affect every other 1/0 in the hash
    at the moment the max key size is 256.
    source posted with this.

    i looking for ways to take the c->key and get the user key from it
    or any way to get the origional text.

    above is just to setup the pad.
    to encript data this is what i've come up with
    Code:
    void xorp::xor(codeblock* c, char* data, int len){
        int p = 0, k = 0;
    
        for( int N = 0; N < 16; N++){
            for( int d = 0; d < len; d++){
       
                data[d] = data[d] ^ c->pad[p];
                data[d] = data[d] ^ c->key[k];
                
                //cycle through the pad.
                p++;
                if (p >= 1024)
                    p = 0;
                
                //cycle through the key.
                k++;
                if (k >= 256)
                    k = 0;          
                
                k = k ^ (c->pad[p] >> k%24) ;//alter k based on data at pad[p]
            }    
        }        
    }
    simple. the data is xor'ed with the pad. then the key hash
    k will be changing alot through the process.
    by shifting k by k%24 i will shift all the bites off and have nothing... ERROR ERROR.. i'll fix this later.
    anyway by shifting k a few bites and xor'ng it with pad[p] it should prevent and data from being xored with the same 2 things twice.
    add on top of that , that this process will be repeated 16 times. making the chances of this very low and shouldn't weaken the cipher any.
    any good links are welcome.
    Last edited by Nor; 11-16-2008 at 09:11 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #2
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    thx.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  3. #3
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Originally posted by Salem
    [B]
    3. In the new C++ standard, 'xor' (which you've used as a function name) is actually a reserved word, and a synonym for the ^ operator
    i'll rename that

    2. Your code only encrypts the first 128 bytes of the message. Everything after that is in cleartext.
    B]
    thx.
    with 1086 input i get
    Code:
    £xgªz┬,úΣ⌡s╠öI/☻←¬?φN←∩r⌡º å■v╔╞gK5k↕ÑG≥G╠♫!♠A·î¥Uq!ºH║o4-≤1►_↨-(D╩>Åï(∟ò(±áGvfÿ
    µ!iH╚╔┬{`Ω╗U6╛|¬m4╝w¡å5=;Φ↨→Φÿ⌂çà♀É:^«☺↔f╗►æΘ⌡3÷■       ÿ┌d└3KK6╩á}¢cOxD→[;Ü╣æ♣Ç
    ƒ╘₧├#(∙é¿'½┘za₧=8°╥├ûƒ∞íUï├è↓°Ö♦└özWeùé_♥▄mτ#·Qoôf┌πàM█Sⁿ▐♠╡Rg6=q↕╒KWâUç¼t╩ƒ┼4[R
    ╥²╪5►_µ⌐Åë▒¥-╚æ┘3]┐╤TE±↕H☺fï▬≤┘▼♀┤↓É∩^Φ╡╓;├2§┌B
    ╒♠f;└πHE|K½ß^¶☺╤♂*▌►=_§ mkí♦8▼dA;bÑ^εS/4▼♠▄96╦Fü╛-vZú··<ùcqσ♀∟-╣ü▬î╕╤☻?└@VjαKu≥e
    o≤ù"Ñ>-Ü☻₧¼/¢¼smâ6░♂µpÖÆóüjdëε╒¬|◄§♠s╘NM
    º╝§╘*+bû╦uvó|G3¶T▒♀'\≡j[$µcΓ╒1?╓É,I»≤╩\sσªz↕}(dE╥6>ÆαÄ←♣c♫6un0▬¡Dj»╜æ]+F╚6▐~╫8rD
    ∙"∙←αµ↨bB8☻g#╫,╠ µ⌡Ñ1g?+Ñ└      :┐w─"δδ╙Y\¬![▓.î&âÜE¶┼-BΦuR═µσ╣╛⌂╓sPq%♦☺Σ▓[.ñ╦sí
    ∙⌠☻2ëêSa═îÜ▲6|O\♦BâÜŬ☻n :Æ║╡D⌐▒\Φk╘π╨~╩A^é½p_+╬c╟═j◄P╔p┼-⌂╧£╞└♥╠▐&≤►Z♦Ä|!h¥s`ôΣ
    :|
    òó┤O²J+,┤╟Cà☼╫(vJ╧╡ÑT╜$è+╒<èûƒ,┐àW╤b&☼p*Σù≡║Æ≡┤+ûα      ╥       )÷»ö≥═☼½üŧ·-ç↔d
    σm↓r¶░┼║Öx┤]¬♦è*%H≤Nä0q▲        u╛╞@▼]âQ~■↑╧l→£O£"n∙┐\╤Ñß└8R^&♦ò#¼┘▌ɬÄi♀■º?└ ∩\
    äô@⌠o{v↔╤?►XN╧c<╣╢i╬µüi▌;ûüü├½bδ╧Zï→ @Γ;∟nL╪╟nP≤☺+╤j≈Eÿ.₧√Xk╓╘1╗V≈xφ3∩}2↔ù√U╛}╩[
    ┬-s8L┴∩F├►<πìF[_hx♥▀'♣_$ÉéÉ4S▼╝ê¥╪Σx∩2#╫¬6úÉ♠c(╒►¿Ow♫;b↕╤ê¼y∙Γ═Ωê╩)←é ê╢8%±[*πíì
    Åkb♣Uocα4 m│ó:╛┐⌂ßî▓╩lH►
    ╚~╓┴╛ │╟àäa`╤·
    origional data: you will never read this. I hope................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ................................................................................
    ..
    can you post your code i dont see any plaintext.
    the driver in xor.cpp will print the decripted data to the screen
    [edit]
    k. i've ciphered 10megs of data and found no plaintext in the stream.
    i'm going to rewrite the code using persion types and see if this fixes it.
    Last edited by Nor; 07-09-2003 at 12:12 AM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  4. #4
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    there must be a porting problem or something.
    i can't find any plaintext in the output.

    never used a linux system fulltime.
    od -Ax -xc hex.bin
    what's your prams do?
    Last edited by Nor; 07-09-2003 at 01:26 AM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    I have a question regarding encryption which I've wondered about for sometime. Thought this may be a reasonable place to ask it.

    Assuming I have a decent stream cipher & some encypted data, for which a password (key) is required in order to decrypt that data, how can I devise a routine to check whether a key entered by the user is the correct one needed for decryption. I have two thoughts:

    1. Certain bytes in the data decrypt to KNOWN values. Hence, a program can compare the decrypted ones to hardcoded values - if they match - the key is correct. However, I KNOW that this would be a serious weakness because it would give a hacker a handle on the key.

    or

    2. The key used to encrypt (or decrypt) the data is stored in the data, but encypted along with the data with itself. That way, the stored key can be decrypted with the 'trial key' entered by a user, and then compared.

    To my mind option 2 seems the better, but I'm not sure what weakness this would involve (if any). Is there a standard way to approach this problem?

    Anybody know?

  6. #6
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Hi Salem, I think thats useful. I can see that there maybe some passwords which, when tested against an encrypted test password, will match but the resulting decrypted data will be garbage. I'm not sure it answers my question, or at least I don't follow.

    Can I re-phrase my question.

    I save a document in MS Word using password protection. The document will be encrypted. If I then go to open the document, but enter the wrong password, MS Word tells me I've entered an incorrect password. How does it know it is an incorrect password?

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Davros, another thought. Before encrypting your messages, hash them, and append that value to the end. When you decrypt, take everything except the last 256-bits (or whatever it happens to be), hash it, and compare against that last block of data.

    A couple other things to 'google' for: differential cryptanalysis, linear cryptanalysis. There's an introductory paper on cryptanalysis on www.counterpane.com as well.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    >Before encrypting your messages, hash them, and append that value to the end.

    Hi Zach. Thanks for that - sounds like a good solution.

    Thanks also for the links.

  9. #9
    ‡ †hë Ö†hÈr sîÐè ‡ Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    another method is to pad the first section of data with a random number of random bits then have something to search for i.e."\r\n\r" showing the start of your data

    with the above cipher i get 203 bits matching in the cipher and text. when i change the key i start getting plain text.
    not sure whats happeneing but i got softice loaded and should only take a few minutes
    Last edited by Nor; 07-09-2003 at 01:25 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Cipher Program
    By PunchOut in forum C Programming
    Replies: 7
    Last Post: 11-22-2008, 01:12 PM
  2. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Substitution Cipher Program Problem...
    By Junior89 in forum C++ Programming
    Replies: 13
    Last Post: 12-28-2005, 05:02 PM
  5. My little Caesar cipher emulator
    By dead_cell in forum C++ Programming
    Replies: 3
    Last Post: 01-16-2004, 01:05 AM