One-Time-Pad Entschlüsselung?

2 Antworten

Vom Fragesteller als hilfreich ausgezeichnet

Hallo Greenready!

Hab mal ein kleines c-Programm gebastelt:

#include <stdio.h>

int main(int argc, char **argv){

 int i, k;   

 int c[14]= { 0x04, 0x10, 0x12, 0x00, 0x01, 0x08, 0x67,  0x0a, 0x19, 0x65, 0x0f, 0x03, 0x0a, 0x00}; 
 int ka[14]= { 0x45, 0x44, 0x46, 0x41, 0x42, 0x43, 0x47, 0x4B, 0x4D, 0x45, 0x4B, 0x42, 0x5D, 0x4E};
 int kb[14]= { 0x45, 0x44, 0x46, 0x41, 0x42, 0x43, 0x47, 0x4B, 0x4D, 0x45, 0x41, 0x4C, 0x45, 0x4E};


   printf("Zuerst Schluessel ka:\n");

  for (i=0; i< 14; i++)
   {
    k = c[i] ^ ka[i];  /* Exclusiv-Oder-> XOR */
    printf("%c", k);  
   }  

   printf("\n\n");
   printf("Dann Schluessel kb:\n");

  for (i=0; i< 14; i++)
   {
    k = c[i] ^ kb[i]; /* Exclusiv-Oder-> XOR */
    printf("%c", k);  
   }  

   printf("\n\n\n");

   return 0;
}

Das kam dabei raus:

Zuerst Schluessel ka:

ATTACK AT DAWN

Dann Schluessel kb:

ATTACK AT NOON


Greenready 
Fragesteller
 10.12.2020, 19:09

Vielen Dank hat mir geholfen!

0

So könnte das Ganze in C++ aussehen:

#include <iostream>
#include <vector>
#include <string> 
#include <sstream>

using namespace std;

string xorFunction(vector<int> c, vector<int> k);
string hex2str (string str);

vector<int> c  = { 0x04, 0x10, 0x12, 0x00, 0x01, 0x08, 0x67, 0x0a, 0x19, 0x65, 0x0f, 0x03, 0x0a, 0x00 }; 
vector<int> ka = { 0x45, 0x44, 0x46, 0x41, 0x42, 0x43, 0x47, 0x4B, 0x4D, 0x45, 0x4B, 0x42, 0x5D, 0x4E };
vector<int> kb = { 0x45, 0x44, 0x46, 0x41, 0x42, 0x43, 0x47, 0x4B, 0x4D, 0x45, 0x41, 0x4C, 0x45, 0x4E };


int main()
{
    string plain1 = xorFunction(c,ka);
    string plain2 = xorFunction(c,kb);

    cout << plain1 + "\n";
    cout << plain2;
}

string xorFunction(vector<int> c, vector<int> k) 
{
    vector<string> p;
    stringstream ss;
    for(int i = 0; i < c.size(); i++) {
        ss << hex << (c[i]^k[i]);
        string res ( ss.str() );
        p.push_back(res);
    }
    return hex2str(p[p.size()-1]);
}

string hex2str (string str)
{
    string tmp;
    const char *c = str.c_str();
    unsigned int x;
    while(*c != 0) {
        sscanf(c, "%2X", &x);
        tmp += x;
        c += 2;
    }
    return tmp;
}
Woher ich das weiß:Berufserfahrung