質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

暗号化

ネットワークを通じてデジタルデータをやり取りする際に、第三者に解読されることのないよう、アルゴリズムを用いてデータを変換すること。

Q&A

解決済

1回答

1652閲覧

Python2系で書かれたコードを3.7に変換したい

NextToYou

総合スコア28

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

暗号化

ネットワークを通じてデジタルデータをやり取りする際に、第三者に解読されることのないよう、アルゴリズムを用いてデータを変換すること。

0グッド

1クリップ

投稿2019/09/18 01:09

前提・実現したいこと

Python2系で書かれたコードを3.7に変換したい

発生している問題・エラーメッセージ

Traceback (most recent call last): File "C:\Users(UserNane)\Desktop\新しいテキスト ドキュメント.txt", line 75, in <module> auth_key = encrypt(client_id, user_id, shared_key) File "C:\Users(UserNane)\Desktop\新しいテキスト ドキュメント.txt", line 39, in encrypt encrypted = cipher.encrypt(plain_text.encode("UTF-8")) File "C:\Users(UserNane)\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 183, in encrypt raise ValueError("Data must be padded to %d byte boundary in CBC mode" % self.block_size) ValueError: Data must be padded to 8 byte boundary in CBC mode

該当のソースコード

Python

1#!/usr/bin/env python 2# -*- coding: utf-8 -+- 3 4""" 5認証用文字列生成サンプル 6""" 7 8import base64 9from builtins import Exception 10import json 11import random 12import string 13from struct import pack 14import time 15import urllib.request, urllib.error, urllib.parse 16 17from Crypto.Cipher import Blowfish 18 19BLOCK_SIZE = 8 20 21 22def encrypt(client_id, user_id, shared_key): 23 ''' 暗号文字列を生成します. ''' 24 #iv = ''.join(random.choice(string.letters) for i in range(BLOCK_SIZE)) 25 iv = ''.join(random.choices(string.ascii_letters, k = BLOCK_SIZE)) 26 # 暗号化前の平文文字列 27 # 書式:[clientid]:[アクセス日時]:[ユーザーID]:[IV値]: 28 plain_text = "{}:{}:{}:{}:".format( 29 client_id, int(time.time()), user_id, iv) 30 # 暗号文字列サイズが8の倍数になるように調整 31 plen = BLOCK_SIZE - len(plain_text) % BLOCK_SIZE 32 padding = [plen] * plen 33 padding = pack('b' * plen, *padding) 34 #plain_text += padding 35 plain_text += str(padding) 36 37 #cipher = Blowfish.new(shared_key, Blowfish.MODE_CBC, iv) 38 cipher = Blowfish.new(shared_key.encode('utf-8'), Blowfish.MODE_CBC, iv.encode('utf-8')) 39 encrypted = cipher.encrypt(plain_text.encode("UTF-8")) 40 # IV値(8文字) + 暗号文字列をbase64エンコード 41 return base64.urlsafe_b64encode(iv+encrypted) 42 43 44def create_session_id(client_id, auth_key): 45 46 #todo Python2系から変換する 47 48if __name__ == '__main__': 49 import sys 50 51 # クライアントID 52 #client_id = sys.argv[1] 53 client_id = "kkcdev" 54 # ユーザーID(任意) 55 #user_id = sys.argv[2] 56 user_id = "sss" 57 # 共有キー 58 #shared_key = sys.argv[3] 59 shared_key = "PDSKKCDEV" 60 61 auth_key = encrypt(client_id, user_id, shared_key) 62 session_id = create_session_id(client_id, auth_key) 63 print("Session ID: {}".format(session_id)) 64

試したこと

encrypted = cipher.encrypt(plain_text)
だと

Traceback (most recent call last): File "C:\Users(UserNane)\Desktop\新しいテキスト ドキュメント.txt", line 75, in <module> auth_key = encrypt(client_id, user_id, shared_key) File "C:\Users(UserNane)\Desktop\新しいテキスト ドキュメント.txt", line 39, in encrypt encrypted = cipher.encrypt(plain_text) File "C:\Users(UserNane)\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 178, in encrypt c_uint8_ptr(plaintext), File "C:\Users(UserNane)\AppData\Local\Programs\Python\Python37\lib\site-packages\Crypto\Util\_raw_api.py", line 145, in c_uint8_ptr raise TypeError("Object type %s cannot be passed to C code" % type(data)) TypeError: Object type <class 'str'> cannot be passed to C code

となったので以下に修正
encrypted = cipher.encrypt(plain_text.encode("UTF-8"))

補足情報(FW/ツールのバージョンなど)

IDLE (Python 3.7 64-bit)
pycryptodome 3.9.0
Python 3.7

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

paddingはstr型のまま足しこむのがポイントかと思います。

Python

1def encrypt(client_id, user_id, shared_key): 2 ''' 暗号文字列を生成します. ''' 3 iv = ''.join(random.choices(string.ascii_letters, k = BLOCK_SIZE)) 4 5 # 暗号化前の平文文字列 6 # 書式:[clientid]:[アクセス日時]:[ユーザーID]:[IV値]: 7 plain_text = "{}:{}:{}:{}:".format( 8 client_id, int(time.time()), user_id, iv) 9 10 # 暗号文字列サイズが8の倍数になるように調整 11 plen = BLOCK_SIZE - len(plain_text) % BLOCK_SIZE 12 13 # 文字列のまま足しこむ 14 padding = str(plen) * plen 15 plain_text += padding 16 17 iv = iv.encode('utf-8') # byte型に 18 19 cipher = Blowfish.new(shared_key.encode('utf-8'), Blowfish.MODE_CBC, iv) 20 encrypted = cipher.encrypt(plain_text.encode("UTF-8")) 21 # IV値(8文字) + 暗号文字列をbase64エンコード 22 return base64.urlsafe_b64encode(iv+encrypted)

投稿2019/09/18 11:55

can110

総合スコア38233

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

NextToYou

2019/09/19 01:53

ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問