Birat Multiple College
Tribhuwan University
Institute of Science and Technology
Lab Report Of
Cryptography
Submitted To:
Department of Computer Science and Information Technology
Birat Multiple College
Submitted By:
Anuj Dhungana
April 2025
Contents
1. WAP to implement Caesar Cipher. .......................................................................................................... 4
2. WAP to implement Hill Cipher. ................................................................................................................ 4
3. WAP to implement Playfair Cipher. ......................................................................................................... 5
4. WAP to implement Vigenere Cipher. ....................................................................................................... 8
5. WAP to implement Rail Fence Cipher. ................................................................................................... 10
6. WAP to implement DES Algorithm. ....................................................................................................... 11
7. WAP to implement RSA Algorithm. ....................................................................................................... 12
8. WAP to implement Diffie-Hellman Key Exchange Algorithm. .............................................................. 14
9. WAP to implement MD5 Hashing Algorithm......................................................................................... 15
10. WAP to implement AES Algorithm .................................................................................................... 17
1. WAP to implement Caesar Cipher.
#include<stdio.h>
#include<string.h>
#include<ctype.h>
void main() {
char plain[100], cipher[100];
int key, i, length;
int result;
printf("Enter the plain text : ");
scanf("%s", plain);
printf("Enter the key value : ");
scanf("%d", &key);
printf("PLAIN TEXT : %s \n", plain);
printf("CIPHER TEXT : ");
for(i = 0, length = strlen(plain); i < length; i++) {
cipher[i] = plain[i] + key;
if (isupper(plain[i]) && (cipher[i] > 'Z'))
cipher[i] = cipher[i] - 26;
if (islower(plain[i]) && (cipher[i] > 'z'))
cipher[i] = cipher[i] - 26;
printf("%c", cipher[i]);
}
printf("\nAFTER DECRYPTION: ");
for(i = 0; i < length; i++) {
plain[i] = cipher[i] - key;
if (isupper(cipher[i]) && (plain[i] < 'A'))
plain[i] = plain[i] + 26;
if (islower(cipher[i]) && (plain[i] < 'a'))
plain[i] = plain[i] + 26;
printf("%c", plain[i]);
}
printf("\n");
}
Output:
Enter the plain text : BIRATMULTIPLECOLLEGE
Enter the key value : 10
PLAIN TEXT : BIRATMULTIPLECOLLEGE
CIPHER TEXT : LSBKDWEVDSZVOMYVVOQO
AFTER DECRYPTION: BIRATMULTIPLECOLLEGE
2. WAP to implement Hill Cipher.
#include<stdio.h>
#include<string.h>
void main(){
unsigned int a[3][3] = {{6,24,1},{13,16,10},{20,17,15}};
unsigned int b[3][3] = {{8,5,10},{21,8,21},{21,12,8}};
int i,j,t = 0; char msg[20];
unsigned int c[20],d[20];
printf("Enter the plain text : ");
scanf("%s",msg);
for(i = 0; i<strlen(msg); i++){
c[i] = msg[i]-65;
printf("%d ",c[i]);
}
for(i = 0; i< 3; i++){
t = 0;
for(j = 0; j<3; j++){
t = t + (a[i][j]*c[j]);
}
d[i] = t%26;
}
printf("\nEncrypted Cipher text : ");
for(i = 0; i < 3; i ++)
printf("%c ",d[i]+65);
for(i = 0; i<3; i++){
t = 0;
for(j = 0; j<3; j++){
t = t+(b[i][j]*d[j]);
}
c[i] = t % 26;
}
printf("\nDecrypted cipher text : ");
for(i = 0; i< 3; i++)
printf("%c ",c[i]+65);
}
Output:
Enter the plain text : RAM
17 0 12
Encrypted Cipher text : K D A
Decrypted cipher text : R A M
3. WAP to implement Playfair Cipher.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define MX 5
void playfair(char ch1, char ch2, char key[MX][MX]){
int i,j,w,x,y,z;
FILE *out;
if((out = fopen("[Link]","a+")) == NULL)
printf("File Currupted.");
for(i = 0 ; i<MX; i++){
for(j = 0; j< MX; j++){
if(ch1==key[i][j]){
w = i;
x = j;
}
else if(ch2 == key[i][j]){
y = i;
z = j;
}
}
}
if(w==y){
x = (x +1)%5;
z = (z+1)%5;
printf("%c%c",key[w][x],key[y][z]);
fprintf(out,"%c%c",key[w][x],key[y][z]);
}
else if (x == z){
w = (w+1)%5;
y = (y+1)%5;
printf("%c%c",key[w][x],key[y][z]);
fprintf(out,"%c%c",key[w][x],key[y][z]);
}
else{
printf("%c%c",key[w][z],key[y][x]);
fprintf(out,"%c%c",key[w][z],key[y][x]);
}
fclose(out);
}
void main(){
int i,j,k =0, l, m = 0, n;
char key[MX][MX],keyminus[25],keystr[10],str[25] = {0};
char alpa[26] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
printf("Enter key : ");
gets(keystr);
printf("Enter the plain text : ");
gets(str);
n = strlen(keystr);
for (i = 0; i< n; i ++){
if(keystr[i]=='j')
keystr[i] = 'i';
else if (keystr[i] == 'J')
keystr[i] = 'I';
keystr[i] = toupper(keystr[i]);
}
for(i=0; i<strlen(str); i++){
if(str[i]=='j')
str[i] = 'i';
else if(str[i]== 'J')
str[i] = 'I';
str[i] = toupper(str[i]);
}
j = 0;
for(i = 0; i<26; i++){
for(k = 0; k<n; k++){
if(keystr[k] == alpa[i])
break;
else if(alpa[i] == 'J')
break;
}
if(k == n){
keyminus[j] = alpa[i];
j++;
}
}
k = 0;
for(i = 0; i<MX; i++){
for(j = 0; j< MX; j++){
if(k<n){
key[i][j]=keystr[k];
k++;
}
else{
key[i][j] = keyminus[m];
m++;
}
printf("%c ",key[i][j]);
}
printf("\n");
}
printf("\n\n Entered text : %s \n Cipher text: ",str);
for(i = 0; i < strlen(str); i ++){
if(str[i]== 'J')
str[i]='I';
if(str[i+1]== '\0')
playfair(str[i],'X',key);
else{
if(str[i+1] == 'J')
str[i+1] = 'I';
if(str[i] == str[i+1])
playfair(str[i],'X',key);
else{
playfair(str[i],str[i+1],key);
i++;
}
}
}
}
Output:
Enter key : ANUJ
Enter the plain text : DHUNGANA
A N U I B
C D E F G
H K L M O
P Q R S T
V W X Y Z
Entered text : DHUNGANA
Cipher text: CKIUCBUN
4. WAP to implement Vigenere Cipher.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
void encipher();
void decipher();
int main() {
int choice;
while (1) {
printf("\n1. Encrypt Text");
printf("\n2. Decrypt Text");
printf("\n3. Exit");
printf("\n\nEnter Your Choice: ");
scanf("%d", &choice);
getchar(); // To consume the newline character left in the buffer
if (choice == 3)
exit(0);
else if (choice == 1)
encipher();
else if (choice == 2)
decipher();
else
printf("\nInvalid Choice! Try Again.\n");
}
return 0;
}
void encipher() {
char text[100], key[100];
printf("\nEnter Plaintext: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0'; // Remove trailing newline
printf("Enter Key: ");
fgets(key, sizeof(key), stdin);
key[strcspn(key, "\n")] = '\0';
int textLen = strlen(text);
int keyLen = strlen(key);
char encryptedText[textLen + 1];
for (int i = 0, j = 0; i < textLen; i++) {
char c = text[i];
if (isalpha(c)) { // Encrypt only letters
char base = isupper(c) ? 'A' : 'a';
encryptedText[i] = (c - base + (toupper(key[j]) - 'A')) % 26 + base;
j = (j + 1) % keyLen;
} else {
encryptedText[i] = c; // Keep non-alphabetic characters unchanged
}
}
encryptedText[textLen] = '\0';
printf("Encrypted Text: %s\n", encryptedText);
}
void decipher() {
char text[100], key[100];
printf("\nEnter Ciphertext: ");
fgets(text, sizeof(text), stdin);
text[strcspn(text, "\n")] = '\0';
printf("Enter Key: ");
fgets(key, sizeof(key), stdin);
key[strcspn(key, "\n")] = '\0';
int textLen = strlen(text);
int keyLen = strlen(key);
char decryptedText[textLen + 1];
for (int i = 0, j = 0; i < textLen; i++) {
char c = text[i];
if (isalpha(c)) { // Decrypt only letters
char base = isupper(c) ? 'A' : 'a';
decryptedText[i] = (c - base - (toupper(key[j]) - 'A') + 26) % 26 + base;
j = (j + 1) % keyLen;
} else {
decryptedText[i] = c;
}
}
decryptedText[textLen] = '\0';
printf("Decrypted Text: %s\n", decryptedText);
}
Output:
1. Encrypt Text
2. Decrypt Text
3. Exit
Enter Your Choice: 1
Enter Plaintext: computer
Enter Key: cab
Encrypted Text: eonruugr
1. Encrypt Text
2. Decrypt Text
3. Exit
Enter Your Choice: 2
Enter Ciphertext: eonruugr
Enter Key: cab
Decrypted Text: computer
1. Encrypt Text
2. Decrypt Text
3. Exit
Enter Your Choice: 3
5. WAP to implement Rail Fence Cipher.
#include <stdio.h>
#include <string.h>
void encryptRailFence(char *text, int key) {
int len = strlen(text);
char rail[key][len];
memset(rail, '\n', sizeof(rail));
int row = 0, down = 1;
for (int i = 0; i < len; i++) {
rail[row][i] = text[i];
if (row == key - 1)
down = 0;
else if (row == 0)
down = 1;
row += down ? 1 : -1;
}
printf("Encrypted Text: ");
for (int i = 0; i < key; i++)
for (int j = 0; j < len; j++)
if (rail[i][j] != '\n')
printf("%c", rail[i][j]);
printf("\n");
}
void decryptRailFence(char *cipher, int key) {
int len = strlen(cipher);
char rail[key][len];
memset(rail, '\n', sizeof(rail));
int row = 0, down = 1;
for (int i = 0; i < len; i++) {
rail[row][i] = '*';
if (row == key - 1)
down = 0;
else if (row == 0)
down = 1;
row += down ? 1 : -1;
}
int index = 0;
for (int i = 0; i < key; i++)
for (int j = 0; j < len; j++)
if (rail[i][j] == '*' && index < len)
rail[i][j] = cipher[index++];
printf("Decrypted Text: ");
row = 0, down = 1;
for (int i = 0; i < len; i++) {
printf("%c", rail[row][i]);
if (row == key - 1)
down = 0;
else if (row == 0)
down = 1;
row += down ? 1 : -1;
}
printf("\n");
}
int main() {
char text[100];
int key;
printf("Enter the text to encrypt: ");
gets(text);
printf("Enter the key (number of rails): ");
scanf("%d", &key);
getchar();
encryptRailFence(text, key);
char cipher[100];
printf("Enter the text to decrypt: ");
gets(cipher);
decryptRailFence(cipher, key);
return 0;
}
Output:
Enter text to encrypt: informationtechnology
Enter the key (number of rails): 2
Encrypted Text: ifrainehooynomtotcnlg
Enter text to decrypt: ifrainehooynomtotcnlg
Decrypted Text: informationtechnology
6. WAP to implement DES Algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int initial_perm[64] = {
58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4,
62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8,
57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7
};
int final_perm[64] = {
40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31,
38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29,
36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27,
34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25
};
void permute(unsigned char *input, unsigned char *output, int *table, int size) {
memset(output, 0, size / 8);
for (int i = 0; i < size; i++) {
int bit = (input[(table[i] - 1) / 8] >> (7 - ((table[i] - 1) % 8))) & 1;
output[i / 8] |= bit << (7 - (i % 8));
}
}
void des_encrypt(unsigned char *plaintext, unsigned char *ciphertext) {
unsigned char temp[8] = {0};
permute(plaintext, temp, initial_perm, 64);
permute(temp, ciphertext, final_perm, 64);
}
void des_decrypt(unsigned char *ciphertext, unsigned char *decryptedtext) {
unsigned char temp[8] = {0};
permute(ciphertext, temp, final_perm, 64);
permute(temp, decryptedtext, initial_perm, 64);
decryptedtext[8] = '\0';
}
int main() {
unsigned char plaintext[8] = "COMPUTER", ciphertext[8] = {0}, decryptedtext[9] = {0};
printf("Original Text: %s\n", plaintext);
des_encrypt(plaintext, ciphertext);
printf("Encrypted Text (Hex): ");
for (int i = 0; i < 8; i++) printf("%02X ", ciphertext[i]);
printf("\n");
des_decrypt(ciphertext, decryptedtext);
printf("Decrypted Text: %s\n", decryptedtext);
return 0;
}
Output:
Original Text: ANUJDHUN#
Encrypted Text (Hex): 41 4E 55 4A 44 48 55 4E
Decrypted Text: ANUJDHUN
7. WAP to implement RSA Algorithm.
#include <stdio.h>
int p = 13, q = 11;
int gcd(int a, int b) {
return (a == 0) ? b : gcd(b % a, a);
}
int modexp(int base, int exp, int mod) {
int result = 1;
for (int i = 0; i < exp; i++) {
result = (result * base) % mod;
}
return result;
}
int main() {
int n = p * q, f = (p - 1) * (q - 1), e, d = 2, i;
char m[100];
printf("n = %d\n", n);
printf("f = %d\n", f);
do {
printf("Enter public component (e): ");
scanf("%d", &e);
if (e <= 1 || e >= f || gcd(e, f) != 1) {
printf("Invalid public component. Try again.\n");
}
} while (e <= 1 || e >= f || gcd(e, f) != 1);
while ((e * d) % f != 1) {
d++;
}
printf("Computed private component (d): %d\n", d);
printf("Enter message to encrypt: ");
scanf("%s", m);
printf("Encrypted message:\n");
int encrypted[100];
for (i = 0; m[i] != '\0'; i++) {
encrypted[i] = modexp(m[i], e, n);
printf("%c -> %d\n", m[i], encrypted[i]);
}
encrypted[i] = -1;
printf("\nDecrypted message:\n");
for (i = 0; encrypted[i] != -1; i++) {
char decrypted_char = modexp(encrypted[i], d, n);
printf("%d -> %c\n", encrypted[i], decrypted_char);
}
return 0;
}}
Output:
n = 143
f = 120
Enter public component (e): 19
Computed private component (d): 19
Enter message to encrypt: ANUJDHUNGANA
Encrypted message:
A -> 65
N -> 78
U -> 84
J -> 139
D -> 94
H -> 123
U -> 84
N -> 78
G -> 20
A -> 65
N -> 78
A -> 65
Decypted message:
65 -> A
78 -> N
84 -> U
139 -> J
94 -> D
123 -> H
84 -> U
78 -> N
20 -> G
65 -> A
78 -> N
65 -> A
8. WAP to implement Diffie-Hellman Key Exchange Algorithm.
#include <stdio.h>
#include <conio.h>
long long int power(int a, int b, int mod){
long long int t;
if (b == 1)
return a;
t = power(a, b / 2, mod);
if (b % 2 == 0)
return (t * t) % mod;
else
return (((t * t) % mod) * a) % mod;
}
long int calculateKey(int a, int x, int n){
return power(a, x, n);
}
void main(){
int n, g, x, a, y, b;
printf("Enter the value of n and g: ");
scanf("%d %d", &n, &g);
printf("Enter the value of x for the first person: ");
scanf("%d", &x);
a = power(g, x, n);
printf("Enter the value of y for the second person: ");
scanf("%d", &y);
b = power(g, y, n);
printf("Key for the first person is: %lld\n", power(b, x, n));
printf("Key for the second person is: %lld\n", power(a, y, n));
getch();
}
Output:
Enter the value of n and g: 7
3
Enter the value of x for the first person: 21
Enter the value of y for the second person: 40
Key for the first person is: 1
Key for the second person is: 1
9. WAP to implement MD5 Hashing Algorithm.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
typedef union uwb {
unsigned w;
unsigned char b[4];
} MD5union;
typedef unsigned DigestArray[4];
unsigned func0(unsigned abcd[]) {
return (abcd[1] & abcd[2]) | (~abcd[1] & abcd[3]);
}
unsigned func1(unsigned abcd[]) {
return (abcd[3] & abcd[1]) | (~abcd[3] & abcd[2]);
}
unsigned func2(unsigned abcd[]) {
return abcd[1] ^ abcd[2] ^ abcd[3];
}
unsigned func3(unsigned abcd[]) {
return abcd[2] ^ (abcd[1] | ~abcd[3]);
}
typedef unsigned (*DgstFctn) (unsigned a[]);
unsigned calctable(unsigned k) {
double s, pwr;
int i;
pwr = pow(2, 32);
s = fabs(sin(k + 1));
return (unsigned)(s * pwr);
}
unsigned *md5(const char *msg, int mlen) {
static DigestArray h = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
static DgstFctn ff[] = { func0, func1, func2, func3 };
static unsigned k[64];
static unsigned r[] = { 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23, 6, 10, 15, 21 };
static int init = 1; int i;
if (init) {
for (i = 0; i < 64; i++) {
k[i] = calctable(i);
}
init = 0;
}
int newLen, offset;
for (newLen = mlen + 1; newLen % (512 / 8) != (448 / 8); newLen++);
unsigned char *msgPadded = calloc(newLen + 8, 1);
memcpy(msgPadded, msg, mlen);
msgPadded[mlen] = 0x80;
unsigned long long bitsLen = 8 * mlen;
memcpy(msgPadded + newLen, &bitsLen, 8);
for (offset = 0; offset < newLen; offset += (512 / 8)) {
unsigned *w = (unsigned *)(msgPadded + offset);
unsigned a = h[0], b = h[1], c = h[2], d = h[3], f, g, temp; int i;
for (i = 0; i < 64; i++) {
if (i < 16) {
f = ff[0]((unsigned[]){ a, b, c, d });
g = i;
} else if (i < 32) {
f = ff[1]((unsigned[]){ a, b, c, d });
g = (5 * i + 1) % 16;
} else if (i < 48) {
f = ff[2]((unsigned[]){ a, b, c, d });
g = (3 * i + 5) % 16;
} else {
f = ff[3]((unsigned[]){ a, b, c, d });
g = (7 * i) % 16;
}
temp = d;
d = c;
c = b;
b = b + ((a + f + k[i] + w[g]) << r[i % 4]) | ((a + f + k[i] + w[g]) >> (32 - r[i % 4]));
a = temp;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
}
free(msgPadded);
return h;
}
void print_md5(unsigned *digest) { int i;
for (i = 0; i < 4; i++)
printf("%02x%02x%02x%02x", digest[i] & 0xFF, (digest[i] >> 8) & 0xFF, (digest[i] >> 16) & 0xFF,
(digest[i] >> 24) & 0xFF);
printf("\n");
}
int main() {
char input[256];
unsigned *digest;
printf("Enter a string to hash: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0';
digest = md5(input, strlen(input));
printf("MD5 Hash: ");
print_md5(digest);
getch();
return 0;
}
Output:
Enter a string to hash: anujdhungana
MD5 Hash: 8062a8fc88aba045fddc8b4a75d4c01a
10. WAP to implement AES Algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define AES_BLOCK_SIZE 16
static const unsigned char sbox[256] = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
};
void SubBytes(unsigned char state[AES_BLOCK_SIZE]) { int i;
for (i = 0; i < AES_BLOCK_SIZE; i++) {
state[i] = sbox[state[i]];
}
}
void ShiftRows(unsigned char state[AES_BLOCK_SIZE]) {
unsigned char temp;
temp = state[1]; state[1] = state[5]; state[5] = state[9]; state[9] = state[13]; state[13] = temp;
temp = state[2]; state[2] = state[10]; state[10] = temp;
temp = state[6]; state[6] = state[14]; state[14] = temp;
temp = state[3]; state[3] = state[15]; state[15] = state[11]; state[11] = state[7]; state[7] = temp;
}
void AddRoundKey(unsigned char state[AES_BLOCK_SIZE], const unsigned char
key[AES_BLOCK_SIZE]) { int i;
for (i = 0; i < AES_BLOCK_SIZE; i++) {
state[i] ^= key[i];
}
}
void AES_encrypt(unsigned char state[AES_BLOCK_SIZE], const unsigned char
key[AES_BLOCK_SIZE]) {
AddRoundKey(state, key); int round;
for (round = 1; round < 10; round++) {
SubBytes(state);
ShiftRows(state);
AddRoundKey(state, key);
}
}
int main() {
unsigned char plaintext[AES_BLOCK_SIZE] = "HelloAES1234567";
unsigned char key[AES_BLOCK_SIZE] = "MySecretKey12345";
AES_encrypt(plaintext, key);
printf("Encrypted Output: "); int i;
for (i = 0; i < AES_BLOCK_SIZE; i++) {
printf("%02X ", plaintext[i]);
}
printf("\n");
return 0;
}
Output:
Encrypted Output: E7 3B 63 BD FA CE B0 1E C7 01 80 8E FE 2A 2B D0