Joined: Fri Nov 21, 2008 6:18 pm Posts: 51 Location: thessaloniki Has thanked: 0 time Have thanks: 2 time
here you can see an example of some programmes with ascii codes.. there is ceasar encryption... there is affine encryption and decryption.. and there is gcd )) well this are most kindly examples.. for people who are interestead to see some usage of ascii
well for the affine encryption and decryption i will leave it to you to find why there is a small problem with the codes and they loose the 100% encryption and decryption..it would be interesting to see your thoughts about it ..))
ceasar
Code:
#include <stdio.h> #include <string.h>
void menu(void){ printf("Please enter a number: \n" "1-Encrypt\n" "2-Decrypt\n" "3-Exit\n" "prompt > "); fflush( stdout ); }
int getchoice(void){ char buff[BUFSIZ]; int choice = 0; do { menu(); if(fgets( buff, sizeof buff, stdin ) != NULL){ /* success reading a line, does it make sense? */ if(sscanf( buff, "%d", &choice) != 1){ printf("Enter a number\n"); } }else{ /* user EOF, just exit now */ choice = 3; } }while(choice < 1 || choice > 3); return choice; }
void encode(void){ char buff[BUFSIZ]; int i = 0; int shift_value; printf( "Doing encrypt\n" ); printf("\nPlease enter the text you wish to encrypt CAPS LOCK ONLY: "); fgets(buff, sizeof(buff), stdin); printf("\nEnter your encryption shift value (anything from +1 to 25): "); scanf("%i", &shift_value); while(buff[i] != '\0'){ if(buff[i] >= 'A' && buff[i] <= 'Z'){ buff[i] = 'A' + (buff[i] - 'A' + shift_value) % 26; } i++; } printf("\n Your encrypted text is: %s \n", buff); }
void decode(void){ char buff[BUFSIZ]; int i = 0; int shift_value; printf( "Doing decrypt\n" ); printf("\nPlease enter the text you wish to decrypt CAPS LOCK ONLY: "); fgets(buff, sizeof(buff), stdin); printf("\nEnter your encryption shift value (anything from +1 to 25): "); scanf ("%i", &shift_value);
while(buff[i] != '\0'){ if(buff[i] >= 'A' && buff[i] <= 'Z'){ int c = buff[i] - 'A' - shift_value; if(c < 0) c += 26; buff[i] = 'A' + c % 26; } i++; } printf("\nYour decrypted text is: %s \n", buff); }
printf("\nTo keimeno einai %s to mikos toy %d \n",str1,strlen(str1)); n=strlen(str1);
dk[n]='\0'; h=strlen(dk); for (i=0; i<h; i++) { str1[i]=str1[i]-97; xx=(str1[i]-b)%26;/*to kano se periptosi pou o b einai megalos*/ if (xx<0) { xx=26+xx; }
int euclid(int a,int b) { if(b==0) return a; else return euclid(b,a%b); }
int main() { int n1,n2; printf("Enter two numbers to find its GCD:"); scanf("%d %d",&n1,&n2); printf("The GCD of %d and %d is %d\n",n1,n2,euclid(n1,n2)); return 0; }
well someone could think why i did two separate ways of affine encryption and decryption.. well if you find the way to add the missing lines on the code of ceasar you can find the answer for it ) it just needs the additions to make a ceaser cryptosystem to affine... well looking forward to hear your thoughts..
_________________ if you want make an effort yourself no one will make it for you... best regards
biskot188
Question subject: Re: ascii codes using of it in c
Posted: Mon Jan 12, 2009 3:00 pm
Joined: Fri Nov 21, 2008 6:18 pm Posts: 51 Location: thessaloniki Has thanked: 0 time Have thanks: 2 time
and for those who are more interestead ill give you the ceasar code in another languages just to see the usage of ascii there )) and this is printable ascii code usage..
pascall
Code:
program shfrovalka; uses crt; var text:string; m,k,c,men:integer; procedure cez(var text:string; var k:integer); begin clrscr; writeln('vvedite isxodniy text '); write('$~/> '); read(text); write('vvedite klu4 shifrovani9 '); readln(k); for m:=1 to length(text) do begin c:=ord(text[m])+k; if c>256 then c:=c-256; text[m]:=chr(c); end; end; procedure desh (var text:string); begin writeln('vvedite kluch deshifrovki' ); readln(k); for m:=1 to length(text) do begin c:=ord(text[m])-k; if c<256 then c:=c+256; text[m]:=chr(c); end; end; begin repeat clrscr; writeln('1 - shifrovka '); writeln('2 - deshifrovka '); writeln('3 - sosto9nie texta '); readln(men); case men of 1:cez(text,k); 2:desh(text); U? 3:begin writeln(text); readkey; end; end; until men = 0; end.
in c++
Code:
#include "stdafx.h"
void encode(char *input, char *output, int start); // шифрование void decode(char *input, char *output, int start); // раÑшифровка
for (unsigned int i=0; i if (!isdigit(argv[4][i])) { cout << "Величина ÑÐ¼ÐµÑ‰ÐµÐ½Ð¸Ñ Ð°Ð»Ñ„Ð°Ð²Ð¸Ñ‚Ð° должна быть чиÑлом "; exit(1); } }