Προς το περιεχόμενο

Γνωρίζει κανείς κρυπτογράφηση και αποκρυπτογράφηση για C;


NIOU

Προτεινόμενες αναρτήσεις

Δημοσ.

ΜΠΟΡΕΙ ΝΑ ΒΟΗΘΗΣΕΙ ΚΑΠΟΙΟΣ ΓΙΑ ΤΟ ΠΩΣ ΚΑΝΟΥΜΕ ΚΡΥΠΤΟΓΡΑΦΗΣΗ ΚΑΙ ΑΠΟΚΡΥΠΤΟΓΡΑΦΗΣΗ ΕΝΑ ΚΕΙΜΕΝΟ ΜΕ ΤΗ ΓΛΩΣΣΑ C

Δημοσ.

Ένας απλός αλγόριθμος XOR κρυπτογράφησης:

>
#include <stdio.h>
#include <string.h>

int main() {
char *text=new char,*key=new char;
printf("Keimeno gia kryptografisi:\n");
gets(text);
printf("Kleidi: ");
gets(key);
for (int i=0;i<strlen(text);i++) text[i]=text[i]^key[i%strlen(key)]*2;
printf("Kryptografimeno: %s\n",text);
for (int i=0;i<strlen(text);i++) text[i]=text[i]^key[i%strlen(key)]*2;
printf("Apokriptografimeno: %s\n",text);
getchar();
}

Αν θες κάτι πιο συγκεκριμένο δώσε μας περισσότερες πληροφορίες.

Δημοσ.
@Aesmade: O kwdikas sou de mou ginetai compile... einai sigoura swstos?

Για μένα λειτουργεί (χρησιμοποιώ το visual studio .net 2003)... Τι errors σου δίνει;

Δημοσ.
@Aesmade: O kwdikas sou de mou ginetai compile... einai sigoura swstos?

 

Δοκίμασε να κάνεις compile το πρόγραμμα του Aesmade ως C++ με τις παρακάτω αλλαγές:

 

>
#include <stdio.h>
#include <string.h>

int main()
{
char *text=new char[bUFSIZ],*key=new char[bUFSIZ];
printf("Keimeno gia kryptografisi:\n");
gets(text);

printf("Kleidi: ");
gets(key);

for (int i=0;i<strlen(text);i++) 
	text[i]=text[i]^key[i%strlen(key)]*2;
printf("Kryptografimeno: %s\n",text);
for (int i=0;i<strlen(text);i++) 
	text[i]=text[i]^key[i%strlen(key)]*2;

printf("Apokriptografimeno: %s\n",text);
getchar();	

delete	text;
delete	key;
}

 

Ο αρχικός κώδικας δεν δηλώνει το μέγεθος του text και key char στην new (text=new char και key=new char αντί text=new char[N] και key=new char[N]) οπότε τα δηλώνω τώρα ως BUFSIZ (N=BUFSIZ) δηλαδή 512Bytes έκαστο (το BUFSIZ είναι stdio.h constant) ώστε να αποφύγουμε buffer overrun κτλ.

 

Ακολουθεί η καθαρή C++ STL εκδοχή του κώδικα:

 

>
#include <iostream>
#include <string>

using namespace std;

int main()
{
string	text,
	key;	

cout<<"Keimeno gia kryptografisi:";
getline(cin,text,'\n');	
	
cout<<"Kleidi: ";
getline(cin,key,'\n');

for (int i=0;i<text.length();i++) 
	text[i]=text[i]^key[i%key.length()]*2;
cout<<"Kryptografimeno:"<<text<<endl;

for (int i=0;i<text.length();i++) 
	text[i]=text[i]^key[i%key.length()]*2;
cout<<"Apokriptografimeno:"<<text<<endl;

   cout<<"Press Enter to quit..";
cin.get();
return 0;
}

 

Ακολουθεί η καθαρή ANSI-C εκδοχή του κώδικα:

 

>
#include <stdio.h>
#include <string.h>

int main()
{
char text[bUFSIZ],
	 key[bUFSIZ];
int	i;

printf("Keimeno gia kryptografisi:\n");
gets(text);

printf("Kleidi: ");
gets(key);

for (i=0;i<strlen(text);i++) 
	text[i]=text[i]^key[i%strlen(key)]*2;
printf("Kryptografimeno: %s\n",text);
for (i=0;i<strlen(text);i++) 
	text[i]=text[i]^key[i%strlen(key)]*2;

printf("Apokriptografimeno: %s\n",text);
getchar();

return 0;	
}

Δημοσ.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

// Function Prototypes

void usage(char *);

void encipher(FILE *, char *);

void decipher(FILE *, char *);

int islower(char);

int isupper(char);

int ishash(char);

int islatin(char);

 

// Auxiliary functions

int islower(char chr) {

return ((chr >= 'a') && (chr <= 'z'));

}

 

int isupper(char chr) {

return ((chr >= 'A') && (chr <= 'Z'));

}

 

int islatin(char chr) {

return ( isupper(chr) || islower(chr) );

}

 

int ishash(char chr) {

return (chr == '#');

}

 

/*

* Given an opened file handler and a key encrypt the content of the

* the input file and output the result on the screen

*/

void encipher(FILE *fp, char *key)

{

int chr, chr_new; // the current letter: text, encrypted text

int k_chr; // the current letter of the key

int key_length; // the length of the key

 

// identify length of the key

key_length = strlen(key);

 

while ( (chr = fgetc(fp)) != EOF)

{

// first charcter of the key

k_chr = *key;

 

// if end of key string reset the key to its first character

if (k_chr == '\0') {

// Subtract key_length bytes from the current key pointer.

// Since a string is consecutive in memory, this takes

// the key pointer back to its first character.

key = key - key_length;

k_chr = *key; // k_chr is a shortcut to *key

}

 

// encrypt only characters of the latin alphabet

if ( islatin(chr) ) {

 

/* if the current key_char is lowercase subtract 97 ('a') */

if (islower(k_chr)) {

chr_new = chr + k_chr - 'a';

} /* else subtract 65 ('A') */

else if (isupper(k_chr)) {

chr_new = chr + k_chr - 'A';

}

 

// Adjust chr_new if it is out of range A-Z and a-z

if (chr_new > 'z') {

fputc('#', stdout);

chr_new -= 26;

}

else if (chr_new > 'Z' && chr_new < 'a' ) {

fputc('#', stdout);

chr_new -= 26;

}

 

*key++;

fputc(chr_new, stdout);

}

else { // no encryption is taking place

fputc(chr, stdout);

}

}

}

 

void decipher(FILE *fp, char* key)

{

int chr, chr_new; // the current letter: text, encrypted text

int k_chr; // the current letter of the key

int key_length; // the length of the key

int hashon; // a flag which indicates if a character is #

 

// identify length of the key

key_length = strlen(key);

 

while ( (chr = fgetc(fp)) != EOF)

{

// the first character is always presumably not a #

hashon = 0;

 

// first charcter of the key

k_chr = *key;

 

// if end of key string reset the key to its first character

if (k_chr == '\0') {

// Subtract key_length bytes from the current key pointer.

// Since a string is consecutive in memory, this takes

// the key pointer back to its first character.

key = key - key_length;

k_chr = *key;

}

 

if (ishash(chr)) {

// Get next character

chr = fgetc(fp);

hashon = 1;

}

 

// encrypt only characters of the latin alphabet

if ( islatin(chr) ) {

 

/* if the added key_char is lowercase */

if (islower(k_chr)) {

chr_new = chr - k_chr + 'a';

}

else if (isupper(k_chr)) {

chr_new = chr - k_chr + 'A';

}

 

// if previous letter was a # add 26 to the next character

if (hashon == 1) {

chr_new += 26;

}

 

*key++;

fputc(chr_new, stdout);

}

else {

fputc(chr, stdout);

}

 

}

}

 

/*

* This function will prompt the user with the correct usage - whenver invoked

*/

void usage(char *s)

{

fprintf(stderr,"Usage: %s -enc or -dec -cipher <maximum 10 letters> <input-file>\n", s);

exit(EXIT_FAILURE);

}

 

 

int main(int argc, char *argv[])

{

int i;

int encrypt = -1;

char filename[65];

char cipher[11];

FILE *fp;

 

/* Memory allocation & initialization */

 

/* exactly 5 parameters */

if (argc != 5)

usage(argv[0]);

 

/* parsing parameters in any order */

for( i=1; i<argc; i++) {

if (strcmp(argv, "-enc") == 0) {

encrypt = 1;

}

else if (strcmp(argv, "-dec") == 0) {

encrypt = 0;

}

else if (strcmp(argv, "-cipher") == 0) {

if (i!=5) {

strncpy(cipher, argv[++i], 10);

}

else {

usage(argv[0]);

}

}

else {

// copy the filename

strncpy(filename, argv, 64);

}

}

 

// sanity check - all parameters have to be provided

if (encrypt == -1) {

usage(argv[0]);

}

 

// sanity check - file has to exist and be readable.

if ((fp = fopen(filename,"r")) == NULL) {

fprintf(stderr, "File %s cannot be read\n", filename);

exit(EXIT_FAILURE);

}

 

if (encrypt) {

encipher(fp, cipher);

}

else {

decipher(fp, cipher);

}

 

fclose(fp);

 

exit(EXIT_SUCCESS);

}

Δημοσ.

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

// Function Prototypes

void usage(char *);

void encipher(FILE *, char *);

void decipher(FILE *, char *);

