- 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
- Composite Number in Python
- Greatest Among Four Numbers in Python
- Reverse the sentence in Python
- Denominations in Python
- Min and max values in an array in JavaScript
- Keyboard events in JavaScript
- Reaching Ground Floor in Python
- Number of Moves in Python
- Starks Adventure in Python
- Neutralization in Python | Assignment Expert
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- First Place in Python
- Triples with Properties in Python
- Nested list Indexing Python
- Team Points in Python
- Two words combination in Python
- ID Card in Python
- Cipher with a key in Python | Assignment Expert
- Multiple of 5 in Python
- Sandglass Star in Python
- Multiple of 3 in Python | Assignment Expert
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Sum of List Elements in Python
- All possible subsets in Python
- Names and Nicknames in Python