Python 3.0 , encoding="utf-8" for encryption

In summary: For encrypting Unicode, you shouldn't operate directly on the Unicode anyway. You should encode it into a byte string (a Python 3 "bytes" object) using your chosen encoding (utf-8 in this case), then encrypt the byte string (where now you can combine the byte and the key modulo 256 without a problem since all ordinals will be 255 or less). When you want to decrypt, you decrypt the byte string, then decode it into Unicode using your chosen encoding.
  • #1
NihalRi
134
12
I had recently upgraded my version of python from 2 to 3. I had a program that encrypted a text file by converting a character to its Unicode value, altering it and then changing it back to a character using the ord() and chr() methods. This does not seem to work with python 3 and I was wondering how I could use encoding="utf-8" to make it work without altering too much of my code. Below is what my code looks like.for x in range(0, len(content)):
output = chr(ord(content[x])+ord(key[x])%256)
crypt = crypt + output

new.write(crypt)Ther error I would get isTraceback (most recent call last):
File "C:\Users\\Desktop\programing\Python Files\Encryptor and Decryptor\encryption.py", line 26, in <module>
new.write(crypt)
File "C:\Users\\AppData\Local\Programs\Python\Python37-32\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x93' in position 0: character maps to <undefined>
 
Technology news on Phys.org
  • #3
NihalRi said:
I had a program that encrypted a text file by converting a character to its Unicode value, altering it and then changing it back to a character using the ord() and chr() methods.

Since you are combining the content and the key modulo 256, I strongly suspect that your Python 2 program was converting a character to its byte ordinal value, i.e., that you were operating on Python 2 strings, not Python 2 unicode objects. If you really want to operate on unicode objects (which I don't recommend for encryption/decryption--see below), you cannot assume that your ordinals (the integers you operate on before converting back to strings) will be 255 or less.

For encrypting Unicode, you shouldn't operate directly on the Unicode anyway. You should encode it into a byte string (a Python 3 "bytes" object) using your chosen encoding (utf-8 in this case), then encrypt the byte string (where now you can combine the byte and the key modulo 256 without a problem since all ordinals will be 255 or less). When you want to decrypt, you decrypt the byte string, then decode it into Unicode using your chosen encoding.
 
  • Like
Likes jedishrfu
  • #4
NihalRi said:
I had a program that encrypted a text file by converting a character to its Unicode value, altering it and then changing it back to a character using the ord() and chr() methods.

Since we're talking about encryption, I should probably also say that if you're trying to do encryption yourself for anything other than a private hobby program or a class assignment, you shouldn't. You should use available encryption libraries written by people who are experts at avoiding all of the many, many pitfalls that lurk in the world of encryption. For Python programs that need encryption, I have found the bcrypt Python library to be a good choice.
 
  • Like
Likes FactChecker and jedishrfu

1. What is Python 3.0?

Python 3.0, also known as Python 3000 or Py3k, is a major update to the popular programming language Python. It was released in 2008 and introduced several significant changes, including improvements to the language's syntax and standard library.

2. How is Python used for encryption?

Python is commonly used for encryption as it has built-in libraries for cryptographic functions. These libraries, such as hashlib and cryptography, allow for secure encryption and decryption of data using various algorithms.

3. What is the purpose of encoding="utf-8" in Python 3.0?

The encoding="utf-8" parameter in Python 3.0 is used to specify the character encoding of a string. This is important for encryption as it ensures that the data is properly encoded and can be decrypted without any errors or loss of information.

4. How does encoding="utf-8" affect encryption?

The use of encoding="utf-8" can affect encryption in a few ways. Firstly, it ensures that the data is properly encoded and can be decrypted without any errors. Additionally, it allows for the use of non-ASCII characters in the encryption process, making it more versatile and secure.

5. Are there any other encoding options besides utf-8 for encryption in Python 3.0?

Yes, there are other encoding options available for encryption in Python 3.0. These include common encodings such as ASCII, ISO-8859-1, and UTF-16. The choice of encoding will depend on the specific needs and requirements of the encryption process.

Similar threads

  • Programming and Computer Science
Replies
9
Views
8K
  • Programming and Computer Science
Replies
3
Views
886
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Computing and Technology
Replies
1
Views
3K
Back
Top