def import_publickeyblob(cls, data): rsapubkey = RSAPUBKEY._make(RSAPUBKEY_s.unpack_from(data)) assert rsapubkey.magic == RSAPUBKEY_MAGIC bitlen8 = rsapubkey.bitlen // 8 modulus = bytes_to_long(data[12:12 + bitlen8][::-1]) r = RSA.construct((modulus, long(rsapubkey.pubexp))) return cls(r)
def export_publickeyblob(self): n = self.key.key.n e = self.key.key.e n_bytes = long_to_bytes(n)[::-1] result = PUBLICKEYSTRUC_s.pack(bType_PUBLICKEYBLOB, CUR_BLOB_VERSION, CALG_RSA_KEYX) result += RSAPUBKEY_s.pack(RSAPUBKEY_MAGIC, len(n_bytes) * 8, e) result += n_bytes return result
def import_privatekeyblob(cls, data): rsapubkey = RSAPUBKEY._make(RSAPUBKEY_s.unpack_from(data)) assert rsapubkey.magic == PRIVATEKEYBLOB_MAGIC bitlen8 = rsapubkey.bitlen / 8 bitlen16 = rsapubkey.bitlen / 16 privatekeyblob_s = struct.Struct( '%ds%ds%ds%ds%ds%ds%ds' % (bitlen8, bitlen16, bitlen16, bitlen16, bitlen16, bitlen16, bitlen8)) privatekey = PRIVATEKEYBLOB._make(bytes_to_long(x[::-1]) for x in privatekeyblob_s.unpack_from(data[12:])) r = RSA.construct((privatekey.modulus, long(rsapubkey.pubexp), privatekey.privateExponent, privatekey.prime1, privatekey.prime2)) return cls(r)
def export_privatekeyblob(self): key = self.key.key n = key.n e = key.e d = key.d p = key.p q = key.q n_bytes = long_to_bytes(n)[::-1] key_len = len(n_bytes) * 8 result = PUBLICKEYSTRUC_s.pack(bType_PRIVATEKEYBLOB, CUR_BLOB_VERSION, CALG_RSA_KEYX) result += RSAPUBKEY_s.pack(PRIVATEKEYBLOB_MAGIC, key_len, e) result += n_bytes result += long_to_bytes(p, key_len / 16)[::-1] result += long_to_bytes(q, key_len / 16)[::-1] result += long_to_bytes(d % (p - 1), key_len / 16)[::-1] result += long_to_bytes(d % (q - 1), key_len / 16)[::-1] result += long_to_bytes(inverse(q, p), key_len / 16)[::-1] result += long_to_bytes(d, key_len / 8)[::-1] return result