Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fix the python code for aes / s - aes. s _ box = [ [ 0 x 0 9 , 0 x 0 4

Fix the python code for aes/s-aes. s_box =[
[0x09,0x04,0x0A,0x0D],
[0x0D,0x09,0x04,0x0A],
[0x0A,0x0D,0x09,0x04],
[0x04,0x0A,0x0D,0x09]
]
# Fixed 8-bit key for S-AES
key =[0x03,0x05,0x06,0x09]
def shift_rows(state):
# Swap second and third rows
state[1], state[2]= state[2], state[1]
# Swap third and fourth rows
state[2], state[3]= state[3], state[2]
return state
def mix_columns(state):
# Multiply each column by 2
for i in range(4):
state[i]=((state[i]<<1)^(state[i] & 0x80 and 0x1B)) & 0xFF
# XOR each column with the previous column
state[1]^= state[0]
state[2]^= state[1]
state[3]^= state[2]
return state
def s_aes_encrypt(plaintext):
# Convert plaintext characters to ASCII values
plaintext_ascii =[ord(char) for char in plaintext]
# Pad the plaintext if its length is not a multiple of 4
while len(plaintext_ascii)%4!=0:
plaintext_ascii.append(0)
encrypted_blocks =[]
for i in range(0, len(plaintext_ascii),4):
block = plaintext_ascii[i:i+4]
if len(block)<4:
block.extend([0]*(4- len(block)))
# Initial round key addition
state =[byte ^ key[idx] for idx, byte in enumerate(block)]
# S-AES rounds
for _ in range(3):
# Substitute each byte with its corresponding value in the s_box
state =[s_box[byte >>4][byte & 0x0F] for byte in state]
state = shift_rows(state)
state = mix_columns(state)
# Add the round key
state =[byte ^ key[idx +4] for idx, byte in enumerate(state)]
# Final round without MixColumns
state =[s_box[byte >>4][byte & 0x0F] for byte in state]
state = shift_rows(state)
# Add the final round key
state =[byte ^ key[idx +16] for idx, byte in enumerate(state)]
encrypted_blocks.extend(state)
return encrypted_blocks
def s_aes_decrypt(ciphertext):
# Decryption in S-AES is similar to encryption for this simplified version
return s_aes_encrypt(ciphertext)
# Input plaintext as a string
plaintext = input("Enter plaintext: ")
# Encrypt plaintext
encrypted = s_aes_encrypt(plaintext)
print("Encrypted:",''.join(f'{byte:02X}' for byte in encrypted))
# Decrypt ciphertext
decrypted = s_aes_decrypt(encrypted)
# Remove padded zeros and convert ASCII values to characters for the decrypted result
decrypted_text =''.join(chr(byte) for byte in decrypted if byte !=0)
print("Decrypted:", decrypted_text)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Current Trends In Database Technology Edbt 2004 Workshops Edbt 2004 Workshops Phd Datax Pim P2panddb And Clustweb Heraklion Crete Greece March 2004 Revised Selected Papers Lncs 3268

Authors: Wolfgang Lindner ,Marco Mesiti ,Can Turker ,Yannis Tzitzikas ,Athena Vakali

2005th Edition

3540233059, 978-3540233053

More Books

Students also viewed these Databases questions

Question

Demonstrate three aspects of assessing group performance?

Answered: 1 week ago