-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaffine_encrypt.c
More file actions
43 lines (31 loc) · 826 Bytes
/
affine_encrypt.c
File metadata and controls
43 lines (31 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Author - Michael Ibeh
// License - Apache Version 2.0
#include <stdio.h>
#include <string.h>
#include "CipherSolve.h"
void affine_encrypt(void){
int i, temp, len, a, b;
char buffer[1024];
char plaintext[1024];
char ciphertext[1024];
printf("What is the plaintext you want to encrypt?\n");
scanf("%s", buffer);
printf("What is the value of a?\n");
scanf("%d", &a);
printf("What is the value of b?\n");
scanf("%d", &b);
strcpy(plaintext, buffer);
len = strlen(plaintext);
// Encryption formula is y = ax + b
for(i = 0; i < len; i++){
ciphertext[i] = plaintext[i];
// Convert ASCII to 0-26
ciphertext[i] -= 'a';
temp = (int)ciphertext[i] * a;
temp += b;
temp = temp % 26;
ciphertext[i] = 'a' + temp;
}
ciphertext[i] = '\0';
printf("The encrypted text is: %s\n", ciphertext);
}