実現したいこと
kivyで作るpythonアプリにおいて、サーバに送信データを同じディレクトリ内にある.keyファイルを用いて暗号化し、サーバ側の秘密鍵で復号するということがやりたいです。
しかし、python側で公開鍵で暗号化する際にエラーが出てしまって困っています。
いろいろな記事を見たのですが、python側で公開鍵を作成してそのまま使う例しか載っておらず、.keyファイルを使った暗号化方法が見つかりません
pycryptodoモジュール
該当のソースコード(pycryptodoモジュール使用)
python
1#公開鍵での暗号に使用 2from Crypto.PublicKey import RSA 3from Crypto.Cipher import PKCS1_OAEP 4#省略 5f = open('id.txt', 'r', encoding='UTF-8') 6 message = "Hello Python World!" 7 with open('public.key', 'rb') as f: 8 public_key = f.read() #.keyファイルからの読み取り 9 cipher_rsa = PKCS1_OAEP.new(public_key) #公開鍵の読み込み 10 ciphertext = cipher_rsa.encrypt(message.encode()) 11 self.ids['message'].text = str(ciphertext) #出力 12
発生したエラーコード(pycryptodoモジュール使用)
ciphertext = cipher_rsa.encrypt(message.encode()) File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\site-packages\Crypto\Cipher\PKCS1_OAEP.py", line 107, in encrypt modBits = Crypto.Util.number.size(self._key.n) AttributeError: 'bytes' object has no attribute 'n'
該当のソースコード(cryptographyモジュール使用)
python
1from cryptography.fernet import Fernet 2#省略 3 message = "Hello Python World!" 4 with open('public.key', 'rb') as f: 5 public_key = f.read() #.keyファイルからの読み取り 6 #cipher_rsa = PKCS1_OAEP.new(public_key) 7 fer = Fernet(public_key) #公開鍵の読み込み 8 ciphertext = fer.encrypt(message.encode()) 9 self.ids['message'].text = str(ciphertext) #出力 10
発生したエラーコード(cryptographyモジュール使用)
fer = Fernet(public_key) File "C:\Users\Owner\AppData\Local\Programs\Python\Python310\lib\site-packages\cryptography\fernet.py", line 35, in __init__ raise ValueError( ValueError: Fernet key must be 32 url-safe base64-encoded bytes.

回答1件
あなたの回答
tips
プレビュー