- Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define VALID_CHAR(c) strchr(ALPHABET, (c))
#define pressENTER() \
do{ \
char mYcHAr; \
printf( "press ENTER..." ); \
while ( (mYcHAr=getchar()) != '\n' && mYcHAr != EOF ) \
; \
}while(0)
/*********************************************************//**
* http://en.wikipedia.org/wiki/Caesar_cipher#Example (
************************************************************/
char encrypt( const char c, const int shiftby )
{
if ( !VALID_CHAR(c) || shiftby < 0 )
return '\0';
return ALPHABET[ ((c-'A') + shiftby) % 26 ];
}
/*********************************************************//**
* http://en.wikipedia.org/wiki/Caesar_cipher#Example (
************************************************************/
char decrypt( const char c, const int shiftby )
{
int idx = 0;
if ( !VALID_CHAR(c) || shiftby < 0 )
return '\0';
idx = ((c-'A') - shiftby) % 26;
if (idx < 0 )
idx += 26;
return ALPHABET[ idx ];
}
/*********************************************************//**
*
************************************************************/
int main( void )
{
char c = 'Z', ce = '\0';
int shiftby = 2;
ce = encrypt(c, shiftby);
printf( "encrypt(%c, %d) = %c\n", c, shiftby, ce );
printf( "decrypt(%c, %d) = %c\n", ce, shiftby, decrypt(ce, shiftby) );
pressENTER();
exit( EXIT_SUCCESS );
}
που χρησιμοποιεί μόνο κεφαλαία λατινικά γράμματα. Ότι πληροφορία χρειάζεσαι την εξηγεί στη σελίδα της Wikipedia. Εξαίρεση αποτελεί η παραλλαγή της αποκρυπτογράφησης, επειδή το mod (%) στη C δεν λειτουργεί με τον τρόπο που θεωρεί η Wikipedia (το επισημαίνει κιόλας):
wikipedia έγραψε:
(There are different definitions for the modulo operation. In the above, the result is in the range 0...25. I.e., if x+n or x-n are not in the range 0...25, we have to subtract or add 26.)
Ουσιαστικά σου δίνω έτοιμο τον πυρήνα του Ceasar cipher/decipher υλοποιημένο σε C. Συνέχισέ το από εδώ για να το κάνεις ολοκληρωμένο πρόγραμμα με αρχή, μέση και τέλος, με όποιες προσθήκες/αλλαγές κρίνεις απαραίτητες.
Όταν το τελειώσεις, προσθέτεις και τον Vigenère cipher



