def RijndaelTestVec(i, key, pt, ct):
            """ Run single AES test vector with any legal blockSize
                and any legal key size. """
            bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex(ct)
            kSize = len(bkey)
            bSize = len(cipherText) # set block size to length of block
            alg = Rijndael(bkey, keySize=kSize, blockSize=bSize, padding=noPadding())

            self.assertEqual( alg.encrypt(plainText),  cipherText )
            self.assertEqual( alg.decrypt(cipherText), plainText )
Exemplo n.º 2
0
class PRN_Rijndael:
    """ A Psudeo Random Number Generator based on Rijndael_256k_256b
        The algorithm is based on Section 13.4 of:
        "AES Proposal: Rijndael", Joan Daemen, Vincent Rijmen
    """
    def __init__(self, seed=defaultSeed):
        self.__algorithm = Rijndael(padding=noPadding(),
                                    keySize=32,
                                    blockSize=32)
        self.reset()
        self.reseed(seed)

    def reset(self):
        self.__algorithm.setKey(self.__algorithm.keySize *
                                chr(0))  # set key to all zeros
        self.__state = self.__algorithm.blockSize * chr(
            0)  # a single block of zeros

    def reseed(self, seed):
        while len(seed) > 0:
            if len(seed) < self.__algorithm.blockSize:
                block = seed + (self.__algorithm.blockSize -
                                len(seed)) * chr(0)
                seed = ''
            else:
                block = seed[:self.__algorithm.blockSize]
                seed = seed[self.__algorithm.blockSize:]
            self.__algorithm.setKey(self.__algorithm.encrypt(block))

    def getBytes(self, numBytes):
        """ Return a psuedo random byte string of length numBytes """
        bytes = ''
        while len(bytes) < numBytes:
            bytes = bytes + self.getSomeBytes()
        return bytes[:numBytes]  # truncate to the requested length

    def getSomeBytes(self):
        """ Psuedo random bytes are generated 16 bytes at a time.
            The state is updated by applying Rijndael using the Cipher
            Key. The first 128 bits of the state are output as a �pseudorandom number�.
        """
        self.__state = self.__algorithm.encrypt(self.__state)
        return self.__state[:16]
Exemplo n.º 3
0
class PRN_Rijndael:
    """ A Psudeo Random Number Generator based on Rijndael_256k_256b
        The algorithm is based on Section 13.4 of:
        "AES Proposal: Rijndael", Joan Daemen, Vincent Rijmen
    """
    def __init__(self, seed=defaultSeed):
        self.__algorithm = Rijndael(padding=noPadding(),keySize=32, blockSize=32)
        self.reset()
        self.reseed(seed)

    def reset(self):
        self.__algorithm.setKey(self.__algorithm.keySize*chr(0))      # set key to all zeros
        self.__state = self.__algorithm.blockSize*chr(0)              # a single block of zeros

    def reseed(self,seed):
        while len(seed) > 0 :
            if len(seed) < self.__algorithm.blockSize:
                block = seed + (self.__algorithm.blockSize-len(seed))*chr(0)
                seed = ''
            else:
                block =  seed[:self.__algorithm.blockSize]
                seed = seed[self.__algorithm.blockSize:]
            self.__algorithm.setKey( self.__algorithm.encrypt(block) )

    def getBytes(self, numBytes):
        """ Return a psuedo random byte string of length numBytes """
        bytes = ''
        while len(bytes)< numBytes :
            bytes = bytes + self.getSomeBytes()
        return bytes[:numBytes]     # truncate to the requested length

    def getSomeBytes(self):
        """ Psuedo random bytes are generated 16 bytes at a time.
            The state is updated by applying Rijndael using the Cipher
            Key. The first 128 bits of the state are output as a “pseudorandom number”.
        """
        self.__state = self.__algorithm.encrypt(self.__state)
        return self.__state[:16]
Exemplo n.º 4
0
        def RijndaelTestVec(i, key, pt, ct):
            """ Run single AES test vector with any legal blockSize
                and any legal key size. """
            bkey, plainText, cipherText = a2b_hex(key), a2b_hex(pt), a2b_hex(
                ct)
            kSize = len(bkey)
            bSize = len(cipherText)  # set block size to length of block
            alg = Rijndael(bkey,
                           keySize=kSize,
                           blockSize=bSize,
                           padding=noPadding())

            self.assertEqual(alg.encrypt(plainText), cipherText)
            self.assertEqual(alg.decrypt(cipherText), plainText)