S AES Encryption
S AES Encryption
S AES Encryption
Batch-2
Roll-34
Date:2/8/2023
Assignment-4
S-AES encryption
Code(cpp):
#include <iostream>
#include <string>
#include <cstdlib>
#include <limits>
#include <unordered_map>
/
******************************************************************
**************************
Simplified - Advanced Encryption Standard CLASS
******************************************************************
**************************/
class SAES
{
//Round Constant
const string RC1 = "10000000"; // Left Byte RC[1] = x^3 = 1000
const string RC2 = "00110000"; // Left Byte RC[2] = x + 1 =
0011
//S-Box
string SBox[4][4] = { {"1001","0100","1010","1011"},
{"1101","0001","1000","0101"},
{"0110","0010","0000","0011"},
{"1100","1110","1111","0111"} };
string invSBox[4][4] = { {"1010","0101","1001","1011"},
{"0001","0111","1000","1111"},
{"0110","0000","0010","0011"},
{"1100","0100","1101","1110"}};
unordered_map<string,int> stringToInt;
unordered_map<int,string> intToString;
public:
SAES()
{
key0.resize(KeyLength,'x');
key1.resize(KeyLength,'x');
key2.resize(KeyLength,'x');
stringToInt["00"]=0;
stringToInt["01"]=1;
stringToInt["10"]=2;
stringToInt["11"]=3;
intToString[0]="00";
intToString[1]="01";
intToString[2]="10";
intToString[3]="11";
}
string getKey0(){
return key0;
}
string getKey1(){
return key1;
}
string getKey2(){
return key2;
}
};
/
******************************************************************
**************************
S-AES Encryption Function
******************************************************************
**************************/
string SAES::SAES_Encryption(const string& ip)
{
return cipherOp;
}
/
******************************************************************
**************************
S-AES Decryption Function
******************************************************************
**************************/
string SAES::SAES_Decryption(const string& ip)
{
return plainText;
}
/
******************************************************************
**************************
Rounds Used in Encryption
******************************************************************
**************************/
string SAES::Encryption_Rounds(const string &text, const string&
key, int round)
{
int dim = 2; // Matrix Dimensions
vector<vector<string> > StateM(dim, vector<string>(dim)); //
State matrix.
return op;
}
/
******************************************************************
**************************
SAES Key Expansion and Generation
******************************************************************
**************************/
void SAES::SAES_KEY_GENERATION(const string& key)
{
// key0
string w0 = key.substr(0,8); // w_0 is key 0 to 7 bits
string w1 = key.substr(8,8); // w_1 is key 8 to 15 bits
key0 = w0+w1;
//key1
string w1g = key_function_g(w1, RC1);
key1 = w2+w3;
//key2
string w3g = key_function_g(w3, RC2);
string w4 = XOR_OP(w2, w3g);
string w5 = XOR_OP(w4, w3);
key2 = w4+w5;
/
******************************************************************
**************************
Key Transformation Function g used in SAES
******************************************************************
**************************/
string SAES::key_function_g(const string &w, const string& RC)
{
string N0 = w.substr(0,4); // Nibble 0 is w 0 to 3 bits
string N1 = w.substr(4,4); // Nibble 1 is w 4 to 7 bits
/
******************************************************************
**************************
XOR Operation between two texts
******************************************************************
**************************/
string SAES::XOR_OP(const string &t1, const string &t2){
string result_xor;
for(int i=0; i<t1.size(); i++){
if(t1[i] == t2[i])
result_xor.push_back('0');
else
result_xor.push_back('1');
}
return result_xor;
}
/
******************************************************************
**************************
Main Function
******************************************************************
**************************/
int main()
{
char choice;
string ip_plain_txt, op_plain_txt; //plain txt to encrypt
string op_cipher_txt, ip_cipher_txt;
string key;
cout<<endl<<endl<<endl;
//system(PAUSE);
//system(CLR);
switch(ch){
//encryption
case 'e':
cout << "\n Enter Plain Text String (16-bits) : ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, ip_plain_txt);
// ip_plain_txt="0110111101101011";
//--> op_cipher_txt="0000011100111000";
// key="1010011100111011";
obj->SAES_KEY_GENERATION(key);
op_cipher_txt = obj-
>SAES_Encryption(ip_plain_txt);
break;
//decryption
case 'd':
cout << "\n Enter Cipher Text String : ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, ip_cipher_txt);
obj->SAES_KEY_GENERATION(key);
op_plain_txt = obj-
>SAES_Decryption(ip_cipher_txt);
break;
choice=ch;
}while(choice != 'n');
delete obj;
cout << "\n Press Enter To Exit. ";
cin.get();
cin.get();
return 0;
OUTPUT: