Sdes P10 And P8 Key Generation

  1. Rotates the given number left by one. Defaults to a 5-bit word.
  2. def left_shift(num, length=5):
  3. for x inrange(length):
  4. Returns the bit value of the 'pos'th bit in num. Defaults to little endian positioning. (MSB is position 1)
  5. def get_bit(num, pos, size=8, little_endian=True):
  6. return(num >>(size - pos)) & 1;
  7. return(num >> pos) & 1;
  8. ''
  9. Permutate the input's bits according to the given permutation array.
  10. The permutation array describes which input bits make up the output, and where they're positioned.
  11. def permutate(input_num, permutation, input_size=8):
  12. for p inrange(len(permutation)):
  13. ret_num += get_bit(input_num, permutation[p], input_size)<<(len(permutation) - 1 - p);
  14. return ret_num;
  15. ''
  16. ''
  17. return permutate(num,[3,5,2,7,4,10,1,9,8,6], input_size=10);
  18. def P8(num):
  19. return permutate(num,[6,3,7,4,8,5,10,9], input_size=10);
  20. def P4(num):
  21. return permutate(num,[2,6,3,1,4,8,5,7], input_size=8);
  22. def IP_inverse(num):
  23. return permutate(num,[4,1,3,5,7,2,8,6], input_size=8);
  24. def E_P(num):
  25. return permutate(num,[4,1,2,3,2,3,4,1], input_size=4);
  26. ''
  27. Performs the S0-box calculation on the 4-bit input 'num'.
  28. def S0(num):
  29. ,(3,2,1,0)
  30. ,(3,1,3,2))
  31. row =(get_bit(num,1,4)<<1) + get_bit(num,4,4);
  32. Performs the S1-box calculation on the 4-bit input 'num'.
  33. def S1(num):
  34. ,(2,0,1,3)
  35. ,(2,1,0,3))
  36. row =(get_bit(num,1,4)<<1) + get_bit(num,4,4);
  37. Performs the switching function. Switches the left four bits of 'num' with the right four bits.
  38. def SW(num):
  39. Generates the two subkeys K1 and K2 for use in the SDES algorithm.
  40. ''
  41. key = P10(key);
  42. key =( left_shift(key >>5)<<5) + ( left_shift(key & 0b11111))
  43. K1 = P8(key);
  44. key =( left_shift(key >>5)<<5) + ( left_shift(key & 0b11111))
  45. key =( left_shift(key >>5)<<5) + ( left_shift(key & 0b11111))
  46. K2 = P8(key);
  47. return(K1, K2);
  48. ''
  49. Performs one round of the Feistel cipher on the provided number.
  50. def f_K(num, subkey):
  51. right_input = num & 0b1111;
  52. data = E_P(right_input);
  53. data =(S0(data >>4)<<2) + S1(data & 0b1111);
  54. left_output = left_input ^ data;
  55. return(left_output <<4) + right_input;
  56. ''
  57. Encrypts the provided plaintext byte using the two subkeys, K1 and K2.
  58. def encrypt_byte(plaintext, K1, K2):
  59. round= f_K(SW(round), K2);
  60. ciphertext = IP_inverse(round);
  61. return ciphertext;
  62. ''
  63. Decrypts the provided ciphertext byte using the two subkeys, K1 and K2.
  64. def decrypt_byte(ciphertext, K1, K2):
  65. round= f_K(SW(round), K1);
  66. plaintext = IP_inverse(round);
  67. return plaintext;
  68. ''
  69. Encrypt or decrypt the input using SDES in electronic code book mode.
  70. input_data: The data to process. Expects a byte-array.
  71. mode: Whether to encrypt or decrypt the data. Defaults to encryption.
  72. def ecb(input_data, key, mode='encrypt'):
  73. for b in input_data:
  74. processed_byte = encrypt_byte(b, K1, K2);
  75. processed_byte = decrypt_byte(b, K1, K2);
  76. # Help text if not given enough arguments.
  77. print('***********************');
  78. print('***********************n');
  79. 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');
  80. print(f'Example: python3 {sys.argv[0]} -e 1100101010 plaintext.txt cipertext.sdes');
  81. withopen(sys.argv[3],'rb')as input_file:
  82. chunk=bytearray(input_file.read(65535));# Process the file in 64kB chunks
  83. key =int(sys.argv[2],2);# Grab and decode the provided cipher key
  84. if(sys.argv[1]'-e'): # Encrypt the data
  85. elif(sys.argv[1]'-d'): # Decrypt the data
  86. output_file.write( ecb(chunk, key, mode='decrypt'));
  87. # Error case if an improper mode is provided.
  88. sys.stderr.write(f'Mode '{sys.argv[1]}' is not supported! Please use '-e' or '-d'!n');
  89. chunk= input_file.read(65535);
  90. main()
An Algorithm to implement Simplified-DES encryption
Sdes P10 And P8 Key Generation
Simplified-DES.cpp

It is well written overall, but remember that string += thing is very slow when used in loops and you should join a generator expression for efficiency. Share improve this answer answered Oct 19 '15 at 20:33. Jul 05, 2010  In this case, the key is first subjected to a permutation (P10). Then a shift operation is performed. The output of the shift operation then passes through a permutation function that produces an 8-bit output (P8) for the first subkey (K1). Sep 05, 2014  Key Generator. A 10-bit key shared between sender and receiver. From this key, two 8-bit subkeys are produced for use in particular stages of the encryption and decryption algorithm. First, permute the key in the following fashion. Let the 10-bit key be designated as (k1, k2, k3, k4, k5, k6, k7, k8, k9, k10). Then the permutation P10 is defined as. Feb 19, 2017  K1 = P8 (Shift (P10 (Key))) K2 = P8 (Shift (shift (P10 (Key)))) Decryption can be shown as. Plaintext = IP-1 (f K1 (SW (f k2 (IP (ciphertext))))) 1. S-DES key generation. S-DES depends on the use of a 10-bit key shared between sender and receiver. From this key, two 8-bit subkeys are produced for use in particular stages of the encryption and decryption algorithm. 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. It is well written overall, but remember that string += thing is very slow when used in loops and you should join a generator expression for efficiency. Share improve this answer answered Oct 19 '15 at 20:33. 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.

Jun 19, 2018  Buy Generac 0G66240KEY Direct. Check the Generac Replacement Key For Air-Cooled Standby Generators (2008+) ratings before checking out. The flywheel key might have sheared in half. The flywheel key is a small metal piece which fits into the crankshaft and engages with the flywheel. If the generator engine stops suddenly, the flywheel. Lost key to my generic generator online. Jun 20, 2019  Generac - Key for Lock 0G6624-0G66240KEY. Lost my generator key and this was the fastest way to get another. Worked great too. Comment Report abuse.

Ssh

// 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;
}

Sdes P10 And P8 Key Generation 2

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment