- Cipher means an algorithm that can be used to encrypt or decrypt messages or sentences.
- Encrypt means converting a message to a secret form that can not be understood by an unknown person.
- Decrypt means converting an encrypted message back to its normal form.
- Key is a text which is used with the message we want to encrypt/decrypt.
A cipher(algorithm) can be of 2 types based on symmetricity:
- Symmetric key Cipher: These types of ciphers require the same key for encryption and decryption.
- message => Encipher(message, key) => ciphered text => Decipher(ciphered text, key) => original message
- Asymmetric Key Cipher: These types of ciphers use different types of keys for encryption and decryption.
- message => Encipher(message, key1) => ciphered text => Decipher(ciphered text, key2) => original message
Code to encrypt using a cipher with a key in Python:
# importing AES from Crypto.Cipher import AES # encryption key key = b'C&F)H@McQfTjWnZr' # create new instance of cipher cipher = AES.new(key, AES.MODE_EAX) # data to be encrypted data = "Welcome to violet-cat-415996.hostingersite.com!".encode() # nonce is a random value generated each time we instantiate the cipher using new() nonce = cipher.nonce # encrypt the data ciphertext = cipher.encrypt(data) # print the encrypted data print("Cipher text:", ciphertext)
Output:
Code to decrypt using a cipher with a key in Python:
# generate new instance with the key and nonce same as encryption cipher cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) # decrypt the data plaintext = cipher.decrypt(ciphertext) print("Plain text:", plaintext)
Output:
Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- Team Points in Python
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Split the sentence in Python
- String Slicing in JavaScript
- First and Last Digits in Python | Assignment Expert
- List Indexing in Python
- Date Format in Python | Assignment Expert
- New Year Countdown in Python
- Add Two Polynomials in Python
- Sum of even numbers in Python | Assignment Expert
- Evens and Odds in Python
- A Game of Letters in Python
- Sum of non-primes in Python
- Smallest Missing Number in Python
- String Rotation in Python
- Secret Message in Python
- Word Mix in Python
- Single Digit Number in Python
- Shift Numbers in Python | Assignment Expert
- Weekend in Python
- Shift Numbers in Python | Assignment Expert
- Temperature Conversion in Python
- Special Characters in Python
- Sum of Prime Numbers in the Input in Python