Skip to content

Instantly share code, notes, and snippets.

@wavescholar
Last active August 29, 2015 13:58
Show Gist options
  • Select an option

  • Save wavescholar/10152479 to your computer and use it in GitHub Desktop.

Select an option

Save wavescholar/10152479 to your computer and use it in GitHub Desktop.
Stream Cipher Code - Experimental
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <cctype>
int to_int(int c) {
if (! isxdigit(c)) return -1; // error: non-hexadecimal digit found
if (isdigit(c)) return c - '0';
if (isupper(c)) c = tolower(c);
return c - 'a' + 10;
}
char hexDigit(unsigned n)
{
if (n < 10) {
return n + '0';
} else {
return (n - 10) + 'A';
}
}
void charToHex(char c, char hex[3])
{
hex[0] = hexDigit(c / 0x10);
hex[1] = hexDigit(c % 0x10);
hex[2] = '\0';
}
using namespace std;
const char* ciphertext1="315c4eeaa8b5f8aaf9174145bf43e1784b8fa00dc71d885a804e5ee9fa40b16349c146fb778cdf2d3aff021dfff5b403b510d0d0455468aeb98622b137dae857553ccd8883a7bc37520e06e515d22c954eba5025b8cc57ee59418ce7dc6bc41556bdb36bbca3e8774301fbcaa3b83b220809560987815f65286764703de0f3d524400a19b159610b11ef3e";
const char* ciphertext2="234c02ecbbfbafa3ed18510abd11fa724fcda2018a1a8342cf064bbde548b12b07df44ba7191d9606ef4081ffde5ad46a5069d9f7f543bedb9c861bf29c7e205132eda9382b0bc2c5c4b45f919cf3a9f1cb74151f6d551f4480c82b2cb24cc5b028aa76eb7b4ab24171ab3cdadb8356f";
const char* ciphertext3="32510ba9a7b2bba9b8005d43a304b5714cc0bb0c8a34884dd91304b8ad40b62b07df44ba6e9d8a2368e51d04e0e7b207b70b9b8261112bacb6c866a232dfe257527dc29398f5f3251a0d47e503c66e935de81230b59b7afb5f41afa8d661cb";
const char* ciphertext4="32510ba9aab2a8a4fd06414fb517b5605cc0aa0dc91a8908c2064ba8ad5ea06a029056f47a8ad3306ef5021eafe1ac01a81197847a5c68a1b78769a37bc8f4575432c198ccb4ef63590256e305cd3a9544ee4160ead45aef520489e7da7d835402bca670bda8eb775200b8dabbba246b130f040d8ec6447e2c767f3d30ed81ea2e4c1404e1315a1010e7229be6636aaa";
const char* ciphertext5="3f561ba9adb4b6ebec54424ba317b564418fac0dd35f8c08d31a1fe9e24fe56808c213f17c81d9607cee021dafe1e001b21ade877a5e68bea88d61b93ac5ee0d562e8e9582f5ef375f0a4ae20ed86e935de81230b59b73fb4302cd95d770c65b40aaa065f2a5e33a5a0bb5dcaba43722130f042f8ec85b7c2070";
const char* ciphertext6="32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd2061bbde24eb76a19d84aba34d8de287be84d07e7e9a30ee714979c7e1123a8bd9822a33ecaf512472e8e8f8db3f9635c1949e640c621854eba0d79eccf52ff111284b4cc61d11902aebc66f2b2e436434eacc0aba938220b084800c2ca4e693522643573b2c4ce35050b0cf774201f0fe52ac9f26d71b6cf61a711cc229f77ace7aa88a2f19983122b11be87a59c355d25f8e4";
const char* ciphertext7="32510bfbacfbb9befd54415da243e1695ecabd58c519cd4bd90f1fa6ea5ba47b01c909ba7696cf606ef40c04afe1ac0aa8148dd066592ded9f8774b529c7ea125d298e8883f5e9305f4b44f915cb2bd05af51373fd9b4af511039fa2d96f83414aaaf261bda2e97b170fb5cce2a53e675c154c0d9681596934777e2275b381ce2e40582afe67650b13e72287ff2270abcf73bb028932836fbdecfecee0a3b894473c1bbeb6b4913a536ce4f9b13f1efff71ea313c8661dd9a4ce";
const char* ciphertext8="315c4eeaa8b5f8bffd11155ea506b56041c6a00c8a08854dd21a4bbde54ce56801d943ba708b8a3574f40c00fff9e00fa1439fd0654327a3bfc860b92f89ee04132ecb9298f5fd2d5e4b45e40ecc3b9d59e9417df7c95bba410e9aa2ca24c5474da2f276baa3ac325918b2daada43d6712150441c2e04f6565517f317da9d3";
const char* ciphertext9="271946f9bbb2aeadec111841a81abc300ecaa01bd8069d5cc91005e9fe4aad6e04d513e96d99de2569bc5e50eeeca709b50a8a987f4264edb6896fb537d0a716132ddc938fb0f836480e06ed0fcd6e9759f40462f9cf57f4564186a2c1778f1543efa270bda5e933421cbe88a4a52222190f471e9bd15f652b653b7071aec59a2705081ffe72651d08f822c9ed6d76e48b63ab15d0208573a7eef027";
const char* ciphertext10="466d06ece998b7a2fb1d464fed2ced7641ddaa3cc31c9941cf110abbf409ed39598005b3399ccfafb61d0315fca0a314be138a9f32503bedac8067f03adbf3575c3b8edc9ba7f537530541ab0f9f3cd04ff50d66f1d559ba520e89a2cb2a83";
const char* targetciphertext="32510ba9babebbbefd001547a810e67149caee11d945cd7fc81a05e9f85aac650e9052ba6a8cd8257bf14d13e6f0a803b54fde9e77472dbff89d71b57bddef121336cb85ccb8f3315f4b52e301d16e9f52f904";
//Caller must release returned memory
unsigned char* hexDecodCipher(int len,const char* ciphertext)
{
unsigned char* hdc= new unsigned char [len/2];
for (int i=0;i<len/2;i++)
{
unsigned char ch1 = ciphertext[2*i];
unsigned char ch2 = ciphertext[2*i +1];
int hMSB = ch1;//ciphertext[2*i];
int hLSB = ch2;//ciphertext[2*i +1];
int iMSB =to_int(hMSB);
int iLSB= to_int(hLSB);
int a = (iMSB << 4) + iLSB;
unsigned char ch = a;
hdc[i]=ch;
//cout<<"Value = "<<a<<"\t Hex Format = "<<ch1<<ch2<<"\tacsii char = "<<ch<<endl;
}
return hdc;
}
class EncryptionHelper
{
public:
unsigned char* hpt;
unsigned char* ct;
int len;
EncryptionHelper(const char* plaintext=NULL)
{
if( plaintext!=NULL)
{
string splaintext(plaintext);
len = splaintext.length();
char ch1 = splaintext[len-1];
hpt = new unsigned char[len];
for(int i =0 ;i<len;i++)
{
char ch1 = splaintext[i];
hpt[i]= ch1;
}
}
}
unsigned char* encrypt(unsigned char* key)
{
unsigned char* chipertext = new unsigned char[len];
for(int i =0; i<len;i++)
{
unsigned char ch = key[i] ^hpt[i];
chipertext[i] = ch;
unsigned char dci = key[i] ^hpt[i] ^ key[i];
cout<<dci;
}
return chipertext;
}
};
class CipherHelper
{
public:
unsigned char* hdc;
int len;
//This is for hex encoded ciphertest
CipherHelper(const char* ciphertext=NULL)
{
if( ciphertext!=NULL)
{
string sciphertext(ciphertext);
len = sciphertext.length()/2;
char ch1 = sciphertext[2*len-1];
hdc =hexDecodCipher(2*len,ciphertext);
}
}
//This is for ascii encoded ciphertest
CipherHelper( unsigned char* ciphertext=NULL,unsigned int length=0)
{
len = length;
hdc = ciphertext;
}
//Don't call unless the default path is used in the ctor
void setup(const char* ciphertext)
{
string sciphertext1(ciphertext1);
len = sciphertext1.length()/2;
char ch1 = sciphertext1[len-1];
hdc =hexDecodCipher(len,ciphertext);
}
~CipherHelper()
{
//delete hdc;
}
unsigned char* decrypt(unsigned char* key)
{
cout<<endl<<"Decrypt"<<endl;
unsigned char* pt = new unsigned char[len];
for(int i =0; i<len;i++)
{
unsigned char ch = key[i] ^hdc[i];
pt[i] = ch;
cout<<ch;
}
return pt;
}
//Caller must release returned memory
unsigned char* operator^(CipherHelper text2)
{
unsigned char* hdc2 = text2.hdc;
int len2 = text2.len;
int minLen = min(len,len2);
unsigned char* xoredCipherTexts = new unsigned char[minLen];
for (int i=0;i<minLen;i++)
{
xoredCipherTexts[i] = hdc2[i] ^ hdc[i];
}
return xoredCipherTexts;
}
char* hexEncodeCipherText()
{
char* xoredCipherTextsHexEncoded = new char[2*len];
char* hex= new char[3];
for (int i=0;i<len;i++)
{
unsigned char cXor = hdc[i];
charToHex( cXor, hex);
xoredCipherTextsHexEncoded[2*i]=hex[0];
xoredCipherTextsHexEncoded[2*i+1]=hex[1];
}
return xoredCipherTextsHexEncoded;
}
};
void addToCipherMatrix(CipherHelper* ch1, CipherHelper* ch2, std::vector<unsigned char* > &cipherMatrix)
{
unsigned char* xoredCipherTexts = ch1->operator^( *ch2);
int len1 = ch1->len;
int len2 = ch2->len;
int minLen = min(len1,len2);
cipherMatrix.push_back(xoredCipherTexts);
}
void addToCipherMap(CipherHelper* ch1, CipherHelper* ch2, map<pair< unsigned char,unsigned char>,unsigned char > &chipherMap)
{
unsigned char* xoredCipherTexts = ch1->operator^( *ch2);
int len1 = ch1->len;
int len2 = ch2->len;
int minLen = min(len1,len2);
for (int i=0;i<minLen;i++)
{
unsigned char c1 = ch1->hdc[i];
unsigned char c2 = ch2->hdc[i];
unsigned char cXor = xoredCipherTexts[i];
pair<unsigned char, unsigned char> p(c1,c2);
chipherMap[p]=cXor;
}
}
const char* shortStoryP1 = "attack at dawn";
const char* shortStoryP2 = "attack at dusk";
void TestCrytptoHelper(void)
{
EncryptionHelper e1(shortStoryP1);
EncryptionHelper e2(shortStoryP2);
unsigned int maxLen = max(e1.len,e2.len);
unsigned char* key = new unsigned char[maxLen];
for(int i=0;i<maxLen;i++)
{
key[i] = rand() % 255;
}
unsigned char* ciphertext1 = e1.encrypt(key);
unsigned char* ciphertext2 = e2.encrypt(key);
map< int, CipherHelper*> ciphertextMap;
CipherHelper c1 = CipherHelper(ciphertext1,e1.len);
ciphertextMap[1] = &c1;
CipherHelper c2 = CipherHelper(ciphertext2,e2.len);
ciphertextMap[2] = &c2;
unsigned char* pt1 = c1.decrypt(key);
unsigned char* pt2 = c2.decrypt(key);
unsigned char* xortext =c1^(c2);
map<pair< unsigned char,unsigned char>,unsigned char > asciiXORMap;
for (int i=0;i<255;i++)
{
for(int j=0;j<256; j++)
{
unsigned char chi =i;
unsigned char chj =j;
pair< unsigned char,unsigned char> p(chi,chj);
unsigned char chiXORchj = chi^chj;
asciiXORMap[p] = chiXORchj;
//cout<<" ("<<i<<" ^ "<<j<<")=\t"<<chiXORchj;
}cout<<endl;
}cout<<endl<<endl;
for(unsigned int j=0;j<maxLen;j++)
{
unsigned char ch = xortext[j];
for(map<pair< unsigned char,unsigned char>,unsigned char >::iterator ii=asciiXORMap.begin(); ii!=asciiXORMap.end(); ++ii)
{
pair< unsigned char,unsigned char> p =(*ii).first;
unsigned char xor =(*ii).second ;
if(ch ==xor)
{
//cout<<" ("<<p.first<<" ^ "<<p.second<<")=\t"<<xor<<endl;
int c1 =p.first;
int c2 =p.second;
int c3 =xor;
cout<<c1<<" "<<c2<<" "<<c3<<endl;
}
}
}
cout<<endl<<endl;
cout<<endl<<endl;
map<pair< unsigned char,unsigned char>,unsigned char > cipherXORMap;
for(int i=1;i<=2;i++)
{
for(int j=1;j<=2;j++)
{
if (j>=i)
continue;
cerr<<i<<" "<<j<<endl;
CipherHelper* ch1 = ciphertextMap[i];
CipherHelper* ch2 = ciphertextMap[j];
addToCipherMap(ch1,ch2,cipherXORMap);
}
}
int count =0;
for(map<pair< unsigned char,unsigned char>,unsigned char >::iterator ii=cipherXORMap.begin(); ii!=cipherXORMap.end(); ++ii)
{
cout<<count++<<"\t";
pair< unsigned char,unsigned char> p =(*ii).first;
unsigned char xor =(*ii).second ;
cout<<" ("<<p.first<<" ^ "<<p.second<<")=\t"<<xor<<endl;
}cout<<endl<<endl;
char* xorCipherTexts = new char[166];
for (int i=0;i<166;i++)
{
char xCH = ciphertext1[i] ^ ciphertext2[i] ^ ciphertext3[i]^ ciphertext4[i]^ ciphertext5[i]^ ciphertext6[i]^ ciphertext7[i]^ ciphertext8[i]^ ciphertext9[i]^ ciphertext10[i];
xorCipherTexts[i] = xCH ^ targetciphertext[i];
}
string sxorCipherTexts(xorCipherTexts);
cout<<sxorCipherTexts.c_str()<<endl;
for (int i=0;i<166;i++)
{
unsigned char mask = 1; // Bit mask
unsigned char bits[8];
unsigned char ch =xorCipherTexts[i];
for (int j = 0; j < 8; j++)
{
// Mask each bit in the byte and store it
bits[j] =(ch & (mask << j)) != 0;
}
// For debug purposes, lets print the received data
for (int j = 7; j >= 0; j--)
{
printf("%d",bits[j]);
}
cout<<" "<<i<<" "<<ch<<" ";
printf("%2x",ch);
cout<<endl;
}
}
void StreamCipherAttack_Play(void)
{
map< int, CipherHelper*> ciphertextMap;
CipherHelper c1 = CipherHelper(ciphertext1);
ciphertextMap[1] = &c1;
cout<<c1.hdc<<endl<<endl;
cout<<ciphertext1<<endl<<endl;
char* hexDeReEncodedAsciiCipher1 =c1.hexEncodeCipherText();
for(int i=0;i<c1.len*2;i++)
{
char c = hexDeReEncodedAsciiCipher1[i];
cout<<c;
}
string shexDeReEncodedAsciiCipher1(hexDeReEncodedAsciiCipher1);
cout<<shexDeReEncodedAsciiCipher1<<endl;
CipherHelper c2 = CipherHelper(ciphertext2);
ciphertextMap[2] = &c2;
CipherHelper c3 = CipherHelper(ciphertext3);
ciphertextMap[3] = &c3;
CipherHelper c4 = CipherHelper(ciphertext4);
ciphertextMap[4] = &c4;
CipherHelper c5 = CipherHelper(ciphertext5);
ciphertextMap[5] = &c5;
CipherHelper c6 = CipherHelper(ciphertext6);
ciphertextMap[6] = &c6;
CipherHelper c7 = CipherHelper(ciphertext7);
ciphertextMap[7] = &c7;
CipherHelper c8 = CipherHelper(ciphertext8);
ciphertextMap[8] = &c8;
CipherHelper c9 = CipherHelper(ciphertext9);
ciphertextMap[9] = &c9;
CipherHelper c10 = CipherHelper(ciphertext10);
ciphertextMap[10] = &c10;
cout<<endl<<endl;
map<pair< unsigned char,unsigned char>,unsigned char > cipherXORMap;
std::vector<unsigned char* > cipherMatrix;
for(int i=1;i<=10;i++)
{
for(int j=1;j<=10;j++)
{
if (j>=i)
continue;
cerr<<i<<" "<<j<<endl;
CipherHelper* ch1 = ciphertextMap[i];
CipherHelper* ch2 = ciphertextMap[j];
addToCipherMap(ch1,ch2,cipherXORMap);
addToCipherMatrix( ch1, ch2,cipherMatrix);
}
}
for(int i=0;i<cipherMatrix.size();i++)
{
unsigned char* cm = cipherMatrix[i];
cout<<cm<<endl;
}
int count =0;
for(map<pair< unsigned char,unsigned char>,unsigned char >::iterator ii=cipherXORMap.begin(); ii!=cipherXORMap.end(); ++ii)
{
cout<<count++<<"\t";
pair< unsigned char,unsigned char> p =(*ii).first;
unsigned char xor =(*ii).second ;
cout<<" ("<<p.first<<" ^ "<<p.second<<")=\t"<<xor<<endl;
}cout<<endl<<endl;
char* xorCipherTexts = new char[166];
for (int i=0;i<166;i++)
{
char xCH = ciphertext1[i] ^ ciphertext2[i] ^ ciphertext3[i]^ ciphertext4[i]^ ciphertext5[i]^ ciphertext6[i]^ ciphertext7[i]^ ciphertext8[i]^ ciphertext9[i]^ ciphertext10[i];
xorCipherTexts[i] = xCH ^ targetciphertext[i];
}
string sxorCipherTexts(xorCipherTexts);
cout<<sxorCipherTexts.c_str()<<endl;
for (int i=0;i<166;i++)
{
unsigned char mask = 1; // Bit mask
unsigned char bits[8];
unsigned char ch =xorCipherTexts[i];
for (int j = 0; j < 8; j++)
{
// Mask each bit in the byte and store it
bits[j] =(ch & (mask << j)) != 0;
}
// For debug purposes, lets print the received data
for (int j = 7; j >= 0; j--)
{
printf("%d",bits[j]);
}
cout<<" "<<i<<" "<<ch<<" ";
printf("%2x",ch);
cout<<endl;
}
}
/*
Suppose you are told that the one time pad encryption of the message "attack at dawn" is
09e1c5f70a65ac519458e7e53f36 (the plaintext letters are encoded as 8-bit ASCII and the given ciphertext is written in hex).
What would be the one time pad encryption of the message "attack at dusk" under the same OTP key?
*/
void OneTimePadQestion(void)
{
//Recall the chipher text is hex encoded. There are 2 chars form the cipher text for each ascii element in the message text
const char* cipherText="6c73d5240a948c86981bc294814d";//"09e1c5f70a65ac519458e7e53f36";
const char* messageText = "attack at dawn";
const char* messageText2 ="attack at dusk";
const char* messageTextUC = "attack at dawn";
string smessageTest(messageTextUC);
int len = smessageTest.length();
char ch = smessageTest[len-1];
unsigned char* key = new unsigned char[len];
unsigned char* asciiCipher2 = new unsigned char [len];
unsigned char* hexDecodedCipher1 = new unsigned char [len];
unsigned char * hexEncodedCipherMessage1 = new unsigned char [len];
for (int i=0;i<len;i++)
{
unsigned char ch1 = cipherText[2*i];
unsigned char ch2 = cipherText[2*i +1];
int hMSB = cipherText[2*i];
int hLSB = cipherText[2*i +1];
int iMSB =to_int(hMSB);
int iLSB= to_int(hLSB);
int a = (iMSB << 4) + iLSB;
unsigned char ch = a;
hexDecodedCipher1[i]=ch;
cout<<"Value = "<<a<<"\t Hex Format = "<<ch1<<ch2<<"\tacsii char = "<<ch<<endl;
}
cout<<endl;
for (int i=0;i<len;i++)
{
key[i] = hexDecodedCipher1[i] ^ messageText[i];
char m2 = messageText2[i];
asciiCipher2[i] = key[i] ^ messageText2[i];
hexEncodedCipherMessage1[i] = key[i] ^ messageText[i];
}
cout<<endl<<"Hex Encoded Ciphertext 1: "<<endl;
for (int i=0;i<len;i++)
{
unsigned char ch= hexEncodedCipherMessage1[i];
printf("%2x ",ch);
}
cout<<endl;
cout<<endl<<"Ciphertext Hex De-Re-Encoded Ascii : "<<endl;
for (int i=0;i<len;i++)
{
unsigned char ch= asciiCipher2[i];
printf("%2x ",ch);
}
cout<<endl;
cout<<endl<<"Key in Hex Encoded Ascii : "<<endl;
for (int i=0;i<len;i++)
{
unsigned char ch= key[i];
printf("%2x ",ch);
}
cout<<endl;
unsigned char* decodedasciiCipher1=new unsigned char[len];
cout<<endl<<"Decoded Cipher 2: "<<endl;
for (int i=0;i<len;i++)
{
decodedasciiCipher1[i] = asciiCipher2[i] ^ key[i];
unsigned char ch= decodedasciiCipher1[i];
cout<<ch;
}
cout<<endl;
cout<<endl<<"Hex Encoded Chiphertext 2 : "<<endl;
for (int i=0;i<len;i++)
{
unsigned char ch= asciiCipher2[i];
printf("%2x ",ch);
}
//cout<<decodedasciiCipher1<<endl;
delete key;
delete asciiCipher2;
delete decodedasciiCipher1;
}
int main(int argc, char* argv[])
{
TestCrytptoHelper();
StreamCipherAttack_Play();
OneTimePadQestion();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment