前提・実現したいこと
python でコードを引数/eで実行して鍵を16バイト(例えば0123456789123456)で文字列を暗号化することはできるのですが/dの引数で復号することができずに悩んでいます。
cipher_text = bytes(input("復号する文字列を入力してください。:"),'utf-8')
で入力を待ち受けてバイト型にすることができません。バイト型にする¥が¥¥になってしまい上手くいかないのです。誰かお解りになる方ご教授願います。
発生している問題・エラーメッセージ
TypeError: Object type <class 'str'> cannot be passed to C code
該当のソースコード
python
1#pip install pycryptodome 2# 3from Crypto.Cipher import AES 4import sys 5 6if(len(sys.argv) <= 1): 7 print("aes.py [/e] 暗号化モード\naes.py [/d] 復号化モード") 8 sys.exit() 9 10# Encryption 11 12if sys.argv[1] == "/e": 13 key = str(input("鍵を16byteで入力してください。:")) 14 key = key.encode('utf-8') 15 print("key=" + str(key)) 16 text = str(input("AESのCBCモードで暗号化する文字列を入力してください。:")) 17 byteLimit = len(text.encode()) 18 print('bytecount=' + str(byteLimit)) 19 20 if byteLimit < 16: 21 a = 16 - byteLimit 22 if a == 1: 23 padding = '#' 24 elif a == 2: 25 padding = '##' 26 elif a == 3: 27 padding = '###' 28 elif a == 4: 29 padding = '####' 30 elif a == 5: 31 padding = '#####' 32 elif a == 6: 33 padding = '######' 34 elif a == 7: 35 padding = '#######' 36 elif a == 8: 37 padding = '########' 38 elif a == 9: 39 padding = '#########' 40 elif a == 10: 41 padding = '##########' 42 elif a == 11: 43 padding = '###########' 44 elif a == 12: 45 padding = '############' 46 elif a == 13: 47 padding = '#############' 48 elif a == 14: 49 padding = '##############' 50 elif a == 15: 51 padding = '###############' 52 if byteLimit % 16 == 0: 53 padding = '' 54 elif byteLimit % 16 == 1: 55 padding = '###############' 56 elif byteLimit % 16 == 2: 57 padding = '##############' 58 elif byteLimit % 16 == 3: 59 padding = '#############' 60 elif byteLimit % 16 == 4: 61 padding = '############' 62 elif byteLimit % 16 == 5: 63 padding = '###########' 64 elif byteLimit % 16 == 6: 65 padding = '##########' 66 elif byteLimit % 16 == 7: 67 padding = '#########' 68 elif byteLimit % 16 == 8: 69 padding = '########' 70 elif byteLimit % 16 == 9: 71 padding = '#######' 72 elif byteLimit % 16 == 10: 73 padding = '######' 74 elif byteLimit % 16 == 11: 75 padding = '#####' 76 elif byteLimit % 16 == 12: 77 padding = '####' 78 elif byteLimit % 16 == 13: 79 padding = '###' 80 elif byteLimit % 16 == 14: 81 padding = '##' 82 elif byteLimit % 16 == 15: 83 padding = '#' 84 85 print('padding='+ padding) 86 text = (text + padding) 87 text = text.encode('utf-8') 88 #print(text) 89 90 encryption_suite = AES.new(key, AES.MODE_CBC, b'OnTheKumoProject') 91 cipher_text = encryption_suite.encrypt(text) 92 93 cipher_text = cipher_text.decode('utf-8') 94 95 print("cipher_text=" + str(cipher_text)) 96 sys.exit() 97 98# Decryption 99 100if sys.argv[1] == "/d": 101 102 key = str(input("鍵を16byteで入力してください。:")) 103 key = key.encode('utf-8') 104 print("key=" + str(key)) 105 106 cipher_text = bytes(input("復号する文字列を入力してください。:"),'utf-8') 107 cipher_text = cipher_text.decode('utf-8') 108 print(cipher_text) 109 110 decryption_suite = AES.new(key, AES.MODE_CBC, b'OnTheKumoProject') 111 plain_text = decryption_suite.decrypt(cipher_text) 112 plain_text = plain_text.decode('utf-8') 113 print("plain_text=" + plain_text)
試したこと
入力にb'\xe3\x83=J\xa3\x94\xe2AYUH\xc8\xc2\xd1\xcbh\x9c\xad\x95\xd9\x17G`\x15yB$"\xaa\xecQ\xa3\xab<6\xf8\xf8\x13\xda\x10y\x98\xc0\x87\xaf<\xd5\x0e\t\xb3G\xa8\x04\x84t\x85\xaa\xb2\xa52\xee\xec\x8a\xa8\xca\x1e\xc3\xb7=8\xe8QC\xe7\xbb\xf2\x0b\xd8\xcb\x97'
などを試しましたが上手くいきません。
補足情報(FW/ツールのバージョンなど)
pip install pycryptodome
でモジュールをインストールしました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。