Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ 1 Time Pad Optimization


yours31f's Avatar
Retired
10 0

I wrote a code in C++ to try to brute force the 1-time pad method. Basically the issue is that it goes through the loops about 5000 times in about two seconds then it gets unbearably slow. I know I would never crack it with the current code, but I have no clue how I can further optimize it. Any ideas would be great.

#include <windows.h>
#include <cstdlib> 
#include <ctime> 
#include <iostream>
#include <string.h>
using namespace std;
int main(){
    string binary[26];      // for binaryary letterbet
    string letter[26];     // ASCII letterbet
    string text[3];       // 1 Time Pad Packets 
    string key;          // binaryary Key To decryptpt 1 Time Pad
    string out;         // Hold the "decryptpted" binaryary
    string decrypt[4]; // Holds the 1 byte string to decrpyt
    binary[0]  = "01100001";
    binary[1]  = "01100010";
    binary[2]  = "01100011";
    binary[3]  = "01100100";
    binary[4]  = "01100101";
    binary[5]  = "01100110";
    binary[6]  = "01100111";
    binary[7]  = "01101000";
    binary[8]  = "01101001";
    binary[9]  = "01101010";
    binary[10] = "01101011";
    binary[11] = "01101100";
    binary[12] = "01101101";
    binary[13] = "01101110";
    binary[14] = "01101111";
    binary[15] = "01110000";
    binary[16] = "01110001";
    binary[17] = "01110010";
    binary[18] = "01110011";
    binary[19] = "01110100";
    binary[20] = "01110101";
    binary[21] = "01110110";
    binary[22] = "01110111";
    binary[23] = "01111000";
    binary[24] = "01111001";
    binary[25] = "01111010";
    letter[0] =  "a";
    letter[1] =  "b";
    letter[2] =  "c";
    letter[3] =  "d";
    letter[4] =  "e";
    letter[5] =  "f";
    letter[6] =  "g";
    letter[7] =  "h";
    letter[8] =  "i";
    letter[9] =  "j";
    letter[10] = "k";
    letter[11] = "l";
    letter[12] = "m";
    letter[13] = "n";
    letter[14] = "o";
    letter[15] = "p";
    letter[16] = "q";
    letter[17] = "r";
    letter[18] = "s";
    letter[19] = "t";
    letter[20] = "u";
    letter[21] = "v";
    letter[22] = "w";
    letter[23] = "x";
    letter[24] = "y";
    letter[25] = "z";
    text[0] = "00001001000010010000110000010110";
    text[1] = "00000011000001000001101000011000";
    text[2] = "00011011000001000001111000010110";
    int count=0;
    cout<<"\t\t\tBruteForcing Initiated \n\t\t";
    for (int a=0;a<26;a++){
        for (int b=0;b<26;b++){
            for (int c=0;c<26;c++){
                for (int d=0;d<26;d++){
                    
                    key = binary[a]+binary[b]+binary[c]+binary[d]; // Sets the key to the 4 part binaryary key
                                              
                    for (int e=0;e<32;e++){             // 32 part loop for the 32 bit enc
                        if (key[e]==text[0][e]){        // if bit "a" in key ==  bit "b" in text 
                           out = out + "0";             // add a "0" to out
                           }                            //
                        if (key[e]!=text[0][e]){        // else 
                           out = out + "1";             // add a "1"
                           }
                    } 
                    for (int t=0;t<8;t++){
                   decrypt[0] =decrypt[0]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 1-8 part loop for the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<"\n -- Packet 1 = "<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=8;t<16;t++){
                   decrypt[1] =decrypt[1]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 9-16 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=16;t<24;t++){
                   decrypt[2] =decrypt[2]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 17-24 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=24;t<32;t++){
                   decrypt[3] =decrypt[3]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 25-32 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g]<<"\nKey: "<<letter[a]<<letter[b]<<letter[c]<<letter[d]<<"\n";
                                 g=26;
                        }
                    }for (int e=0;e<32;e++){            // 32 part loop for the 32 bit enc
                        if (key[e]==text[1][e]){        // if bit "a" in key ==  bit "b" in text 
                           out = out + "0";             // add a "0" to out
                           }                            //
                        if (key[e]!=text[1][e]){        // else 
                           out = out + "1";             // add a "1"
                           }
                    } 
                    for (int t=0;t<8;t++){
                   decrypt[0] =decrypt[0]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 1-8 part loop for the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<"\n -- Packet 2 = "<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=8;t<16;t++){
                   decrypt[1] =decrypt[1]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 9-16 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=16;t<24;t++){
                   decrypt[2] =decrypt[2]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 17-24 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=24;t<32;t++){
                   decrypt[3] =decrypt[3]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 25-32 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g]<<"\nKey: "<<letter[a]<<letter[b]<<letter[c]<<letter[d]<<"\n";
                                 g=26;
                        }
                    }
                    for (int e=0;e<32;e++){             // 32 part loop for the 32 bit enc
                        if (key[e]==text[2][e]){        // if bit "a" in key ==  bit "b" in text 
                           out = out + "0";             // add a "0" to out
                           }                            //
                        if (key[e]!=text[2][e]){        // else 
                           out = out + "1";             // add a "1"
                           }
                    } 
                    for (int t=0;t<8;t++){
                   decrypt[0] =decrypt[0]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 1-8 part loop for the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<"\n -- Packet 3 = "<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=8;t<16;t++){
                   decrypt[1] =decrypt[1]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 9-16 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=16;t<24;t++){
                   decrypt[2] =decrypt[2]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 17-24 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g];
                                 g=26;
                        }
                    }
                    
                    for (int t=24;t<32;t++){
                   decrypt[3] =decrypt[3]+out[t];
                    }
                    for (int g=0;g<26;g++){            // 25-32 part of the 32 bit enc
                        if(decrypt[0] == binary[g]){
                                 cout<<letter[g]<<"\nKey: "<<letter[a]<<letter[b]<<letter[c]<<letter[d]<<"\n";
                                 g=26;
                        }
                    }
                    out="";
                count=count+1;
                if (count%1000==0){ 
                 cout<<".";
                 }
                }                
            }
        }
    }
cout<<"\n\n\t\t\t"<<count<<" Keys tried\n";
cout<<"\t\t\tNo Matches Found\n\t\t\t";                  
system("pause");        
}



yours31f's Avatar
Retired
10 0

Ok, well I updated the code, It is no longer an issue with speed. Now it BF it in about 10-15 seconds. Now, The issue is that it finds no matches for the packets.


ghost's Avatar
0 0

If you're talking about a pure brute force attack against a securely implemented one time pad, it's actually been proven that it is mathematically impossible.

The only things that can lead to a compromise in the security of a OTP is if the pad used is not totally random, or if the same key is used for more than one different message.

You're familiar with python, no? Check out http://www.hellboundhackers.org/articles/691-cryptanalysis-case-study:-the-one-time-pad.html


p4plus2's Avatar
Member
0 0

He is talking about the OTP for one of the encryption challenges which has enough info to make it vulnerable. The goal is to break it, hes not going for OTP as a whole just the challenge.


ghost's Avatar
0 0

Ah. Well then, good luck.