- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to encrypt a string using Vigenere cipher in Python
Suppose we have a lowercase alphabet string text, and have another string called key. We have to find a new string where every letter in text[i] is moved to the right side with offset key[i]. Here the offset represented by key[i]'s position in the alphabet (A=0, B=1 etc.) If the letter overflows, it gets wrapped around the other side.
So, if the input is like text = "code", key = "team", then the output will be "vsdq"
To solve this, we will follow these steps −
- cip := a new list
- start := ASCII of 'a'
- for each l from text and k from key, do
- shift := (ASCII of k) - start
- pos := start +((ASCII of l) - start + shift) mod 26
- insert character of pos at the end of cip
- join strings of cip and return
Let us see the following implementation to get better understanding −
class Solution: def solve(self, text, key): cip =  start = ord('a') for l, k in zip(text, key): shift = ord(k) - start pos = start + (ord(l) - start + shift) % 26 cip.append(chr(pos)) return ''.join([l for l in cip]) ob = Solution() text = "code" key = "team" print(ob.solve(text, key))
- Program to encrypt a string using Vertical Cipher in Python
- C++ Program to Encode a Message Using Playfair Cipher
- Encrypt and Decrypt a string in MySQL?
- C++ Program to Implement the Vigenere Cypher
- C++ Program to Decode a Message Encoded Using Playfair Cipher
- Atbash cipher in Python
- Caesar Cipher in Python
- C++ Program to Implement Affine Cipher
- Python Program to Reverse a String Using Recursion
- How to encrypt and decrypt data in Python
- Python Program to Reverse a String without using Recursion
- Program to make a bulb switcher using binary string using Python
- How to encrypt a CLOB datatype in JDBC?
- Program to find second largest digit in a string using Python