int islower(char);

int isupper(char);

int ishash(char);

int islatin(char);

 

// Auxiliary functions

int islower(char chr) {

return ((chr >= 'a') && (chr <= 'z'));

}

 

int isupper(char chr) {

return ((chr >= 'A') && (chr <= 'Z'));

}

 

int islatin(char chr) {

return ( isupper(chr) || islower(chr) );

}

 

int ishash(char chr) {

return (chr == '#');

}

 

/*

* Given an opened file handler and a key encrypt the content of the

* the input file and output the result on the screen

*/

void encipher(FILE *fp, char *key)

{

int chr, chr_new; // the current letter: text, encrypted text

int k_chr; // the current letter of the key

int key_length; // the length of the key

 

// identify length of the key

key_length = strlen(key);

 

while ( (chr = fgetc(fp)) != EOF)

{

// first charcter of the key

k_chr = *key;

 

// if end of key string reset the key to its first character

if (k_chr == '\0') {

// Subtract key_length bytes from the current key pointer.

// Since a string is consecutive in memory, this takes

// the key pointer back to its first character.

key = key - key_length;

k_chr = *key; // k_chr is a shortcut to *key

}

 

// encrypt only characters of the latin alphabet

if ( islatin(chr) ) {

 

/* if the current key_char is lowercase subtract 97 ('a') */

if (islower(k_chr)) {

chr_new = chr + k_chr - 'a';

} /* else subtract 65 ('A') */

else if (isupper(k_chr)) {

chr_new = chr + k_chr - 'A';

}

 

// Adjust chr_new if it is out of range A-Z and a-z

if (chr_new > 'z') {

fputc('#', stdout);

chr_new -= 26;

}

else if (chr_new > 'Z' && chr_new < 'a' ) {

fputc('#', stdout);

chr_new -= 26;

}

 

*key++;

fputc(chr_new, stdout);

}

else { // no encryption is taking place

fputc(chr, stdout);

}

}

}

 

void decipher(FILE *fp, char* key)

{

int chr, chr_new; // the current letter: text, encrypted text

int k_chr; // the current letter of the key

int key_length; // the length of the key

int hashon; // a flag which indicates if a character is #

 

// identify length of the key

key_length = strlen(key);

 

while ( (chr = fgetc(fp)) != EOF)

{

// the first character is always presumably not a #

hashon = 0;

 

// first charcter of the key

k_chr = *key;

 

// if end of key string reset the key to its first character

if (k_chr == '\0') {

// Subtract key_length bytes from the current key pointer.

// Since a string is consecutive in memory, this takes

// the key pointer back to its first character.

key = key - key_length;

k_chr = *key;

}

 

if (ishash(chr)) {

// Get next character

chr = fgetc(fp);

hashon = 1;

}

 

// encrypt only characters of the latin alphabet

if ( islatin(chr) ) {

 

/* if the added key_char is lowercase */

if (islower(k_chr)) {

chr_new = chr - k_chr + 'a';

}

else if (isupper(k_chr)) {

chr_new = chr - k_chr + 'A';

}

 

// if previous letter was a # add 26 to the next character

if (hashon == 1) {

chr_new += 26;

}

 

*key++;

fputc(chr_new, stdout);

}

else {

fputc(chr, stdout);

}

 

}

}

 

/*

* This function will prompt the user with the correct usage - whenver invoked

*/

void usage(char *s)

{

fprintf(stderr,"Usage: %s -enc or -dec -cipher <maximum 10 letters> <input-file>\n", s);

exit(EXIT_FAILURE);

}

 

 

int main(int argc, char *argv[])

{

int i;

int encrypt = -1;

char filename[65];

char cipher[11];

FILE *fp;

 

/* Memory allocation & initialization */

 

/* exactly 5 parameters */

if (argc != 5)

usage(argv[0]);

 

/* parsing parameters in any order */

for( i=1; i<argc; i++) {

if (strcmp(argv, "-enc") == 0) {

encrypt = 1;

}

else if (strcmp(argv, "-dec") == 0) {

encrypt = 0;

}

else if (strcmp(argv, "-cipher") == 0) {

if (i!=5) {

strncpy(cipher, argv[++i], 10);

}

else {

usage(argv[0]);

}

}

else {

// copy the filename

strncpy(filename, argv, 64);

}

}

 

// sanity check - all parameters have to be provided

if (encrypt == -1) {

usage(argv[0]);

}

 

// sanity check - file has to exist and be readable.

if ((fp = fopen(filename,"r")) == NULL) {

fprintf(stderr, "File %s cannot be read\n", filename);

exit(EXIT_FAILURE);

}

 

if (encrypt) {

encipher(fp, cipher);

}

else {

decipher(fp, cipher);

}

 

fclose(fp);

 

exit(EXIT_SUCCESS);

}

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...