コード例 #1
0
    def encryptResponse(self, request, decrypted, response):
        randomSalt = self.getRandomSalt()
        sha256 = hashlib.sha256()
        sha256.update(randomSalt)  #*2to3*
        result = sha256.digest()

        iv = bytearray(request['message']['salt'].encode('latin-1'))  #*2to3*

        randomStuff = bytearray(16)
        for i in range(0, 16):
            randomStuff[i] = (bytearray(decrypted['salt'].encode('latin-1'))[i]
                              ^ iv[i] ^ randomSalt[i]) & 0xff  #*2to3*

        responsedata = self.DecryptedResponse()
        responsedata['response'] = response
        responsedata['keys'] = randomStuff  #*2to3*
        responsedata['hash'] = result

        padded = aes.append_PKCS7_padding(
            str(responsedata).encode('latin-1'))  #*2to3*
        moo = aes.AESModeOfOperation()
        moo.aes.v6 = self.v6
        mode, orig_len, crypted = moo.encrypt(padded,
                                              moo.ModeOfOperation["CBC"],
                                              self.key,
                                              moo.aes.KeySize["SIZE_128"],
                                              iv)  #*2to3*

        return iv.decode('latin-1').encode('latin-1'), crypted  #*2to3*
コード例 #2
0
ファイル: kmsRequestV5.py プロジェクト: andir/py-kms
	def generateRequest(self, requestBase):
		esalt = self.getRandomSalt()

		moo = aes.AESModeOfOperation()
		moo.aes.v6 = self.v6
		dsalt = moo.decrypt(esalt, 16, moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], esalt)
		dsalt = bytearray(dsalt)

		decrypted = self.DecryptedRequest()
		decrypted['salt'] = str(dsalt)
		decrypted['request'] = requestBase

		padded = aes.append_PKCS7_padding(str(decrypted))
		mode, orig_len, crypted = moo.encrypt(padded, moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], esalt)

		message = self.RequestV5.Message(str(bytearray(crypted)))

		bodyLength = len(message) + 2 + 2

		request = self.RequestV5()
		request['bodyLength1'] = bodyLength
		request['bodyLength2'] = bodyLength
		request['versionMinor'] = requestBase['versionMinor']
		request['versionMajor'] = requestBase['versionMajor']
		request['message'] = message

		if self.config['debug']:
			print "Request V%d Data: %s" % (self.ver, request.dump())
			print "Request V%d: %s" % (self.ver, binascii.b2a_hex(str(request)))

		return request
コード例 #3
0
        def generateRequest(self, requestBase):
                esalt = self.getRandomSalt()

                moo = aes.AESModeOfOperation()
                moo.aes.v6 = self.v6
                dsalt = moo.decrypt(esalt, 16, moo.ModeOfOperation["CBC"], self.key, moo.aes.KeySize["SIZE_128"], esalt) #*2to3*
                dsalt = bytearray(dsalt)

                decrypted = self.DecryptedRequest()
                decrypted['salt'] = dsalt #*2to3*
                decrypted['request'] = requestBase

                padded = aes.append_PKCS7_padding(str(decrypted).encode('latin-1')) #*2to3*
                mode, orig_len, crypted = moo.encrypt(padded, moo.ModeOfOperation["CBC"], self.key, moo.aes.KeySize["SIZE_128"], esalt) #*2to3*

                message = self.RequestV5.Message(bytes(crypted)) #*2to3*

                bodyLength = len(message) + 2 + 2

                request = self.RequestV5()
                request['bodyLength1'] = bodyLength
                request['bodyLength2'] = bodyLength
                request['versionMinor'] = requestBase['versionMinor']
                request['versionMajor'] = requestBase['versionMajor']
                request['message'] = message

                shell_message(nshell = 10)
                request = byterize(request)
                logging.info("Request V%d Data: \n%s\n" % (self.ver, justify(request.dump(print_to_stdout = False))))
                logging.info("Request V%d: \n%s\n" % (self.ver, justify(binascii.b2a_hex(str(request).encode('latin-1')).decode('utf-8')))) #*2to3*
                
                return request
コード例 #4
0
    def encryptResponse(self, request, decrypted, response):
        randomSalt = self.getRandomSalt()
        sha256 = hashlib.sha256()
        sha256.update(str(randomSalt))
        result = sha256.digest()

        SaltC = bytearray(request['message']['salt'])
        DSaltC = bytearray(decrypted['salt'])

        randomStuff = bytearray(16)
        for i in range(0, 16):
            randomStuff[i] = (SaltC[i] ^ DSaltC[i] ^ randomSalt[i]) & 0xff

        # XorSalts
        XorSalts = bytearray(16)
        for i in range(0, 16):
            XorSalts[i] = (SaltC[i] ^ DSaltC[i]) & 0xff

        message = self.DecryptedResponse.Message()
        message['response'] = response
        message['keys'] = str(randomStuff)
        message['hash'] = result
        message['xorSalts'] = str(XorSalts)
        message['hwid'] = self.config['hwid']

        # SaltS
        SaltS = self.getRandomSalt()

        moo = aes.AESModeOfOperation()
        moo.aes.v6 = True
        d = moo.decrypt(SaltS, 16, moo.modeOfOperation["CBC"], self.key,
                        moo.aes.keySize["SIZE_128"], SaltS)

        # DSaltS
        DSaltS = bytearray(d)

        # HMacMsg
        HMacMsg = bytearray(16)
        for i in range(0, 16):
            HMacMsg[i] = (SaltS[i] ^ DSaltS[i]) & 0xff
        HMacMsg.extend(str(message))

        # HMacKey
        requestTime = decrypted['request']['requestTime']
        HMacKey = self.getMACKey(requestTime)
        HMac = hmac.new(HMacKey, str(HMacMsg), hashlib.sha256)
        digest = HMac.digest()

        responsedata = self.DecryptedResponse()
        responsedata['message'] = message
        responsedata['hmac'] = digest[16:]

        padded = aes.append_PKCS7_padding(str(responsedata))
        mode, orig_len, crypted = moo.encrypt(str(padded),
                                              moo.modeOfOperation["CBC"],
                                              self.key,
                                              moo.aes.keySize["SIZE_128"],
                                              SaltS)

        return str(SaltS), str(bytearray(crypted))
コード例 #5
0
ファイル: kmsRequestV5.py プロジェクト: zen9073/py-kms
        def generateRequest(self, requestBase):
                esalt = self.getRandomSalt()

                moo = aes.AESModeOfOperation()
                moo.aes.v6 = self.v6
                dsalt = moo.decrypt(esalt, 16, moo.ModeOfOperation["CBC"], self.key, moo.aes.KeySize["SIZE_128"], esalt)
                dsalt = bytearray(dsalt)

                decrypted = self.DecryptedRequest()
                decrypted['salt'] = dsalt
                decrypted['request'] = requestBase

                padded = aes.append_PKCS7_padding(str(decrypted).encode('latin-1'))
                mode, orig_len, crypted = moo.encrypt(padded, moo.ModeOfOperation["CBC"], self.key, moo.aes.KeySize["SIZE_128"], esalt)

                message = self.RequestV5.Message(bytes(crypted))   

                request = self.RequestV5()
                bodyLength = 2 + 2 + len(message)
                request['bodyLength1'] = bodyLength
                request['bodyLength2'] = bodyLength
                request['versionMinor'] = requestBase['versionMinor']
                request['versionMajor'] = requestBase['versionMajor']
                request['message'] = message

                shell_message(nshell = 10)
                request = byterize(request)
                logger.info("Request V%d Data: \n%s\n" % (self.ver, justify(request.dump(print_to_stdout = False))))
                logger.info("Request V%d: \n%s\n" % (self.ver, justify(binascii.b2a_hex(str(request).encode('latin-1')).decode('utf-8'))))
                
                return request
コード例 #6
0
    def generateRequest(self, requestBase):
        esalt = self.getRandomSalt()

        moo = aes.AESModeOfOperation()
        moo.aes.v6 = self.v6
        dsalt = moo.decrypt(esalt, 16, moo.modeOfOperation["CBC"], self.key,
                            moo.aes.keySize["SIZE_128"], esalt)
        dsalt = bytearray(dsalt)

        decrypted = self.DecryptedRequest()
        decrypted['salt'] = str(dsalt)
        decrypted['request'] = requestBase

        padded = aes.append_PKCS7_padding(str(decrypted))
        mode, orig_len, crypted = moo.encrypt(padded,
                                              moo.modeOfOperation["CBC"],
                                              self.key,
                                              moo.aes.keySize["SIZE_128"],
                                              esalt)

        message = self.RequestV5.Message(str(bytearray(crypted)))

        bodyLength = len(message) + 2 + 2

        request = self.RequestV5()
        request['bodyLength1'] = bodyLength
        request['bodyLength2'] = bodyLength
        request['versionMinor'] = requestBase['versionMinor']
        request['versionMajor'] = requestBase['versionMajor']
        request['message'] = message
        logging.info("Request V%d Data: %s" % (self.ver, request.dump()))
        logging.info("Request V%d: %s" %
                     (self.ver, binascii.b2a_hex(str(request))))

        return request
コード例 #7
0
    def test08(self):
        print 'test08-------------------------'
        # en--
        key = 'Call me Ishmael.'[:16]
        key = map(ord, key)
        data = 'This is not a block length.'
        mode = aes.AESModeOfOperation.modeOfOperation["CBC"]

        if mode == aes.AESModeOfOperation.modeOfOperation["CBC"]:
            data = aes.append_PKCS7_padding(data)
        keysize = len(key)
        assert keysize in aes.AES.keySize.values(
        ), 'invalid key size: %s' % keysize
        # create a new iv using random data
        iv = [12, 34, 96, 15, 12, 34, 96, 15, 12, 34, 96, 15, 12, 34, 96, 15]
        moo = aes.AESModeOfOperation()
        (mode, length, ciph) = moo.encrypt(data, mode, key, keysize, iv)
        # With padding, the original length does not need to be known. It's a bad
        # idea to store the original message length.
        # prepend the iv.
        encrypt_data = ciph
        print aes.decryptDataIv(key, ciph, iv)

        keysize = len(key)
        assert keysize in aes.AES.keySize.values(
        ), 'invalid key size: %s' % keysize
        # iv is first 16 bytes
        #iv = map(ord, encrypt_data[:16])

        moo = aes.AESModeOfOperation()
        decr = moo.decrypt(encrypt_data, None, mode, key, keysize, iv)
        if mode == aes.AESModeOfOperation.modeOfOperation["CBC"]:
            decr = aes.strip_PKCS7_padding(decr)
        print decr
コード例 #8
0
  def test08(self):
    print 'test08-------------------------'
    # en--
    key = 'Call me Ishmael.'[:16]
    key = map(ord, key)
    data = 'This is not a block length.'
    mode = aes.AESModeOfOperation.modeOfOperation["CBC"]


    if mode == aes.AESModeOfOperation.modeOfOperation["CBC"]:
        data = aes.append_PKCS7_padding(data)
    keysize = len(key)
    assert keysize in aes.AES.keySize.values(), 'invalid key size: %s' % keysize
    # create a new iv using random data
    iv = [12, 34, 96, 15, 12, 34, 96, 15, 12, 34, 96, 15, 12, 34, 96, 15]
    moo = aes.AESModeOfOperation()
    (mode, length, ciph) = moo.encrypt(data, mode, key, keysize, iv)
    # With padding, the original length does not need to be known. It's a bad
    # idea to store the original message length.
    # prepend the iv.
    encrypt_data = ciph
    print aes.decryptDataIv(key, ciph, iv)


    keysize = len(key)
    assert keysize in aes.AES.keySize.values(), 'invalid key size: %s' % keysize
    # iv is first 16 bytes
    #iv = map(ord, encrypt_data[:16])

    moo = aes.AESModeOfOperation()
    decr = moo.decrypt(encrypt_data, None, mode, key, keysize, iv)
    if mode == aes.AESModeOfOperation.modeOfOperation["CBC"]:
        decr = aes.strip_PKCS7_padding(decr)
    print decr
コード例 #9
0
ファイル: service.py プロジェクト: xxDaGeRxx/libsecret
 def encrypt(self, key, data):
     key = map(ord, key)
     data = aes.append_PKCS7_padding(data)
     keysize = len(key)
     iv = [ord(i) for i in os.urandom(16)]
     mode = aes.AESModeOfOperation.modeOfOperation["CBC"]
     moo = aes.AESModeOfOperation()
     (mode, length, ciph) = moo.encrypt(data, mode, key, keysize, iv)
     return ("".join([chr(i) for i in iv]), "".join([chr(i) for i in ciph]))
コード例 #10
0
        def encryptResponse(self, request, decrypted, response):
                randomSalt = self.getRandomSalt()

                sha256 = hashlib.sha256()
                sha256.update(randomSalt) #*2to3*
                result = sha256.digest()

                SaltC = bytearray(request['message']['salt'].encode('latin-1')) #*2to3*
                DSaltC = bytearray(decrypted['salt'].encode('latin-1')) #*2to3*

                randomStuff = bytearray(16)
                for i in range(0,16):
                        randomStuff[i] = (SaltC[i] ^ DSaltC[i] ^ randomSalt[i]) & 0xff

                # XorSalts
                XorSalts = bytearray(16)
                for i in range (0, 16):
                        XorSalts[i] = (SaltC[i] ^ DSaltC[i]) & 0xff

                message = self.DecryptedResponse.Message()
                message['response'] = response
                message['keys'] = randomStuff #*2to3* 
                message['hash'] = result
                message['xorSalts'] = XorSalts #*2to3* 
                message['hwid'] = self.config['hwid']

                # SaltS
                SaltS = self.getRandomSalt()

                moo = aes.AESModeOfOperation()
                moo.aes.v6 = True
                decry = moo.decrypt(SaltS, 16, moo.ModeOfOperation["CBC"], self.key, moo.aes.KeySize["SIZE_128"], SaltS) #*2to3*

                # DSaltS
                DSaltS = bytearray(decry)

                # HMacMsg
                HMacMsg = bytearray(16)
                for i in range(0,16):
                        HMacMsg[i] = (SaltS[i] ^ DSaltS[i]) & 0xff
                HMacMsg.extend(str(message).encode('latin-1')) #*2to3*

                # HMacKey
                requestTime = decrypted['request']['requestTime']
                HMacKey = self.getMACKey(requestTime)                
                HMac = hmac.new(HMacKey, HMacMsg, hashlib.sha256) #*2to3*
                digest = HMac.digest()

                responsedata = self.DecryptedResponse()
                responsedata['message'] = message
                responsedata['hmac'] = digest[16:]

                padded = aes.append_PKCS7_padding(str(responsedata).encode('latin-1')) #*2to3*
                mode, orig_len, crypted = moo.encrypt(padded, moo.ModeOfOperation["CBC"], self.key, moo.aes.KeySize["SIZE_128"], SaltS) #*2to3*

                return SaltS.decode('latin-1').encode('latin-1'), crypted #*2to3*
コード例 #11
0
def aes_encrypt_with_iv(key, iv, data):
    mode = aes.AESModeOfOperation.modeOfOperation["CBC"]
    key = map(ord, key)
    iv = map(ord, iv)
    data = aes.append_PKCS7_padding(data)
    keysize = len(key)
    assert keysize in aes.AES.keySize.values(), 'invalid key size: %s' % keysize
    moo = aes.AESModeOfOperation()
    (mode, length, ciph) = moo.encrypt(data, mode, key, keysize, iv)
    return ''.join(map(chr, ciph))
コード例 #12
0
ファイル: bitcoin.py プロジェクト: etherx-dev/electrum
def aes_encrypt_with_iv(key, iv, data):
    mode = aes.AESModeOfOperation.modeOfOperation["CBC"]
    key = map(ord, key)
    iv = map(ord, iv)
    data = aes.append_PKCS7_padding(data)
    keysize = len(key)
    assert keysize in aes.AES.keySize.values(), 'invalid key size: %s' % keysize
    moo = aes.AESModeOfOperation()
    (mode, length, ciph) = moo.encrypt(data, mode, key, keysize, iv)
    return ''.join(map(chr, ciph))
コード例 #13
0
ファイル: service.py プロジェクト: Arkhont/libsecret
	def encrypt(self, key, data):
		key = map(ord, key)
		data = aes.append_PKCS7_padding(data)
		keysize = len(key)
		iv = [ord(i) for i in os.urandom(16)]
		mode = aes.AESModeOfOperation.modeOfOperation["CBC"]
		moo = aes.AESModeOfOperation()
		(mode, length, ciph) = moo.encrypt(data, mode, key, keysize, iv)
		return ("".join([chr(i) for i in iv]),
		        "".join([chr(i) for i in ciph]))
コード例 #14
0
ファイル: kmsRequestV6.py プロジェクト: 0oVicero0/py-kms
	def encryptResponse(self, request, decrypted, response):
		randomSalt = self.getRandomSalt()
		sha256 = hashlib.sha256()
		sha256.update(str(randomSalt))
		result = sha256.digest()

		SaltC = bytearray(request['message']['salt'])
		DSaltC = bytearray(decrypted['salt'])

		randomStuff = bytearray(16)
		for i in range(0,16):
			randomStuff[i] = (SaltC[i] ^ DSaltC[i] ^ randomSalt[i]) & 0xff

		# XorSalts
		XorSalts = bytearray(16)
		for i in range (0, 16):
			XorSalts[i] = (SaltC[i] ^ DSaltC[i]) & 0xff

		message = self.DecryptedResponse.Message()
		message['response'] = response
		message['keys'] = str(randomStuff)
		message['hash'] = result
		message['xorSalts'] = str(XorSalts)

		# SaltS
		SaltS = self.getRandomSalt()

		moo = aes.AESModeOfOperation()
		moo.aes.v6 = True
		d = moo.decrypt(SaltS, 16, moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], SaltS)

		# DSaltS
		DSaltS = bytearray(d)

		# HMacMsg
		HMacMsg = bytearray(16)
		for i in range (0, 16):
			HMacMsg[i] = (SaltS[i] ^ DSaltS[i]) & 0xff
		HMacMsg.extend(str(message))

		# HMacKey
		requestTime = decrypted['request']['requestTime']
		HMacKey = self.getMACKey(requestTime)
		HMac = hmac.new(HMacKey, str(HMacMsg), hashlib.sha256)
		digest = HMac.digest()

		responsedata = self.DecryptedResponse()
		responsedata['message'] = message
		responsedata['hmac'] = digest[16:]

		padded = aes.append_PKCS7_padding(str(responsedata))
		mode, orig_len, crypted = moo.encrypt(str(padded), moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], SaltS)

		return str(SaltS), str(bytearray(crypted))
コード例 #15
0
ファイル: kmsRequestV5.py プロジェクト: JBA474/py-kms
	def encryptResponse(self, request, decrypted, response):
		randomSalt = self.getRandomSalt()
		sha256 = hashlib.sha256()
		sha256.update(str(randomSalt))
		result = sha256.digest()

		iv = bytearray(request['message']['salt'])

		randomStuff = bytearray(16)
		for i in range(0,16):
			randomStuff[i] = (bytearray(decrypted['salt'])[i] ^ iv[i] ^ randomSalt[i]) & 0xff

		responsedata = self.DecryptedResponse()
		responsedata['response'] = response
		responsedata['keys'] = str(randomStuff)
		responsedata['hash'] = result
		
		padded = aes.append_PKCS7_padding(str(responsedata))
		moo = aes.AESModeOfOperation()
		moo.aes.v6 = self.v6
		mode, orig_len, crypted = moo.encrypt(padded, moo.modeOfOperation["CBC"], self.key, moo.aes.keySize["SIZE_128"], iv)

		return str(iv), str(bytearray(crypted))
コード例 #16
0
ファイル: kmsRequestV5.py プロジェクト: word911/py-kms-1
    def encryptResponse(self, request, decrypted, response):
        randomSalt = self.getRandomSalt()
        result = hashlib.sha256(str(randomSalt)).digest()
        iv = bytearray(request['message']['salt'])

        randomStuff = bytearray(16)
        for i in range(0, 16):
            randomStuff[i] = (bytearray(decrypted['salt'])[i] ^ iv[i]
                              ^ randomSalt[i]) & 0xff

        responsedata = self.DecryptedResponse()
        responsedata['response'] = response
        responsedata['keys'] = str(randomStuff)
        responsedata['hash'] = result

        padded = aes.append_PKCS7_padding(str(responsedata))
        moo = aes.AESModeOfOperation()
        moo.aes.v6 = self.v6
        mode, orig_len, crypted = moo.encrypt(padded,
                                              moo.modeOfOperation["CBC"],
                                              self.key,
                                              moo.aes.keySize["SIZE_128"], iv)

        return str(iv), str(bytearray(crypted))