0% found this document useful (0 votes)
20 views

Python 1

The document describes a Python function that decrypts a Caesar cipher by shifting letters by a given number and transposes the first six letters to the end. The function reads an encrypted string from an S3 bucket, decrypts it with a shift of 12, and transposes the first six letters to output the decrypted string.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python 1

The document describes a Python function that decrypts a Caesar cipher by shifting letters by a given number and transposes the first six letters to the end. The function reads an encrypted string from an S3 bucket, decrypts it with a shift of 12, and transposes the first six letters to output the decrypted string.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import json

import os
import boto3
import string

def decrypt(text,s):
result = ""
# transverse the plain text
for i in range(len(text)):
char = text[i]
# Encrypt uppercase characters in plain text

if (char.isupper()):
result += chr((ord(char) - s-65) % 26 + 65)
# Encrypt lowercase characters in plain text
else:
result += chr((ord(char) - s-97) % 26 + 97)
return result

def handler(event, context):


## DO NOT EDIT THE SECTION BELOW HERE ##
#Import halfDecoded string
s3 = boto3.resource('s3')
obj = s3.Object(os.environ['bucketName'], 'halfDecoded.txt')
inputString = obj.get()['Body'].read().decode('utf-8')

## ADD NEW CODE DIRECTLY BELOW HERE ##


# *Your halfDecoded input string is already stored in 'inputString' variable
# *Store the result string from your code into the 'outputString' variable

#TO BE DONE - Decipher the Caesar Cipher


print('inputString :: ',inputString)
outputString = decrypt(inputString,12)

#TO BE DONE - Transpose all letters 6 left (wrapping around)


slice1 = outputString[0:6]
outputString = outputString.replace(outputString[0:6], '')+outputString[0:6]

## NEW CODE GOES ABOVE HERE ##


## DO NOT EDIT THE SECTION BELOW THIS LINE ##
#Export outputString
try:
f = open("/tmp/output.txt", "x")
f.close()
f = open("/tmp/output.txt", "a")
f.write(outputString)
f.close()
except:
f = open("/tmp/output.txt", "w")
f.write(outputString)
f.close()

s3 = boto3.client('s3')
s3.upload_file('/tmp/output.txt', os.environ['bucketName'], 'final.txt')

#Return for logs and final answer!


return {
'input' : inputString,
'output': outputString
}

You might also like