| New file |
| | |
| | | section .data |
| | | ; by Matthias Altmann |
| | | ; Define standard values |
| | | ; general |
| | | sys_exit equ 60 |
| | | sys_read equ 0 |
| | | sys_write equ 1 |
| | | stdin equ 0 |
| | | stdout equ 1 |
| | | stderr equ 2 |
| | | alphasize equ 26 ; size of alphabet used for iteration |
| | | bAsciiZero equ 0x30 |
| | | wAsciiZero equ 0x3030 |
| | | pCounter db "00" |
| | | pAsciiSpace db " " |
| | | |
| | | ; program specific |
| | | pPrompt db "Enter string to decode (caesar):" |
| | | promptsize equ $-pPrompt |
| | | |
| | | section .bss |
| | | ; Buffer to read input |
| | | lpCipher resb 255 |
| | | ciphersize equ $-lpCipher |
| | | |
| | | |
| | | section .text |
| | | global _start |
| | | |
| | | _start: |
| | | nop ; for good old gdb |
| | | |
| | | ; Output prompt to enter text |
| | | mov rsi, pPrompt |
| | | mov rdx, promptsize |
| | | call DisplayText |
| | | ; Read text |
| | | mov rsi, lpCipher |
| | | mov rdx, ciphersize |
| | | call ReadText |
| | | |
| | | ; Setup loop |
| | | xor rbx, rbx ; counter (all) |
| | | xor r9,r9 ; counter (10) |
| | | inc r9 |
| | | mov rsi, pCounter |
| | | mov word [rsi],wAsciiZero |
| | | |
| | | ; Start loop over alphabet |
| | | Alpha_Loop: |
| | | mov rsi, pCounter |
| | | ;output counter |
| | | mov rdx,2 |
| | | call DisplayText |
| | | ; update buffer counter |
| | | call IncCounterBuf |
| | | |
| | | ; output an empty space |
| | | mov rsi, pAsciiSpace |
| | | mov rdx,1 |
| | | call DisplayText |
| | | |
| | | ; output rotated cipher |
| | | mov rsi, lpCipher |
| | | mov rdx, ciphersize |
| | | call DisplayText |
| | | ; update buffer rotated cipher |
| | | call IncCipherBuf |
| | | |
| | | ; increase counter |
| | | inc rbx |
| | | inc r9 |
| | | cmp rbx, alphasize |
| | | jne Alpha_Loop |
| | | |
| | | ; Exit prog |
| | | call Exit |
| | | |
| | | ReadText: |
| | | mov rax, sys_read |
| | | mov rdi, stdin |
| | | syscall |
| | | ret |
| | | |
| | | DisplayText: |
| | | mov rax, sys_write |
| | | mov rdi, stdout |
| | | syscall |
| | | ret |
| | | |
| | | IncCounterBuf: |
| | | inc byte [rsi+1] |
| | | cmp r9,10 |
| | | jb .noDecJump |
| | | ; Decimal jump |
| | | xor r9,r9 |
| | | inc byte [rsi] |
| | | mov byte [rsi+1],bAsciiZero |
| | | .noDecJump: |
| | | ret |
| | | |
| | | IncCipherBuf: |
| | | xor r11,r11 |
| | | mov rsi, lpCipher |
| | | mov r10, rsi |
| | | lodsb |
| | | .loopThroughBuf: |
| | | cmp al,0x0A ; line feed '\n' ? |
| | | je .endOfCipher |
| | | cmp r11,ciphersize |
| | | jae .endOfCipher |
| | | ; check if empty space |
| | | cmp byte [r10],0x20 |
| | | je .toNextChar |
| | | ; check if lower case |
| | | cmp byte [r10],0x7A ; is z? |
| | | jne .checkUpCase |
| | | mov byte [r10],0x61 ; set a |
| | | jmp .toNextChar |
| | | .checkUpCase: |
| | | ; check if upper case |
| | | cmp byte [r10],0x5A ; is Z? |
| | | jne .doNormalInc |
| | | mov byte [r10],0x41; set A |
| | | jmp .toNextChar |
| | | .doNormalInc: |
| | | inc byte [r10] |
| | | .toNextChar: |
| | | lodsb |
| | | inc r10 |
| | | inc r11 |
| | | jmp .loopThroughBuf |
| | | .endOfCipher: |
| | | ret |
| | | |
| | | Exit: |
| | | mov rax, sys_exit |
| | | mov rdi, 0 |
| | | syscall |
| | | ret |
| | | |
| New file |
| | |
| | | #include <stdio.h> |
| | | #include <string.h> |
| | | #include <ctype.h> |
| | | #include <stdlib.h> |
| | | #include "idxOfCoIncidence.h" |
| | | #include "chi-squared.h" |
| | | #include "letter-frequency.h" |
| | | |
| | | // gcc ca_vigenere.c chi-squared.c idxOfCoIncidence.c -o ca_vigenere |
| | | // Try to crack vigenere cipher |
| | | // by Matthias Altmann |
| | | int main() |
| | | { |
| | | char cipher [65535]; |
| | | printf("Enter cipher text:"); |
| | | fgets(cipher,sizeof(cipher),stdin); |
| | | // cut line feed |
| | | strtok(cipher,"\n"); |
| | | |
| | | // Calculate index of coincidence. |
| | | double avgIdxOfCoInc; |
| | | for (int i = 1;i<strlen(cipher);i++) |
| | | { |
| | | avgIdxOfCoInc = calcAvgIdxOfCoInc(i,cipher); |
| | | if (avgIdxOfCoInc > 0.0000000001) |
| | | printf("\nKey length:%d %2.16f",i,avgIdxOfCoInc); |
| | | } |
| | | printf("\n\n"); |
| | | |
| | | // Calculat Chi Square for specific key length. |
| | | char keysizeToScanStr [20]; |
| | | int keysizeToScan = 0; |
| | | printf("Choose key length to calculate chi-square test:"); |
| | | fgets(keysizeToScanStr,sizeof(keysizeToScanStr),stdin); |
| | | strtok(keysizeToScanStr,"\n"); |
| | | for (int l=0;l<strlen(keysizeToScanStr);l++) |
| | | { |
| | | if (!isdigit(keysizeToScanStr[l])) |
| | | { |
| | | return 0; |
| | | } |
| | | } |
| | | // convert string key size to int |
| | | keysizeToScan = atoi(keysizeToScanStr); |
| | | // iterate through cipher string with entered key length. |
| | | for (int keyoffset=0;keyoffset < keysizeToScan;keyoffset++) |
| | | { |
| | | int stroffset=keyoffset; |
| | | char strToCheck [65535]; |
| | | int p = 0; |
| | | while (stroffset < strlen(cipher)) |
| | | { |
| | | strToCheck[p]=cipher[stroffset]; |
| | | stroffset = stroffset + keysizeToScan; |
| | | p++; |
| | | } |
| | | strToCheck[p]='\0'; |
| | | printf("\n\n%s ", strToCheck); |
| | | for (int letter=0;letter<=25;letter++) |
| | | { |
| | | printf("\n %d %c %s %f",letter,65+letter,strToCheck,calcChiSquare(strToCheck,letter_freq_english)); |
| | | // printf("\n %d %f",letter,strToCheck,calcChiSquare(strToCheck,letter_freq_english)); |
| | | for (int i=0;i<strlen(strToCheck);i++) |
| | | { |
| | | strToCheck[i]=(toupper(strToCheck[i])=='A')?'Z':(strToCheck[i]-1); |
| | | } |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| New file |
| | |
| | | #include <stdio.h> |
| | | #include <ctype.h> |
| | | #include <string.h> |
| | | |
| | | // by Matthias Altmann |
| | | // Calculate Chi Square |
| | | double calcChiSquare(char * strToCheck, const double * referenceLang) |
| | | { |
| | | int j = 0; |
| | | while (strToCheck[j] != '\0') |
| | | { |
| | | if (!isalpha(strToCheck[j])) |
| | | { |
| | | // cut the character if non-alpha |
| | | memmove(&strToCheck[j], &strToCheck[j + 1], strlen(strToCheck) - j); |
| | | } |
| | | else |
| | | { |
| | | j++; |
| | | } |
| | | } |
| | | int letter_count; |
| | | double sum = 0; |
| | | for (char letter='A'; letter <= 'Z'; letter++) |
| | | { |
| | | letter_count = 0; |
| | | for (int i=0;i<=strlen(strToCheck);i++) |
| | | { |
| | | letter_count = letter_count + (toupper(strToCheck[i]) == letter); |
| | | } |
| | | double expected_letter_count = referenceLang[letter-65]*strlen(strToCheck)/100; |
| | | // printf("%c %d %f\n",letter, letter_count, expected_letter_count); |
| | | double toSquare = letter_count - expected_letter_count; |
| | | sum = sum + ((toSquare*toSquare)/expected_letter_count); |
| | | } |
| | | return sum; |
| | | } |
| | | |
| | | /*! Test for chi square test. |
| | | * |
| | | * Has to be 18.528310082299488 against english distribution. |
| | | * |
| | | */ |
| | | /* |
| | | int test() |
| | | { |
| | | // char strToCheck[] = "aBbbbc"; |
| | | char strToCheck[] = "Defend the east wall of the castle"; |
| | | // char strToCheck[] = "aoljhlzhyjpwolypzvulvmaollhysplzaruvduhukzptwslzajpwolyzpapzhafwlvmzbizapabapvujpwolypudopjolhjoslaalypuaolwshpualeapzzopmalkhjlyahpuubtilyvmwshjlzkvduaolhswohila"; |
| | | printf("%s\n%f\n",strToCheck,calcChiSquare(strToCheck,letter_freq_english)); |
| | | return 0; |
| | | }*/ |
| New file |
| | |
| | | #pragma once |
| | | |
| | | /*! Calculate Chi Square */ |
| | | double calcChiSquare(char * strToCheck, const double * referenceLang); |
| | | |
| New file |
| | |
| | | #include <stdio.h> |
| | | #include <string.h> |
| | | #include <stdlib.h> |
| | | #include <ctype.h> |
| | | |
| | | void decrypt_vigenere(char * cipherToPlain, char * key) |
| | | { |
| | | int k = 0; |
| | | for (int i = 0; i < strlen(cipherToPlain);i++) |
| | | { |
| | | k = k % (strlen(key)-1); |
| | | //printf("\n %c",toupper(cipherToPlain[i])); |
| | | cipherToPlain[i] = (toupper(cipherToPlain[i]-'A')) - (key[k]-'A') + 26; |
| | | //printf(" k:%d",key[k]-'A'); |
| | | //printf(" A:%d",cipherToPlain[i]); |
| | | cipherToPlain[i] = cipherToPlain[i] % 26; |
| | | //printf(" B:%d",cipherToPlain[i]); |
| | | cipherToPlain[i] = cipherToPlain[i] + 'A'; |
| | | //printf(" C:%c",cipherToPlain[i]); |
| | | k++; |
| | | } |
| | | } |
| | | |
| | | int main() |
| | | { |
| | | char cipher[65535]; |
| | | char key[65535]; |
| | | printf("Enter cipher:"); |
| | | fgets(cipher,sizeof(cipher),stdin); |
| | | strtok(cipher,"\n"); |
| | | printf("Enter key:"); |
| | | fgets(key,sizeof(key),stdin); |
| | | |
| | | char *plaintext; |
| | | plaintext = strdup(cipher); |
| | | decrypt_vigenere(plaintext,key); |
| | | printf("\n%s",plaintext); |
| | | free(plaintext); |
| | | return 0; |
| | | } |
| New file |
| | |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <ctype.h> |
| | | |
| | | //! Calculate index of coincidence. |
| | | /*! |
| | | Calculate index of coincidence for a specific char array |
| | | */ |
| | | // by Matthias Altmann |
| | | double calcIdxOfCoInc(char * str) |
| | | { |
| | | int i,count; |
| | | int sum=0; |
| | | for (int letter='A';letter<='Z';letter++) |
| | | { |
| | | for (i=0, count=0; str[i];i++) |
| | | { |
| | | count = count + (toupper(str[i]) == letter); |
| | | } |
| | | // calculate upper index of coincidence |
| | | sum = sum + count * (count - 1); |
| | | } |
| | | int sizeofStr = strlen(str); |
| | | return (sizeofStr<=1) ? 0.0 : sum/(double)((sizeofStr)*(sizeofStr-1)); |
| | | } |
| | | |
| | | double calcAvgIdxOfCoInc(int keysize, char * cipher) |
| | | { |
| | | double sum = 0.0; |
| | | // generate textes jumping keysize, offset 1..keysize |
| | | char * tempcipher = (char*) malloc(strlen(cipher) * sizeof(char)); |
| | | for (int i = 1; i <= keysize; i++) |
| | | { |
| | | int k = 0; // counter for array to create new text jumping keysize |
| | | for (int j=i-1;j<strlen(cipher);j=j+keysize) |
| | | { |
| | | tempcipher[k]=cipher[j]; |
| | | k++; |
| | | } |
| | | tempcipher[k]='\0'; // terminating character |
| | | /* printf("\n%s",tempcipher); |
| | | float d = calcIdxOfCoInc(tempcipher); |
| | | printf("\n%2.13f", d);*/ |
| | | sum = sum + calcIdxOfCoInc(tempcipher); |
| | | } |
| | | free(tempcipher); |
| | | return (sum / keysize); |
| | | } |
| New file |
| | |
| | | #pragma once |
| | | |
| | | //! Calculates average index of coincidence for a given key length |
| | | /*! |
| | | * formula: sum_i=A_to_i=Z(fi*(fi-1)) / N * (N-1), |
| | | * where |
| | | * fi = count of letters (A,B,C...) |
| | | * N = total number of letters in the ciphertext. |
| | | * |
| | | \param keysize key size to check. |
| | | \param cipher cipher to analyse. |
| | | */ |
| | | double calcAvgIdxOfCoInc(int keysize, char * cipher); |
| | | |
| New file |
| | |
| | | #pragma once |
| | | |
| | | // by Matthias Altmann |
| | | const double letter_freq_english [] = { |
| | | 8.167, // A |
| | | 1.492, // B |
| | | 2.782, // C |
| | | 4.253, // D |
| | | 12.702, // E |
| | | 2.228, // F |
| | | 2.015, // G |
| | | 6.094, // H |
| | | 6.966, // I |
| | | 0.153, // J |
| | | 0.772, // K |
| | | 4.025, // L |
| | | 2.406, // M |
| | | 6.749, // N |
| | | 7.507, // O |
| | | 1.929, // P |
| | | 0.095, // Q |
| | | 5.987, // R |
| | | 6.327, // S |
| | | 9.056, // T |
| | | 2.758, // U |
| | | 0.978, // V |
| | | 2.360, // W |
| | | 0.150, // X |
| | | 1.974, // Y |
| | | 0.074 // Z |
| | | }; |
| | | |
| | | |
| New file |
| | |
| | | Enter cipher:IIPVIBQXSVLNBXSVLPOOMGARAYVLYRARKKUIGPEMCDZWCHOHGGGXMHRYPESRDEGDYSFLMLBXPHIGGTEWEZYTZGVRJIQHFRPXSIRXLTOMTAYG |
| | | Enter key:MEETUP |
| | | |
| | | WELCOMETOCRYPTOCRACKINGCOURSECONGRATULATIONSYOUSUCCESSFULLYCRACKEDTHISHIDDENMESSAGEENCRYPTEDBYVIGENERECIPHER |
| New file |
| | |
| | | #include <iostream> |
| | | #include <string> |
| | | #include <algorithm> |
| | | #include <cctype> |
| | | |
| | | using namespace std; |
| | | |
| | | /*! |
| | | * \brief Calculates exclusions for enigma ciphertext. |
| | | */ |
| | | // by Matthias Altmann |
| | | int main() |
| | | { |
| | | string cipher; |
| | | cout << "Enter ciphertext:"; |
| | | getline(cin,cipher); |
| | | transform(cipher.begin(), cipher.end(),cipher.begin(), ::toupper); |
| | | |
| | | string assumed_plaintext; |
| | | cout << "Enter assumed plaintext:"; |
| | | getline(cin,assumed_plaintext); |
| | | transform(assumed_plaintext.begin(), assumed_plaintext.end(), assumed_plaintext.begin(), ::toupper); |
| | | |
| | | // cipher text must be larger than plain text. |
| | | if (cipher.size() < assumed_plaintext.size()) |
| | | { |
| | | cout << "Ciphertext must be larger than plaintext. Exiting. " << endl; |
| | | return 1; |
| | | } |
| | | |
| | | cout << "\t" << cipher; |
| | | string shift = ""; |
| | | for (int i=0;i<=(cipher.size()-assumed_plaintext.size());i++){ |
| | | string tempstr = assumed_plaintext; |
| | | bool toDiscard = false; |
| | | for (int j=0;j<assumed_plaintext.size();j++) |
| | | { |
| | | if (cipher[i+j]==assumed_plaintext[j]) |
| | | { |
| | | tempstr[j]=tolower(tempstr[j]); |
| | | toDiscard = true; |
| | | } |
| | | } |
| | | cout << endl; |
| | | cout << ((toDiscard==true) ? "X\t" : "\t") << shift << tempstr; shift.append(" "); |
| | | } |
| | | cout << endl; |
| | | return 0; |
| | | } |
| | | |
| New file |
| | |
| | | HJYPDOMQNJCOSGAWHLEIHYSOPJSMNU |
| | | |
| | | gegen Klartext: |
| | | |
| | | KeineBesonderenEreignisse |
| New file |
| | |
| | | #! /usr/bin/ruby |
| | | # by Matthias Altmann |
| | | |
| | | def is_prime?(nr_to_check) |
| | | divisor = 2 |
| | | while (divisor < nr_to_check) do |
| | | if (nr_to_check % divisor == 0) |
| | | return false |
| | | end |
| | | divisor = divisor + 1 |
| | | end |
| | | return true |
| | | end |
| | | |
| | | for number in 2..700 |
| | | if (is_prime?(number) == true) |
| | | puts "#{number}" |
| | | end |
| | | end |
| New file |
| | |
| | | Subproject commit a349ff43c58c23f9c837b8ea9b5fc7d4761b8de3 |