0

So I've just started experimenting with Pycrypto and wanted to encrypt and decrypt a message, but this code I put together produced some errors.

Here they are:

enc_data = public_key.encrypt 

TypeError: unsupported operand type(s) for pow(): 'str', 'int','int'

ciphertext = cipher.encrypt('Bob')

Traceback (most recent call last):

line 22, in ciphertext = cipher.encrypt('Bob')

File "C:\Anaconda3\lib\site-packages\Crypto\Cipher\PKCS1_OAEP.py", line 50, in encrypt
db = lHash + ps + bchr(0x01) + message

TypeError: can't concat bytes to str

The code:

import Crypto
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto import Random

random_generator = Random.new().read
key = RSA.generate(1024, random_generator)

public_key = key.publickey()

enc_data = public_key.encrypt('Bob', 32)

cipher = PKCS1_OAEP.new(key)
ciphertext = cipher.encrypt('Bob')

The two commands which are meant to encrypt 'Bob' produce these errors, and yes I now that the first way isn't very secure.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
AtomiCookiez
  • 61
  • 2
  • 10
  • If the object is just to encrypt and decrypt data and there is no need for a public/private key pair use symmetric encryption such as AES. Even when using asymmetric encryption the data is usually encrypted with symmetric encryption. Asymmetric encryptions limited to the key size and is **much slower** than symmetric encryption. – zaph Apr 22 '16 at 20:59

1 Answers1

2

In Python 3 there is a difference between strings and bytes. PyCrypto works on bytes, so you need to give it bytes, but "Bob" is a string. You can convert a string a to bytes with a.encode(), which uses a default encoding. If you have another encoding in mind, then you need to specify it.

You can also mark a literal string as bytes by prefixing it with a b. Example: b"Bob".

Artjom B.
  • 61,146
  • 24
  • 125
  • 222