Python 3.4.1 Keyword Encryption Challenge Help

TryteHD

Estimable
Hey, I need help with programming this certain bit of code. I literally just started using Python a few months ago and am a novice at this. This is the description of the challenge:

A more secure method of encryption would be to use a keyword. A keyword is the information needed in order to transform the plaintext into the encrypted message and is also used by the recipient to decrypt the message. For example, a keyword could be the letters GCSE. The keyword is repeated enough times to match the length of the plaintext message. The alphabet value (position of the letter in the alphabet) of each letter of the key phrase is added to the alphabet value of each letter of the plaintext message to generate the encrypted text.
For example, if we use the keyword GCSE then we make up the key phrase for COMPUTINGISFUN by repeating the letters within GCSE to make a 14 letter code, GCSEGCSEGCSEGC, where G is the 7th letter of the alphabet, C the 3rd, S the 19th and E the 5th.

This is the code I have already:

finalEncrypt = ""

keywordPosition = 0

for currentLetter in message:
alphabetPosition = ord(currentLetter) #alphabetPosition if I type a C will = 67
newAlphPos = (alphabetPosition) - 64
total = (keywordLetter) + (newAlphPos)
keywordLetter = ord(keyword) -64
key = keyword[0+keywordPosition]
if total > 26:
(total) -26

encryptedLetter = chr(total +64)
finalEncrypt = finalEncrypt + (encryptedLetter)

newAlphPos = (keywordPosition) +1

if keywordPosition == currentLetter:
keywordPosition = ord(message) -64

#for i in range (1,2):

print(finalEncrypt)

What am I doing wrong? Where am I missing code? Am I on the right lines? Help

Alabalcho

Judicious
I have no knowledge of Python, but looking at your code, I note two things:
- you don't use the value of "key" assigned in "key = keyword[0+keywordPosition]"
- you don't take into account that keyword' length could be less than your message' length
- the value of "keywordPosition" is used in strange way

- assign "keyLength" to length in chars of the keyword
- assign "msgPosition" for message
- loop thru all chars in the message, and increment "msgPosition" for each char; get msgCode by subtracting 32 (since your message could contain punctuation and digits as well);
- calculate "keyPosition" = modulo(msgPosition, keyLength)
- obtain the character under keyPosition in the keyword, subtract 32, and store it as keyCode
- calculate modulo(msgCode + keyCode, 96), add 32, this is your encoded character from the message
- append that char to the encrypted message

das_stig

Distinguished
found this, not a python expert, but you should be able to rewrite to use a master key as part of the function call ..

from Crypto.Cipher import AES
import base64

MASTER_KEY="Some-long-base-key-to-use-as-encryption-key"

def encrypt_val(clear_text):
enc_secret = AES.new(MASTER_KEY[:32])
tag_string = (str(clear_text) + (AES.block_size - len(str(clear_text)) % AES.block_size) * "\0")
cipher_text = base64.b64encode(enc_secret.encrypt(tag_string))
return cipher_text

--

def decrypt_val(cipher_text):
dec_secret = AES.new(MASTER_KEY[:32])
raw_decrypted = dec_secret.decrypt(base64.b64decode(cipher_text))
clear_val = raw_decrypted.rstrip("\0")
return clear_val

TryteHD

Estimable

Thanks but I need to use the kind of code I've already shown you. I don't understand any of that... I was thinking more on "how could I develop this bit of code to work with what I want it to do?" Can you help me out there?

Alabalcho

Judicious
I have no knowledge of Python, but looking at your code, I note two things:
- you don't use the value of "key" assigned in "key = keyword[0+keywordPosition]"
- you don't take into account that keyword' length could be less than your message' length
- the value of "keywordPosition" is used in strange way

- assign "keyLength" to length in chars of the keyword
- assign "msgPosition" for message
- loop thru all chars in the message, and increment "msgPosition" for each char; get msgCode by subtracting 32 (since your message could contain punctuation and digits as well);
- calculate "keyPosition" = modulo(msgPosition, keyLength)
- obtain the character under keyPosition in the keyword, subtract 32, and store it as keyCode
- calculate modulo(msgCode + keyCode, 96), add 32, this is your encoded character from the message
- append that char to the encrypted message

Apps General Discussion 5
Apps General Discussion 5
Apps General Discussion 4
Apps General Discussion 4
Apps General Discussion 1
Apps General Discussion 1
Apps General Discussion 1
Apps General Discussion 1
Apps General Discussion 1
G Apps General Discussion 5
Apps General Discussion 11
Apps General Discussion 6
Apps General Discussion 19
Apps General Discussion 1
Apps General Discussion 2
Apps General Discussion 4
Apps General Discussion 1
Apps General Discussion 6
Apps General Discussion 3
Apps General Discussion 3