def __init__(self, key): """@param key the ASCII string from id_rsa.pub from ssh-keygen""" if key.startswith("ssh-rsa"): self.decoded = key.split(" ")[1] self.encoded = base64.b64decode(self.decoded) else: self.encoded = key self.decoded = base64.b64encode(self.encoded) self.fp = hashlib.sha1(self.encoded).digest()[:6] fields = read_fields(self.encoded) sigtype = fields.next() if sigtype != "ssh-rsa": raise exceptions.KeyError("Unknown key type %s. This code " "currently only supports ssh-rsa" % sigtype) self.exp = _str_to_int(fields.next()) self.mod = _str_to_int(fields.next()) # it turns out that ssh writes leading zeroes, which we get rid of # by roundtripping to bignum. self.mod_size = len(_int_to_str(self.mod))
def encrypt(self, data): if len(data) > self.mod_size: raise exceptions.KeyError("Key size too small, more than %d bytes " "of data can not be encrypted" % self.mod_size) return _int_to_str(pow(_str_to_int(data), self.private_exp, self.mod))