Commit cdecc42f authored by ulvpn's avatar ulvpn

project demo

parent d43dbbee
......@@ -120,15 +120,12 @@ static void CountChars( int start, int offset, int h[NUMCHARS])
int i;
char c;
for (i=0; i<NUMCHARS; i++) h[i] = 0;
for (i=0; i<NUMCHARS; i++) h[i] = 0; // initilize the counter table
/***************** Task 3 *****************/
// printf("%s\n",TextArray);
for (size_t i = start; i < TextLength; i=i+offset)
{
c = TextArray[i];
switch (c)
c = TextArray[i];
switch (c) // character counter
{
case 'A':h[0]++;break;
case 'B':h[1]++;break;
......@@ -159,11 +156,7 @@ static void CountChars( int start, int offset, int h[NUMCHARS])
default:break;
}
}
return ;
}
/*
......@@ -187,9 +180,14 @@ static double AutoCorrelation (int d)
for (size_t i = 0; i <= (TextLength-d); i++)
{
if(TextArray[i] != TextArray[i+d]){ // when i = i+d, if d is period, then
autoCor = autoCor + 1;
if(TextArray[i] != TextArray[i+d]){
autoCor = autoCor + 1; /* We assume that d is the length of key, and try to
* calculate autoCorelation from d=1 to d = MAXPERIOD(200)
* If d was the length of key or multiple of that length and
* exactly TextArray[i] == TextArray[i+d], which means m[i] == m[i+d],
* we could conclude that situations occured with much lower probablity.
* With d = n*len(key) we obtain a very lower amount(minimal) of autoCor.
*/
}
}
return autoCor;
......@@ -211,9 +209,9 @@ static void CalcPeriod (void)
float min = 1;
float max = 0;
/**** Calculate period ****/
for (size_t i = 1; i < MAXPERIOD; i++)
{
if (AutoCor[i] < min)
{
min = AutoCor[i];
......@@ -221,8 +219,7 @@ static void CalcPeriod (void)
if (AutoCor[i] > max)
{
max = AutoCor[i];
}
}
}
for (size_t i = 1; i < MAXPERIOD; i++)
......@@ -232,11 +229,8 @@ static void CalcPeriod (void)
Period = i;
printf("Period: %d\n", Period);
break;
}
}
}
return ;
}
......@@ -251,19 +245,12 @@ int main(int argc, char **argv)
start = 0;
offset = 1;
CountChars(start,offset,h);
{
int i;
for (i = 0; i <= MAXPERIOD; i++) {
AutoCor [i] = (double) AutoCorrelation (i) / (TextLength - i);
}
CountChars(start,offset,h);
/***** standardized AutoCorrelation *****/
for (int i = 0; i <= MAXPERIOD; i++) {
AutoCor [i] = (double) AutoCorrelation (i) / (TextLength - i);
}
/* Now prepare gnuplot */
{
FILE *f;
......@@ -294,16 +281,10 @@ int main(int argc, char **argv)
system ("gnuplot " GNUPLOT_CMD_FILENAME);
CalcPeriod ();
/***** attack for every subkey *****/
char key[Period];
for (start = 0; start < Period; start++)
{
double min=MaxFileLen;
......@@ -311,24 +292,17 @@ int main(int argc, char **argv)
double kreuzCor ;
int sum = 0;
double H[NUMCHARS];
CountChars(start,Period,h);
CountChars(start,Period,h); // count c_start, c_start+Peroid, c_start+2*Peroid, ......
for (size_t i = 0; i < NUMCHARS; i++)
{
sum = sum + h[i];
// printf("%2d-th absolute frequency h[%2d] = %d\n ", start,i, h[i]);
sum = sum + h[i]; // count the amount of characters in the start-th subciphertext
}
// printf("%2d-th sub-length: %d\n", start, sum);
for (size_t i = 0; i < NUMCHARS; i++)
{
H[i] = (double) h[i]/sum;
// printf("%d-th sub frequency H[%d] = %f\n", start,i, H[i]);
}
H[i] = (double) h[i]/sum; // frequency calculation
}
/***** kreuzCor(k) = sum (P_i - H_(i+k)%N)^2 for i = 0 - 25 ****/
for (size_t k = 0; k < NUMCHARS; k++)
{
kreuzCor = 0.0;
......@@ -336,22 +310,14 @@ int main(int argc, char **argv)
{
kreuzCor = kreuzCor + (PropTable[i] - H[(i+k)%NUMCHARS])*(PropTable[i] - H[(i+k)%NUMCHARS]);
}
// printf("%f\n", kreuzCor);
if (kreuzCor < min)
{
min = kreuzCor;
subkey = k;
}
min = kreuzCor; /* minimal kreuzCor means the probablity distribution
* in the subciphertext is tha same in real English.
*/
subkey = k;
}
}
// printf("%2d-th crosscorelation: %f with subkey = %d\n", start, min,subkey);
// printf("%d: %d\n",start,subkey);
switch (subkey)
{
case 1:key[start] = 'A';break;
......
......@@ -28,7 +28,7 @@ int N = 26; // length of alphabet
static int Encipher(int k, int c)
{
c = 'A' + ((c-'A' + k-'A'+1) % N);
c = 'A' + ((c-'A' + k-'A'+1) % N); // ci = mi + ki mod N
return c;
}
......@@ -40,7 +40,7 @@ static int Encipher(int k, int c)
static int Decipher(int k, int c)
{
c = 'A' + ((c-'A' - (k-'A'+1) + N ) % N);
c = 'A' + ((c-'A' - (k-'A'+1) + N ) % N); // mi = ci - ki mod N
return c;
}
......@@ -57,7 +57,7 @@ int main(int argc, char **argv)
String infilename,outfilename,help,zeile;
int decipher;
/***** local helpful variables *******/
/***** local helpful variables ******/
FILE *infile,*outfile;
/***** print Veigenere Table *****/
......@@ -97,7 +97,7 @@ int main(int argc, char **argv)
string_to_upper(help);
}
while (strlen(help)!=1 && help[0]!='E' && help[0]!='D');
decipher = help[0]=='D'; // decipher == 1: decryption
decipher = help[0]=='D'; // decipher == 1: decrypt the plaintext
}
else {
if (!strncmp(argv[4],"encipher",strlen(argv[4]))) decipher = 0;
......@@ -140,9 +140,7 @@ int main(int argc, char **argv)
strip_crlf(zeile);
string_to_upper(zeile);
/*>>>> <<<<*
*>>>> Vigenere-encrypt line by line <<<<*
*>>>> <<<<*/
/******** Vigenere-encrypt line by line *******/
for (size_t i = 0; i < strlen(zeile); i++)
{
......@@ -151,11 +149,11 @@ int main(int argc, char **argv)
if (decipher)
{
zeile[i] = Decipher(Key[l%key_len],zeile[i]);
zeile[i] = Decipher(Key[l%key_len],zeile[i]);
}
else
{
zeile[i] = Encipher(Key[l%key_len],zeile[i]);
zeile[i] = Encipher(Key[l%key_len],zeile[i]);
}
l++;
......
......@@ -17,7 +17,7 @@
Connection con;
//the padding oracle will return 1 if the padding is valid and 0 otherwise
/* he padding oracle will return 1 if the padding is valid and 0 otherwise */
int padding_oracle(unsigned char *buf, char blocks){
if(blocks > 2 || blocks < 1){
......@@ -36,10 +36,7 @@ int padding_oracle(unsigned char *buf, char blocks){
printf("Invalid message type from daemon\n");
exit(1);
}
return m.oracle_rep.rep;
}
char padding_attack(unsigned char *challenge, unsigned char *plaintext){
......@@ -50,10 +47,9 @@ char padding_attack(unsigned char *challenge, unsigned char *plaintext){
char plaintxt_len = 0;
unsigned char padding_len = 1;
unsigned char deCipher[2*BLOCK_LENGTH];
unsigned char copy[3*BLOCK_LENGTH];
for (size_t i = 0; i < 3*BLOCK_LENGTH; i++)
{
copy[i] = challenge[i];
......@@ -78,19 +74,13 @@ char padding_attack(unsigned char *challenge, unsigned char *plaintext){
printf("padding length: %d\n", padding_len);
for (size_t i = 1; i <= padding_len; i++) // initialize the first padding_len deCipher
{
deCipher[2*BLOCK_LENGTH -i] = copy[2*BLOCK_LENGTH-i] ^ padding_len; // dc2 = m ^ c1'
challenge[2*BLOCK_LENGTH -i] = deCipher[2*BLOCK_LENGTH-i] ^ (padding_len+1); // change c1'
// printf("%d\n", deCipher[3*BLOCK_LENGTH-i]);
// printf("%d\n", deCipher[3*BLOCK_LENGTH-i] ^ copy[2*BLOCK_LENGTH-i]);
challenge[2*BLOCK_LENGTH -i] = deCipher[2*BLOCK_LENGTH-i] ^ (padding_len+1); // change c1'
}
padding_len++;
unsigned char xor;
......@@ -101,48 +91,38 @@ char padding_attack(unsigned char *challenge, unsigned char *plaintext){
xor = 0;
while (xor < 256)
{
challenge[2*BLOCK_LENGTH - padding_len] = xor ;
oracle_rep = padding_oracle(challenge,blocks);
printf("blocks=%d, padding_len=%2d, xor=%3d, response from oracle: %d\n", blocks,padding_len,xor,oracle_rep);
if (oracle_rep == 1)
{
deCipher[2*BLOCK_LENGTH - padding_len] = xor ^ padding_len;
padding_len++;
attampts = attampts + xor;
if(padding_len > BLOCK_LENGTH) break;
for (size_t i = 1; i < padding_len; i++)
{
challenge[2*BLOCK_LENGTH -i] = deCipher[2*BLOCK_LENGTH-i] ^ padding_len;
}
break;
}
xor++;
xor++;
}
}
blocks = 1;
padding_len = 1;
for (size_t i = BLOCK_LENGTH; i < 2*BLOCK_LENGTH; i++) //retore ciphertext from copy
/* retore ciphertext from copy */
for (size_t i = BLOCK_LENGTH; i < 2*BLOCK_LENGTH; i++)
{
challenge[i] = copy[i];
}
while (padding_len <= BLOCK_LENGTH)
{
{
xor = 0;
while (xor < 256)
{
challenge[BLOCK_LENGTH-padding_len] = xor;
oracle_rep = padding_oracle(challenge,blocks);
printf("blocks=%d, padding_len=%2d, xor=%3d, response from oracle: %d\n", blocks, padding_len,xor,oracle_rep);
......@@ -155,27 +135,22 @@ char padding_attack(unsigned char *challenge, unsigned char *plaintext){
for (size_t i = 1; i < padding_len; i++)
{
challenge[BLOCK_LENGTH-i] = deCipher[BLOCK_LENGTH-i] ^ padding_len;
}
}
break;
}
}
xor++;
}
}
}
for (size_t i = 0; i < 3*BLOCK_LENGTH; i++) // restore ciphertext from copy
{
challenge[i] = copy[i];
}
xor_block(&challenge[0], &deCipher[0], &plaintext[0], 2*BLOCK_LENGTH); // mi = ci-1 ^ d(ci)
printf("Decrypted with %d attempts:\n %s<- Here is the end \n",attampts, plaintext);
return plaintxt_len;
}
void solution(unsigned char *buf){
message req;
memset(&req, 0, sizeof(req));
......@@ -227,7 +202,6 @@ int main (int argc, char *argv[]){
// not "the first BLOCK_LENGTH * i bytes of data", but "second BLOCK or third BLOCK"
char plaintext_len = 0;
plaintext_len = padding_attack(challenge, plaintext);
printf("Press enter to continue: \n");
......@@ -251,7 +225,6 @@ int main (int argc, char *argv[]){
}
}
unsigned char chiff[3*BLOCK_LENGTH];
for (size_t i = 0; i < BLOCK_LENGTH; i++)
......@@ -288,9 +261,7 @@ int main (int argc, char *argv[]){
}
xor++;
}
}
for (size_t i = 0; i < BLOCK_LENGTH; i++)
......@@ -301,6 +272,5 @@ int main (int argc, char *argv[]){
solution(chiff);
DisConnect (con);
exit(0);
return 0;
}
File added
......@@ -3,7 +3,7 @@
** Internship "Cryptography" *
** *
** Experiment 9: Meet-in-the-Middle *
** *
** *
** *
**************************************************************
**
......@@ -143,7 +143,7 @@ void stage_1 (int iteration)
bzero (hashtable, HASHTABLE_ELEM*sizeof (int)); /* Erase all bits */
/* Task */
/* Encrypt plain text for midblock and set bits */
k_left = 0;
while (k_left < KEY_MAX)
{
......@@ -151,9 +151,7 @@ void stage_1 (int iteration)
ssc_encrypt(k_left,&plaintext,&enc_plain);
if(HASH_ELEM(enc_plain.left,enc_plain.right) - iteration*HASHTABLE_ELEM < HASHTABLE_ELEM) // only store the i-th range of blocks
{
SET_BIT(hashtable,HASH_ELEM(enc_plain.left,enc_plain.right),HASH_BIT(enc_plain.left,enc_plain.right));
}
k_left++;
}
......@@ -165,9 +163,10 @@ void stage_1 (int iteration)
*/
void stage_2 (int iteration)
{
/* Task */
int c=0;
k_right = 0;
/* Decrypt cipher text for midblock and store collisions*/
while (k_right < KEY_MAX)
{
c++;
......@@ -219,10 +218,11 @@ static struct collision tmp_collision; /* Helpful variable for the following
*/
void stage_4 (void)
{
/* Task */
struct collision *found;
k_left = 0;
block myCipher;
/* Encrypt plain text again and search for collision in collision table */
while (k_left < KEY_MAX)
{
......
File added
File added
......@@ -2,8 +2,8 @@
** European System Security Institute *
** Internship "Cryptography" *
** *
** Experiment 9: Meet-in-the-Middle *
** *
** Experiment 3: Meet-in-the-Middle *
** *
** *
**************************************************************
**
......
......@@ -38,7 +38,7 @@ int power(int base, int exp){
void doexp(mpz_t x, mpz_t y, mpz_t z, mpz_t p)
{
/*>>>> <<<<*
*>>>> TASK: Implementation of the modulo exponentation <<<<*
*>>>> Implementation of the modulo exponentation <<<<*
*>>>> <<<<*/
......
......@@ -2,7 +2,7 @@
** European System Security Institute *
** Internship "Cryptanalysis" *
** *
** Experiment 7: El-Gamal Signature *
** Experiment 6: El-Gamal Signature *
** *
** *
**************************************************************
......@@ -30,9 +30,7 @@ static mpz_t w; // generator
*/
static int Verify_Sign(mpz_t mdc, mpz_t r, mpz_t s, mpz_t y)
{
/*>>>> <<<<*
*>>>> TASK: Verify an El Gamal signature <<<<*
*>>>> <<<<*/
/**** Verify an El Gamal signature ****/
mpz_t left_side0, left_side1, left_side, right_side;
......@@ -77,10 +75,8 @@ static int Verify_Sign(mpz_t mdc, mpz_t r, mpz_t s, mpz_t y)
static void Generate_Sign(mpz_t mdc, mpz_t r, mpz_t s, mpz_t x)
{
/*>>>> <<<<*
*>>>> TASK: Generate an El Gamal signature <<<<*
*>>>> <<<<*/
/**** Generate an El Gamal signature ****/
mpz_t k,k_inverse,gcd,p_1;
mpz_init(k);
mpz_init(k_inverse);
......@@ -182,11 +178,7 @@ int main(int argc, char **argv)
printf("Dämon-Signatur ist FEHLERHAFT!\n");
}
/*>>>> <<<<*
*>>>> TASK: Forge the demon signaturre <<<<*
*>>>> <<<<*/
/**** Forge the demon signaturre ****/
Message msg0;
mpz_t mdc0,sign_r0,sign_s0,u, mdc_inverse, p_1;
mpz_init(mdc0);
......
/*************************************************************
** Europäisches Institut für Systemsicherheit *
** Proktikum "Kryptographie und Datensicherheitstechnik" *
** *
** Versuch 7: El-Gamal-Signatur *
** *
**************************************************************
**
** sign.h: Headerfile für den Signatur-Vrsuch
**/
** European System Security Institute *
** Internship "Cryptography" *
** *
** Experiment 6: El-Gamal Signature *
** *
** *
**************************************************************
**
** sign.h: Headerfile for Signature Experiment
**/
#include <stdio.h>
#include <stdlib.h>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment