Ejemplo n.º 1
0
 def a2b_hex(s):
     return _a2b_hex(b(s))
Ejemplo n.º 2
0
    def test_pbkdf2(self):
        """Module self-test"""
        from binascii import a2b_hex as _a2b_hex

        def a2b_hex(s):
            return _a2b_hex(b(s))

        #
        # Test vectors from RFC 3962
        #

        # Test 1
        result = PBKDF2("password", "ATHENA.MIT.EDUraeburn", 1).read(16)
        expected = a2b_hex("cdedb5281bb2f801565a1122b2563515")
        self.assertEqual(expected, result)

        # Test 2
        result = PBKDF2("password", "ATHENA.MIT.EDUraeburn", 1200).hexread(32)
        expected = "5c08eb61fdf71e4e4ec3cf6ba1f5512b" "a7e52ddbc5e5142f708a31e2e62b1e13"
        self.assertEqual(expected, result)

        # Test 3
        result = PBKDF2("X" * 64, "pass phrase equals block size", 1200).hexread(32)
        expected = "139c30c0966bc32ba55fdbf212530ac9" "c5ec59f1a452f5cc9ad940fea0598ed1"
        self.assertEqual(expected, result)

        # Test 4
        result = PBKDF2("X" * 65, "pass phrase exceeds block size", 1200).hexread(32)
        expected = "9ccad6d468770cd51b10e6a68721be61" "1a8b4d282601db3b36be9246915ec82a"
        self.assertEqual(expected, result)

        #
        # Other test vectors
        #

        # Chunked read
        f = PBKDF2("kickstart", "workbench", 256)
        result = f.read(17)
        result += f.read(17)
        result += f.read(1)
        result += f.read(2)
        result += f.read(3)
        expected = PBKDF2("kickstart", "workbench", 256).read(40)
        self.assertEqual(expected, result)

        #
        # crypt() test vectors
        #

        # crypt 1
        result = crypt("cloadm", "exec")
        expected = "$p5k2$$exec$r1EWMCMk7Rlv3L/RNcFXviDefYa0hlql"
        self.assertEqual(expected, result)

        # crypt 2
        result = crypt("gnu", "$p5k2$c$u9HvcT4d$.....")
        expected = "$p5k2$c$u9HvcT4d$Sd1gwSVCLZYAuqZ25piRnbBEoAesaa/g"
        self.assertEqual(expected, result)

        # crypt 3
        result = crypt("dcl", "tUsch7fU", iterations=13)
        expected = "$p5k2$d$tUsch7fU$nqDkaxMDOFBeJsTSfABsyn.PYUXilHwL"
        self.assertEqual(expected, result)

        # crypt 4 (unicode)
        result = crypt(
            b("\xce\x99\xcf\x89\xce\xb1\xce\xbd\xce\xbd\xce\xb7\xcf\x82").decode("utf-8"),
            "$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ",
        )
        expected = "$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ"
        self.assertEqual(expected, result)

        # crypt 5 (UTF-8 bytes)
        result = crypt(
            b("\xce\x99\xcf\x89\xce\xb1\xce\xbd\xce\xbd\xce\xb7\xcf\x82"),
            "$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ",
        )
        expected = "$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ"
        self.assertEqual(expected, result)
Ejemplo n.º 3
0
 def a2b_hex(s):
     return _a2b_hex(b(s))
Ejemplo n.º 4
0
    def test_pbkdf2(self):
        """Module self-test"""
        from binascii import a2b_hex as _a2b_hex

        def a2b_hex(s):
            return _a2b_hex(b(s))

        #
        # Test vectors from RFC 3962
        #

        # Test 1
        result = PBKDF2("password", "ATHENA.MIT.EDUraeburn", 1).read(16)
        expected = a2b_hex("cdedb5281bb2f801565a1122b2563515")
        self.assertEqual(expected, result)

        # Test 2
        result = PBKDF2("password", "ATHENA.MIT.EDUraeburn", 1200).hexread(32)
        expected = ("5c08eb61fdf71e4e4ec3cf6ba1f5512b"
                    "a7e52ddbc5e5142f708a31e2e62b1e13")
        self.assertEqual(expected, result)

        # Test 3
        result = PBKDF2("X" * 64, "pass phrase equals block size",
                        1200).hexread(32)
        expected = ("139c30c0966bc32ba55fdbf212530ac9"
                    "c5ec59f1a452f5cc9ad940fea0598ed1")
        self.assertEqual(expected, result)

        # Test 4
        result = PBKDF2("X" * 65, "pass phrase exceeds block size",
                        1200).hexread(32)
        expected = ("9ccad6d468770cd51b10e6a68721be61"
                    "1a8b4d282601db3b36be9246915ec82a")
        self.assertEqual(expected, result)

        #
        # Other test vectors
        #

        # Chunked read
        f = PBKDF2("kickstart", "workbench", 256)
        result = f.read(17)
        result += f.read(17)
        result += f.read(1)
        result += f.read(2)
        result += f.read(3)
        expected = PBKDF2("kickstart", "workbench", 256).read(40)
        self.assertEqual(expected, result)

        #
        # crypt() test vectors
        #

        # crypt 1
        result = crypt("cloadm", "exec")
        expected = '$p5k2$$exec$r1EWMCMk7Rlv3L/RNcFXviDefYa0hlql'
        self.assertEqual(expected, result)

        # crypt 2
        result = crypt("gnu", '$p5k2$c$u9HvcT4d$.....')
        expected = '$p5k2$c$u9HvcT4d$Sd1gwSVCLZYAuqZ25piRnbBEoAesaa/g'
        self.assertEqual(expected, result)

        # crypt 3
        result = crypt("dcl", "tUsch7fU", iterations=13)
        expected = "$p5k2$d$tUsch7fU$nqDkaxMDOFBeJsTSfABsyn.PYUXilHwL"
        self.assertEqual(expected, result)

        # crypt 4 (unicode)
        result = crypt(
            b('\xce\x99\xcf\x89\xce\xb1\xce\xbd\xce\xbd\xce\xb7\xcf\x82'
              ).decode('utf-8'),
            '$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ')
        expected = '$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ'
        self.assertEqual(expected, result)

        # crypt 5 (UTF-8 bytes)
        result = crypt(
            b('\xce\x99\xcf\x89\xce\xb1\xce\xbd\xce\xbd\xce\xb7\xcf\x82'),
            '$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ')
        expected = '$p5k2$$KosHgqNo$9mjN8gqjt02hDoP0c2J0ABtLIwtot8cQ'
        self.assertEqual(expected, result)
Ejemplo n.º 5
0
	plaintext = decryptor.decrypt(ciphertext)
	print "%s" % plaintext
	
with open(r'f:\downloads\TSREXPORT_20131217144602', 'rb') as f:
	data = f.read()
	
	initVector = "tsrvector9567482"
	salt = "akdirn%/=(+\"922xc&@s"
	pswdIterations = 128
	password = "******"weg112W"
	print(initVector)
	saltb = salt.encode('utf8')
	saltc = salt.decode('utf8')
	print(salt, saltb, saltc)
	print(type(salt),type(saltb),type(saltc))
	wb = initVector.encode('utf8')
	initVectorB = b("").join([pack("@H", randint(0, 0xffff)) for i in range(16)])
	print(type(saltb), type(wb), type(initVectorB))
	#for i in range(16):
	#	if i < len(wb): initVectorB[i] = wb[i]
	#	else: initVectorB[i] = -100-i
	#print(initVectorB)
	
	pbk = PBKDF2(password, saltb, pswdIterations)
	
	#k = pbk.read(256)
	#for i in k: print(ord(i))
	cipher = AES.new(pbk.read(32), AES.MODE_CBC, initVector)
	plaintext = cipher.decrypt(data)
	#for i in plaintext: print(ord(i))
	print(plaintext)
Ejemplo n.º 6
0
    def test_crypt(self):
        result = crypt("secret")
        self.assertEqual(result[:6], "$p5k2$")

        # Default algorithm is `sha1`; default number of rounds is 4096
        result = crypt("secret", "XXXXXXXX")
        expected = '$p5k2$sha512$1000$XXXXXXXX$'
        self.assertEqual(expected, result[:27])

        # 400 iterations
        result = crypt("secret", "XXXXXXXX", 400)
        expected = '$p5k2$sha512$190$XXXXXXXX$'
        self.assertEqual(expected, result[:26])

        # 400 iterations (keyword argument)
        result = crypt("spam", "FRsH3HJB", iterations=400)
        expected = '$p5k2$sha512$190$FRsH3HJB$'
        self.assertEqual(expected, result[:26])

        # 1000 iterations
        result = crypt("spam", "H0NX9mT/", iterations=1000)
        expected = '$p5k2$sha512$3e8$H0NX9mT/$'
        self.assertEqual(expected, result[:26])

        # 1000 iterations (iterations count taken from salt parameter)
        expected = '$p5k2$sha1$3e8$H0NX9mT/$ih6FhDyRXAaEN4UXk50pNsZP/nU='
        result = crypt("spam", expected, digestmodule=SHA1)
        self.assertEqual(expected, result)

        # Feed the result back in; both hashes should match, as the algo and iteration count are taken from the expected hash
        expected = crypt("spam")
        result = crypt( "spam", expected )
        self.assertEqual(expected, result)

        # ...and this one shouldn't match
        expected = crypt("password")
        result = crypt( "passwd", expected )
        self.assertNotEqual(expected, result)

        #
        # SHA256
        #
        result = crypt("spam", "XXXXXXXX", digestmodule=SHA256)
        expected = '$p5k2$sha256$1000$XXXXXXXX$'
        self.assertEqual(expected, result[:27])

        # Feed the result back in; both hashes should match, as the algo and iteration count are taken from the expected hash
        expected = crypt("spam", digestmodule=SHA256)
        result = crypt( "spam", expected )
        self.assertEqual(expected, result)

        # ...and this one shouldn't match
        expected = crypt("password", digestmodule=SHA256)
        result = crypt( "passwd", expected )
        self.assertNotEqual(expected, result)

        #
        # SHA512
        #
        result = crypt("spam", "XXXXXXXX", digestmodule=SHA512)
        expected = '$p5k2$sha512$1000$XXXXXXXX$'
        self.assertEqual(expected, result[:27])

        # Feed the result back in; both hashes should match, as the algo and iteration count are taken from the expected hash
        expected = crypt("spam", digestmodule=SHA512)
        result = crypt( "spam", expected )
        self.assertEqual(expected, result)

        # ...and this one shouldn't match
        expected = crypt("password", digestmodule=SHA512)
        result = crypt( "passwd", expected )
        self.assertNotEqual(expected, result)



        #
        # crypt() test vectors
        #

        # crypt 1
        result = crypt("cloadm", "exec", iterations=400, digestmodule=SHA1)
        expected = '$p5k2$sha1$190$exec$jkxkBaZJp.nvBg4WV7BW96972fE='
        self.assertEqual(expected, result)

        # crypt 2
        result = crypt("gnu", '$p5k2$sha1$c$u9HvcT4d$.....')
        expected = '$p5k2$sha1$c$u9HvcT4d$iDgHukD37rW7UgWCS24lnNRjO3c='
        self.assertEqual(expected, result)

        # crypt 3
        result = crypt("dcl", "tUsch7fU", iterations=13, digestmodule=SHA1)
        expected = "$p5k2$sha1$d$tUsch7fU$.8H47sUSBmz0PDHbKfXHkjDDboo="
        self.assertEqual(expected, result)

        # crypt 3, SHA256
        result = crypt("dcl", "tUsch7fU", iterations=13, digestmodule=SHA256)
        expected = "$p5k2$sha256$d$tUsch7fU$A1I2wQdnQb28U7UD4aaxwuFL5IFj.AWLngbwVLHOkVo="
        self.assertEqual(expected, result)

        # crypt3, SHA512
        result = crypt("dcl", "tUsch7fU", iterations=13, digestmodule=SHA512)
        expected = "$p5k2$sha512$d$tUsch7fU$GM78GODhPDWxODRnH4/L9lGnTqMgmsYJEROltbxUVquPm1P9qmbRkQM1KuFOf6QEBXX20eMGwYRmDrFLHDyn6Q=="
        self.assertEqual(expected, result)

        # crypt 4 (unicode)
        result = crypt(b('\xce\x99\xcf\x89\xce\xb1\xce\xbd\xce\xbd\xce\xb7\xcf\x82').decode('utf-8'),
            '$p5k2$sha1$3e8$KosHgqNo$jJ.gcxXLu6COVzAlz5SRvAqZTd8=')
        expected = '$p5k2$sha1$3e8$KosHgqNo$jJ.gcxXLu6COVzAlz5SRvAqZTd8='
        self.assertEqual(expected, result)

        # crypt 5 (UTF-8 bytes)
        result = crypt(b('\xce\x99\xcf\x89\xce\xb1\xce\xbd\xce\xbd\xce\xb7\xcf\x82'),
            '$p5k2$sha1$3e8$KosHgqNo$jJ.gcxXLu6COVzAlz5SRvAqZTd8=')
        expected = '$p5k2$sha1$3e8$KosHgqNo$jJ.gcxXLu6COVzAlz5SRvAqZTd8='
        self.assertEqual(expected, result)