Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Affine cipher (WTFPL 2014 Jake Sebastian-Jones)
- */
- #include<stdio.h>
- #include<stdlib.h>
- char inverse_values[26] = {0, 1, 0, 9, 0, 21, 0, 15, 0, 3, 0, 19, 0,
- 0, 0, 7, 0, 23, 0, 11, 0, 5, 0, 17, 0, 25};
- char encrypt(char x, int a, int b) {
- if( x < 'A' || (x > 'Z' && x < 'a') || x > 'z' ) return x;
- int oset = x <= 'Z' ? 'A' : 'a';
- return (a * (x - oset) + b) % 26 + oset;
- }
- char decrypt(char x, int inv, int b) {
- if( x < 'A' || (x > 'Z' && x < 'a') || x > 'z' ) return x;
- int oset = (x <= 'Z' ? 'A' : 'a');
- int chk = inv * ((x - oset) - b) % 26;
- return (chk >= 0 ? chk : 26 + chk) + oset;
- }
- int main(int argc, char **argv) {
- char c, inv;
- int a = atoi(argv[1]);
- int b = atoi(argv[2]) % 26;
- if(a > 25 || (inv = inverse_values[a]) == 0) {
- fprintf(stderr, "Error: 'a' value must be less than "
- "and coprime with 26\n");
- return 64;
- }
- while( (c = fgetc(stdin)) != EOF) {
- #ifdef DECRYPT
- fprintf(stdout, "%c", decrypt(c, inv, b));
- #else
- fprintf(stdout, "%c", encrypt(c, a, b));
- #endif
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement