コード例 #1
0
class DES:
    def __init__(self, key_size, blocklength):
        if key_size != 64:
            raise InvalidKeyLength("Keylength of DES must be 64 bits!")

        if blocklength % 64 != 0:
            raise InvalidCipherBlockLength(
                "Blocklength of DES should be multiplier of 64")

        self._key = None
        self._blocklength = blocklength

    def set_key(self, key):
        string_key = bits_to_bytes(key)

        self._key = DesKey(string_key)

        if not self._key.is_single():
            raise WrongConfiguration("3DES has been configured")

    def encrypt(self, message):
        byte_message = bits_to_bytes(message)
        encrypted_bytes = None
        for _ in range(round(self._blocklength / 64)):
            encrypted_bytes = self._key.encrypt(byte_message)
        return bytes_to_bits(encrypted_bytes)

    def decrypt(self, message):
        byte_message = bits_to_bytes(message)
        decrypted_bytes = None
        for _ in range(round(self._blocklength / 64)):
            decrypted_bytes = self._key.decrypt(byte_message)

        return bytes_to_bits(decrypted_bytes)
コード例 #2
0
class DES_Crypt():
    def __init__(self, key: str, text: str):
        """
        :key: private key, will be encoded into bytes
        :text: plaintext, will be encoded into bytes
        """
        self.key = DesKey(bytes(key.encode('utf-8')))
        self.text = bytes(text.encode('utf-8'))

    @MyTools.info("T encrypt")
    def encrypt(self):
        self.encrypted = self.key.encrypt(self.text, padding=True)
        return self.encrypted

    @MyTools.info("T decrypt")
    def decrypt(self):
        self.decrypted = self.key.decrypt(self.encrypted, padding=True)
        return self.decrypted

    def is_single(self):
        return self.key.is_single()
コード例 #3
0
ファイル: des_key.py プロジェクト: troyygan/Crypto
#!/usr/bin/python

from des import DesKey

key0 = DesKey("hsueo se")  # FOR DES
key1 = DesKey(
    "a key for TRIPLE")  # for 3DES, same as "a key for TRIPLEa key for"
key2 = DesKey("a 24-byte key for TRIPLE")  # for 3DES
key3 = DesKey("12345678REAL_KEY")  # for DES, same as "REAL_KEY"

key0.is_single()
key1.is_triple()
key2.is_single()
key3.is_triple()

enc = key0.encrypt("any long message")

print enc