The SDES algorithm consists of key generation and encryption function f k as shown in Fig. In the key generation of SDES, two 8-bit subkeys are generated from the main 10-bit secret key. First, the key is permuted through P10. Then, the 10-bit key is divided into two halves, each with 5 bits. We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand. S-DES Encryption (Simplified) This Method takes 8 bit Plain text and 10bit key value as input that produces 8bit Cipher. (IP) Inverse Initial Permutation(IP)-1 p10 function p8 function P4 function S-DES Key Generation S-DES depends on use of a 10 bit key shared between Sender and Receiver From this key two 8 bit sub keys are produced for use. Nov 21, 2018 8.02x - Lect 16 - Electromagnetic Induction, Faraday's Law, Lenz Law, SUPER DEMO - Duration: 51:24. Lectures by Walter Lewin. They will make you ♥ Physics. Recommended for you. SDES.java is an implementation of SDES. The constructor takes the key and initializes the key schedule. The constructor takes the key and initializes the key schedule. Methods are provided to encrypt and decrypt a byte, and a static method to print a byte in binary is also provided.
![Key Key](/uploads/1/2/6/0/126064196/959756596.jpg)
An Algorithm to implement Simplified-DES encryption
![And And](/uploads/1/2/6/0/126064196/928580908.jpg)
Simplified-DES.cpp
Sdes P10 And P8 Key Generation Review
// Algorithm to implement simplified - DES encryption |
#include<bits/stdc++.h> |
usingnamespacestd; |
string Permutation(vector<int> array, string inp){ |
string out = ''; |
for(int i=0;i<array.size();i++) |
out += inp[array[i]-1]; |
return out; |
} |
classS_DES{ |
public: |
string KEY,K1,K2,IPOut,InvIPOut; |
string F1Out; |
string INPUT,OUTPUT; |
voidinitialize(string key){ |
if(key.size()!=10){ |
cout<<'nInValid Key-Length '<<key<<''<<key.size(); |
exit(1); |
} |
KEY = key; |
Keys_Generation(); |
} |
voidKeys_Generation(){ |
cout<<'Enter P10 permutation array: '; |
vector<int> P10(10,0); |
for(int i=0;i<10;i++) |
cin>>P10[i]; |
string P10_output = Permutation(P10,KEY); |
cout<<'P10 output while generating key: '<<P10_output<<endl; |
string P10_left = P10_output.substr(0,5), P10_right = P10_output.substr(5,5); |
string pl = LShift(P10_left,1), pr = LShift(P10_right,1); |
string plpr = pl+pr; |
cout<<'Enter P8 permutation array: '; |
vector<int> P8(10,0); |
for(int i=0;i<8;i++) |
cin>>P8[i]; |
K1 = Permutation(P8,plpr); |
cout<<'K1: '<<K1<<endl; |
string pl1=LShift(pl,2), pr1=LShift(pr,2); |
plpr = pl1+pr1; |
K2 = Permutation(P8,plpr); |
cout<<'K2: '<<K2<<endl; |
} |
string LShift(string input,int n){ |
string output = input; |
char firstbit; |
while(n--){ |
firstbit = output[0]; |
output = output.substr(1,output.size()-1); |
output += firstbit; |
} |
return output; |
} |
voidDES_Encryption(){ |
IP(); |
string LIP = IPOut.substr(0,4); |
string RIP = IPOut.substr(4,4); |
cout<<'IP output: '<<IPOut<<endl; |
Function_F(LIP,RIP,1); |
cout<<'Fn Output: '<<F1Out<<endl; |
string L1 = F1Out.substr(0,4), R1 = F1Out.substr(4,4); |
Function_F(R1,L1,2); |
cout<<'Fn Output second time: '<<F1Out<<endl; |
InvIP(F1Out); |
cout<<'Encrypted Cipher-string: '<<InvIPOut<<endl; |
} |
/*Method to perform Initial-Permutation*/ |
voidIP(){ |
vector<int> IP_array(8,0); |
cout<<'Enter initial Permutation array: '; |
for(int i=0;i<8;i++) |
cin>>IP_array[i]; |
IPOut = Permutation(IP_array,INPUT); |
} |
/*Method to perform Inverse of Initial-Permutation*/ |
voidInvIP(string input){ |
vector<int> InvIPArray(8,0); |
cout<<'Enter Inverse initial Permutation: '; |
for(int i=0;i<8;i++) |
cin>>InvIPArray[i]; |
InvIPOut = Permutation(InvIPArray,input); |
} |
voidFunction_F(string linput,string rinput,int key) |
{ |
cout<<'Enter E/P array: '; |
vector<int> E_P(8,0); |
for(int i=0;i<8;i++) |
cin>>E_P[i]; |
string E_POutput = Permutation(E_P,rinput); |
string EXOR_Output; |
if(key 1) |
EXOR_Output = EX_OR(E_POutput,K1); |
else |
EXOR_Output = EX_OR(E_POutput,K2); |
string LEXOR = EXOR_Output.substr(0,4),REXOR = EXOR_Output.substr(4,4); |
string SBOX0_Output=SBOX0(LEXOR); |
string SBOX1_Output=SBOX1(REXOR); |
string SBOX_Output = SBOX0_Output+SBOX1_Output; |
cout<<'Enter P4 Operation array: '; |
vector<int> P4(4,0); |
for(int i=0;i<4;i++) |
cin>>P4[i]; |
string P4_Output = Permutation(P4,SBOX_Output); |
string fk_Output = EX_OR(P4_Output,linput); |
F1Out = fk_Output + rinput; |
} |
string EX_OR(string a,string b){ |
string output = ''; |
for(int i=0;i<a.size();i++){ |
if(a[i] b[i]) |
output += '0'; |
else |
output += '1'; |
} |
return output; |
} |
string SBOX0(string l) |
{ |
cout<<'Enter Input for S0n'; |
vector<int> temp(4,0); |
vector<vector<int> > S0(4,temp); |
for(int i=0;i<4;i++){ |
for(int j = 0;j<4;j++) |
cin>>S0[i][j]; |
} |
string bits[]={'00','01','10','11'}; |
string lrow = l.substr(0,1)+l.substr(3,1),lcol = l.substr(1,1)+l.substr(2,1); |
string SO; |
int i,lr,lc,b; |
for(i=0;i<4;i++){ |
if(lrow bits[i]) |
lr=i; |
if(lcol bits[i]) |
lc=i; |
} |
b=S0[lr][lc]; |
return bits[b]; |
} |
string SBOX1(string l) |
{ |
cout<<'Enter Input for S1n'; |
vector<int> temp(4,0); |
vector<vector<int> > S0(4,temp); |
for(int i=0;i<4;i++){ |
for(int j = 0;j<4;j++) |
cin>>S0[i][j]; |
} |
string bits[]={'00','01','10','11'}; |
string lrow = l.substr(0,1)+l.substr(3,1),lcol = l.substr(1,1)+l.substr(2,1); |
string SO; |
int i,lr,lc,b; |
for(i=0;i<4;i++){ |
if(lrow bits[i]) |
lr=i; |
if(lcol bits[i]) |
lc=i; |
} |
b=S0[lr][lc]; |
return bits[b]; |
} |
}; |
intmain() |
{ |
int i,n=10,choice; |
string key; |
S_DES S; |
while(1){ |
cout<<'nWhat do you want to do..n1. Encryptionn2. ExitnnEnter the choice? '; |
cin>>choice; |
switch(choice){ |
case1: |
cout<<'nEnter the 10-bits KEY: '; |
cin>>key; |
cout<<'nNotedown this key, as same key is used for Decryptionn'; |
S.initialize(key); |
cout<<'Enter string to encrypt: '; |
cin>>S.INPUT; |
S.DES_Encryption(); |
break; |
case2: |
exit(0); |
default: |
cout<<'nWrong Choice Enter againnPress any key to return to Main Menu..'; |
break; |
} |
} |
return0; |
} |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
- Rotates the given number left by one. Defaults to a 5-bit word.
- def left_shift(num, length=5):
- for x inrange(length):
- Returns the bit value of the 'pos'th bit in num. Defaults to little endian positioning. (MSB is position 1)
- def get_bit(num, pos, size=8, little_endian=True):
- return(num >>(size - pos)) & 1;
- return(num >> pos) & 1;
- ''
- Permutate the input's bits according to the given permutation array.
- The permutation array describes which input bits make up the output, and where they're positioned.
- def permutate(input_num, permutation, input_size=8):
- for p inrange(len(permutation)):
- ret_num += get_bit(input_num, permutation[p], input_size)<<(len(permutation) - 1 - p);
- return ret_num;
- ''
- ''
- return permutate(num,[3,5,2,7,4,10,1,9,8,6], input_size=10);
- def P8(num):
- return permutate(num,[6,3,7,4,8,5,10,9], input_size=10);
- def P4(num):
- return permutate(num,[2,6,3,1,4,8,5,7], input_size=8);
- def IP_inverse(num):
- return permutate(num,[4,1,3,5,7,2,8,6], input_size=8);
- def E_P(num):
- return permutate(num,[4,1,2,3,2,3,4,1], input_size=4);
- ''
- Performs the S0-box calculation on the 4-bit input 'num'.
- def S0(num):
- ,(3,2,1,0)
- ,(3,1,3,2))
- row =(get_bit(num,1,4)<<1) + get_bit(num,4,4);
- Performs the S1-box calculation on the 4-bit input 'num'.
- def S1(num):
- ,(2,0,1,3)
- ,(2,1,0,3))
- row =(get_bit(num,1,4)<<1) + get_bit(num,4,4);
- Performs the switching function. Switches the left four bits of 'num' with the right four bits.
- def SW(num):
- Generates the two subkeys K1 and K2 for use in the SDES algorithm.
- ''
- key = P10(key);
- key =( left_shift(key >>5)<<5) + ( left_shift(key & 0b11111))
- K1 = P8(key);
- key =( left_shift(key >>5)<<5) + ( left_shift(key & 0b11111))
- key =( left_shift(key >>5)<<5) + ( left_shift(key & 0b11111))
- K2 = P8(key);
- return(K1, K2);
- ''
- Performs one round of the Feistel cipher on the provided number.
- def f_K(num, subkey):
- right_input = num & 0b1111;
- data = E_P(right_input);
- data =(S0(data >>4)<<2) + S1(data & 0b1111);
- left_output = left_input ^ data;
- return(left_output <<4) + right_input;
- ''
- Encrypts the provided plaintext byte using the two subkeys, K1 and K2.
- def encrypt_byte(plaintext, K1, K2):
- round= f_K(SW(round), K2);
- ciphertext = IP_inverse(round);
- return ciphertext;
- ''
- Decrypts the provided ciphertext byte using the two subkeys, K1 and K2.
- def decrypt_byte(ciphertext, K1, K2):
- round= f_K(SW(round), K1);
- plaintext = IP_inverse(round);
- return plaintext;
- ''
- Encrypt or decrypt the input using SDES in electronic code book mode.
- input_data: The data to process. Expects a byte-array.
- mode: Whether to encrypt or decrypt the data. Defaults to encryption.
- def ecb(input_data, key, mode='encrypt'):
- for b in input_data:
- processed_byte = encrypt_byte(b, K1, K2);
- processed_byte = decrypt_byte(b, K1, K2);
- # Help text if not given enough arguments.
- print('***********************');
- print('***********************n');
- print(f'Usage:ntEncryption: python3 {sys.argv[0]} -e 'key_in_binary' 'plaintext_file' 'ciphertext_file'ntDecryption: python3 {sys.argv[0]} -d 'key_in_binary' 'ciphertext_file' 'plaintext_file');
- print(f'Example: python3 {sys.argv[0]} -e 1100101010 plaintext.txt cipertext.sdes');
- withopen(sys.argv[3],'rb')as input_file:
- chunk=bytearray(input_file.read(65535));# Process the file in 64kB chunks
- key =int(sys.argv[2],2);# Grab and decode the provided cipher key
- if(sys.argv[1]'-e'): # Encrypt the data
- elif(sys.argv[1]'-d'): # Decrypt the data
- output_file.write( ecb(chunk, key, mode='decrypt'));
- # Error case if an improper mode is provided.
- sys.stderr.write(f'Mode '{sys.argv[1]}' is not supported! Please use '-e' or '-d'!n');
- chunk= input_file.read(65535);
- main()