In: Computer Science
Implement a Message Authentication Code program in either C/C++ or Python.
1. Accept a message as keyboard input to your program.
2. Accept a secret key for the sender/recipient as keyboard input to your program.
3. Your hash function H() is simply the checksum. To compute the checksum, you add all the characters of the string in ASCII codes. For example, the checksum of a string "TAMUC" would be 84 + 65 + 77 + 85 + 67 = 378.
Concatenate the secret key of the sender/recipient (from step 2) and the message (from step 1) and compute the checksum of the concatenated string. For ASCII codes, refer to the following website:
http://www.asciitable.com
4. Accept a secret key for the attacker as keyboard input to your program.
5. The attacker modifies the message from step 1. The original message can be modified any way you want.
6. Concatenate the secret key of the attacker (from step 4) and the modified message (from step 5) and compute the checksum of the concatenated string.
7. Concatenate the secret key of the sender/recipient (from step 2) and the modified message (from step 5) and compute the checksum of the concatenated string.
8. Compare the checksum from step 7 and the checksum from step 6. See if they match or not.
9. Compare the checksum from step 3 and the checksum from step 6. See if they match or not.
NOTE: your program should have separate functions for the checksum and the message modification by the attacker.
#include <bits/stdc++.h>
using namespace std;
int checksum(string s)
{
int ans=0;
int n = s.size();
char* arr;
arr = &s[0];
for (int i= 0; i<n; i++)
{ ans += (int)arr[i]; }
return ans;
}
string modify(string s)
{
string temp;
cout<<"Enter the modified message"<<endl;
cin>>temp;
int n = temp.size();
s.replace(0,n,temp);
return s;
}
int main()
{
string s , se ,seh;//declaring the strings
cout<<"Enter the message"<<endl;
cin>>s;
cout<<"Enter the secret "<<endl;
cin>>se;
int secret = checksum(s+se);
cout<<"Enter the secret key for hacker"<<endl;
cin>>seh;
s = modify(s);//function to modify the string
int mod = checksum(s+seh);//finds the check sum of modified message
+ secret key from attacker
int mods = checksum(s+se);//finds the checksum of modified message
+ secret key from sender
if(mods == mod) cout<<"checksum of modified message+secret
key from sender is equal to checksum of modified message+secret key
from attacker"<<endl;
else cout<<"checksum of modified message+secret key from
sender is not equal to checksum of modified message+secret key from
attacker"<<endl;
if(secret == mod) cout<<"checksum of original message+secret
key from sender is equal to checksum of modified message+secret key
from attacker "<<endl;
else cout<<"checksum of original message+secret key from
sender is not equal to checksum of modified message+secret key from
attacker "<<endl;
return 0;
}