from Crypto.Cipher import DES3

from Crypto.Hash import SHA256 as SHA


class myDES():

    def __init__(self, keytext, ivtext):

        hash = SHA.new()

        hash.update(keytext.encode('utf-8'))

        key = hash.digest()

        self.key = key[:24]


        hash.update(ivtext.encode('utf-8'))

        iv = hash.digest()

        self.iv = iv[:8]


    def enc1(self, plaintext):

        des3 = DES3.new(self.key, DES3.MODE_CBC, self.iv)

        encmsg = des3.encrypt(plaintext)

        return encmsg


    def dec1(self, ciphertext):

        des3 = DES3.new(self.key, DES3.MODE_CBC, self.iv)

        decmsg = des.decrypt(ciphertext)

        return decmsg


def main():

    keytext = 'samejang'

    ivtext = '1234'

    msg = 'python35'


    myCipher = myDES(keytext, ivtext)

    ciphered = myCipher.enc1(msg)

    deciphered = myCipher.dec1(ciphered)

    print('origainal:\t&s' %msg)

    print('ciphered:\t%s' %ciphered)

    print('deciphered:\t%s' %deciphered)


if __name__ == '__main__':

    main()


책 따라한 코드

Traceback (most recent call last):
  File "C:\Users\이얏호우우\AppData\Local\Programs\Python\Python38\8.py", line 38, in <module>
    main()
  File "C:\Users\이얏호우우\AppData\Local\Programs\Python\Python38\8.py", line 31, in main
    ciphered = myCipher.enc1(msg)
  File "C:\Users\이얏호우우\AppData\Local\Programs\Python\Python38\8.py", line 17, in enc1
    encmsg = des3.encrypt(plaintext)
  File "C:\Users\이얏호우우\AppData\Local\Programs\Python\Python38\lib\site-packages\Crypto\Cipher\_mode_cbc.py", line 178, in encrypt
    c_uint8_ptr(plaintext),
  File "C:\Users\이얏호우우\AppData\Local\Programs\Python\Python38\lib\site-packages\Crypto\Util\_raw_api.py", line 243, 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

에러코드