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()
  1. Sdes P10 And P8 Key Generation Download

ISE334/SE425: E-Commerce, Com., and Info. Security Recitation 3 Semester 2 5774 12 March 2014 Simpli ed DES 1 Introduction In this lab we will work through a simpli ed version of the DES algorithm. What is Simplified DES. Nintendo wii u master key generator. Developed 1996 as a teaching tool.Santa Clara University.Prof. Edward Schaefer.Takes an 8-bit block plaintext, a 10 –bit key and produces an 8-bit block of ciphertext.Decryption takes the 8-bit block of ciphertext, the same 10-bit key and produces the original 8. Symmetric key cryptography is useful if you want to encrypt files on your computer, and you intend to decrypt them yourself. It is less useful if you intend to send them to someone else to be decrypted, because in that case you have a 'key distribution problem': securely communicating the encryption key to your correspondent may not be much easier than securely communicating the original text.

SdesP10

Sdes P10 And P8 Key Generation Download

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. Nov 14, 2018  S-DES key generation.