Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

1.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)
2.Apply this for aes/s-aes python code for encrypting and decrypting
app/algorithms/algo_name.py
# imports
# the function should get the parameters and return the result
def algo_function(param1, param2):
# Your algorithm implementation here
result = param1+ param2
# the return should be a dict {'ret_name':value, 'e':100}
return result
# please document your code(a small note on important parts)
# you can use whatever you want in this file just make sure
# that there is a function that i can pass params and get back a dict of results

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions