Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define BUFLEN 1024
- #define ALPHABET_LETTER_COUNT 26
- #define ENGLISH_FREQUENCIES ETAOINSRHDLUCMFYWGPBVKXQJZ
- #define STR_END '\0'
- /* Buffer pou apothikeyei tin isodo prosorina gia na ektelestoun oi algorithmoi */
- int buf[BUFLEN];
- void printhelp() {
- puts (
- " [ Decipher ] -- by -- [ /fb.me/masterleo ] \n"
- " == || == == || == \n"
- " ||==================================================||\n"
- " || Execute with one of the following arguments ||\n"
- " || ||\n"
- " || -c for caesar ||\n"
- " || -a for affine ||\n"
- " || -s for subtitution ||\n"
- " || -t for transposition ||\n"
- " || -v for vigenere ||\n"
- " || ||\n"
- " || TIP: Redirect std streams for comfort ||\n"
- " || Example usage: ./cipher -a < finput > foutput ||\n"
- " ||==================================================||\n"
- );
- }
- /* sinartisi ipologismou tou a^-1 gia to affine cipher */
- inline int modinverse(int a, int m) {
- int x;
- for(a%=m,x=1;x<m;++x)
- if (a*x%m==1)
- return x;
- }
- /* sinartisi mod pou ipostirizi arnitikous */
- inline int mod(int a, int b) {
- return a%b<0?a%b+b:a%b;
- }
- /* epistrefei 'A' i 'a' analoga an to orisma ine kefaleo i mikro */
- inline int aA(int c) {
- return isupper(c)?'A':'a';
- }
- /* ipologizei ton megisto kino diereti dio arithmon */
- inline int gcd(int a, int b) {
- return a?gcd(b%a,a):b;
- }
- inline void printtitle(char *s) {
- printf("= [ %s cipher ] =\n==================================================\n\n",s);
- }
- void caesar() {
- printtitle("Caesar");
- int k=ALPHABET_LETTER_COUNT,*p=buf;
- /* diavazoume to minima ston buffer */
- while (~(*p++=getchar()) || ++*--p);
- /* efarmozoume caesar diatrexontas ton buffer gia oles tis pithanes periptoseis tou k */
- while (k--) {
- printf("- [ k=%d ] -\n",ALPHABET_LETTER_COUNT-k);
- /* diatrexo tous xaraktires tou buffer */
- for (p=buf;*p;putchar(*p++))
- /* an o xaraktiras ine grama */
- if (isalpha(*p))
- /* efarmozo caesar shift */
- *p = (*p-aA(*p)+1)%ALPHABET_LETTER_COUNT + aA(*p);
- puts("--------------------------------------------------\n");
- }
- }
- void affine() {
- printtitle("Affine");
- int a,ainv,b,count=0,*p=buf;
- /* diavazoume to minima ston buffer */
- while (~(*p++=getchar()) || ++*--p);
- /* gia kathe a apo 1 eos arithmo gramaton alfavitou */
- for (a=1;a<ALPHABET_LETTER_COUNT;++a) {
- /* an o a ke o arithmos gramaton alfavitou ine protoi metaksi tous tote */
- if (gcd(ALPHABET_LETTER_COUNT,a)==1) {
- /* ipologizoume ton a^-1 */
- ainv = modinverse(a,ALPHABET_LETTER_COUNT);
- /* gia kathe b apo 0 eos arithmo gramaton alfavitou */
- for (b=0;b<ALPHABET_LETTER_COUNT;++b) {
- printf("- [ count=%d, a=%d, ainv=%d, b=%d ] -\n",++count,a,ainv,b);
- /* diatrexo tous xaraktires tou buffer */
- for (p=buf;*p;p++)
- /* an o xaraktiras ine grama */
- if (isalpha(*p))
- /* efarmozo ton tipo apokriptografisis tou affine cipher */
- putchar(mod(ainv*((*p)-aA(*p)-b),ALPHABET_LETTER_COUNT)+aA(*p));
- else
- /* alios apla ektipono ton xaraktira */
- putchar(*p);
- puts("--------------------------------------------------\n");
- }
- }
- }
- }
- /* domi dedomenon pou periexi ta statistika stixia ton gramaton */
- struct letter {
- int frequency;
- double percent;
- int character;
- } substitutes[ALPHABET_LETTER_COUNT];
- /* sinartisi gia tin sigkrisi 2 sixnotiton pou xrisimopiiete apo tin qsort */
- int compare(const void * a, const void * b) {
- return ((struct letter *)b)->frequency-((struct letter *)a)->frequency;
- }
- void substitution() {
- printtitle("Substitution");
- int letter_count=0,*p=buf,i;
- char *eng_freq = "ETAOINSRHDLUCMFYWGPBVKXQJZ";
- /* diavazoume to minima ston buffer ke afxanoume tin antistixi thesi tou pinaka sixnotiton kata 1 */
- while (~(*p++=getchar()) || ++*--p);
- /* an o xaraktiras ine grama tote */
- if (isalpha(*(--p)))
- /* afxanoume tin antistixi thesi tou pinaka sixnotiton */
- ++(substitutes[(*p)-aA(*p++)].frequency), letter_count++;
- else
- ++p;
- /* enimeronoume tin domi ton statistikon ipologizontas ta pososta */
- for (i=0;i<ALPHABET_LETTER_COUNT;++i) {
- substitutes[i].character=i+'A';
- substitutes[i].percent= (100*(double)substitutes[i].frequency/(double)letter_count);
- }
- /* taksinomoume ton pinaka me basi tis sixnotites emfanisis */
- qsort (substitutes, ALPHABET_LETTER_COUNT, sizeof(struct letter), compare);
- /* ektiponoume ta apotelesmata ton statistikon stixion */
- puts("- Substitute array is now sorted! -\n--------------------------------------------------\n| Chr | ## | % | Eng |\n--------------------------");
- for (i=0;i<ALPHABET_LETTER_COUNT;++i)
- printf("| %c | %2d | %5.2f | %c |\n",substitutes[i].character,substitutes[i].frequency,substitutes[i].percent,eng_freq[i]);
- puts("--------------------------\n\n- Possible message -\n--------------------------------------------------");
- /* ektiponoume to apokriptografimeno minima */
- for (p=buf;*p;++p)
- if (!isalpha(*p))
- putchar(*p);
- else
- for (i=0;;++i)
- if (substitutes[i].character==(*p)) {
- putchar(eng_freq[i]);
- break;
- }
- }
- int main(int argc, char *argv[]) {
- if (argc>1) {
- if (0) {
- /* Caesar */
- } else if (!strcmp("-c",argv[1])) {
- caesar();
- /* Affine */
- } else if (!strcmp("-a",argv[1])) {
- affine();
- /* Subtitution */
- } else if (!strcmp("-s",argv[1])) {
- substitution();
- /* Help */
- } else if (!strcmp("-h",argv[1])) {
- printhelp();
- /* Error */
- } else {
- puts("Error: Invalid flag");
- printhelp();
- }
- } else {
- printhelp();
- int i,t;
- for (i=-5;i<=10;i++) {
- if(i==0) continue;
- t=i;
- t+=(t%t);
- printf("i:%d t:%d\n",i,t);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement