vigenere cipher - not adding correct values

2024/11/15 0:39:19

I want to get specific values from a for loop to add to another string to create a vigenere cipher.

here's the code.

userinput = input('enter message')
keyword = input('enter keyword')
new = ''
for a in keyword:pass
for i in (ord(x) for x in userinput): if 96 < i < 123: #lowercasenew += chr(97 + (i+ord(a)-97)#keeps all values in alphabet
print(new)

so the answer i want if i do 'abcd' as my message and 'ab' as my keyword the desired outcome is 'bddf' as 'a' + 'a' is 'b' and 'b' + 'b' = 'd' and etc. how would i change the code to match my desired outcome or will i have to change it completely and how would i go about doing so.

Answer

try this (you are missing the mod 26-part):

from itertools import cycleplaintext = input('enter message: ')
keyword = input('enter keyword: ')def chr_to_int(char):return 0 if char == 'z' else ord(char)-96
def int_to_chr(integer):return 'z' if integer == 0 else chr(integer+96)
def add_chars(a, b):return int_to_chr(( chr_to_int(a) + chr_to_int(b) ) % 26 )def vigenere(plaintext, keyword):keystream = cycle(keyword)ciphertext = ''for pln, key in zip(plaintext, keystream):ciphertext += add_chars(pln, key)return ciphertextciphertext = vigenere(plaintext, keyword)
print(ciphertext)

if you like list comprehensions, you can also write

def vigenere(plaintext, keyword):keystream = cycle(keyword)return ''.join(add_chars(pln, key)for pln, key in zip(plaintext, keystream))

UPDATE

updated according to the wish that a+a=b. note that z is in that case the neutral element for the addition (z+char=z).

https://en.xdnf.cn/q/119356.html

Related Q&A

Why isnt my output returning as expected?

So I wrote this code def diagsDownRight(M):n = len(M)m = [[] * (n - i - 1) + row + [] * i for i, row in enumerate(M)]return ([.join(col) for col in zip(*m)]), [.join(col[::-1]) for col in zip(*m)] def …

Django Stripe payment does not respond after clicking the Submit Payment button

I have an e-commerce application that Im working on. The app is currently hosted on Heroku free account. At the moment I can select a product, add it on the cart and can get up to the stripe form and t…

get file path using backslash (\) in windows in python [duplicate]

This question already has answers here:How can I put an actual backslash in a string literal (not use it for an escape sequence)?(4 answers)Closed 2 years ago.How to get result exactly the same format…

Printing progress bar on a console without the use of for -loop

I have a script written in python, where I have a statement:Process.open() //some parametersWhich executes a command and puts the output on the console ,where I do not know the time taken to execute t…

ModuleNotFoundError: No module named verovio

Hi there I would like to run my flask app in a container but I got stucked caused of a third party module. (I am using PyCharm)This is my docker file:FROM python:3-alpineMAINTAINER fooCOPY app /appWORK…

Python: TypeError: list object is not callable on global variable

I am currently in the process of programming a text-based adventure in Python as a learning exercise. I want "help" to be a global command, stored as values in a list, that can be called at (…

Python beautifulsoup how to get the line after href

I have this piece of html:<a href="http://francetv.fr/videos/alcaline_l_instant_,12163184.html" class="ss-titre">"Paris Combo" </a> <…

Scrapy empty output

I am trying to use Scrapy to extract data from page. But I get an empty output. What is the problem? spider: class Ratemds(scrapy.Spider):name = ratemdsallowed_domains = [ratemds.com]custom_settings =…

nested classes - how to use function from parent class?

If I have this situation:class Foo(object):def __init__(self):self.bar = Bar()def do_something(self):print doing somethingclass Bar(object):def __init(self):self.a = adef some_function(self):I want to …

CUDA Function Wont Execute For Loop on Python with Numba

Im trying to run a simple update loop of a simulation on the GPU. Basically there are a bunch of "creatures" represented by circles that in each update loop will move and then there will be a…