# Cipher with a key in Python | Assignment Expert

• 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 copyassignment.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)```

## 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: 