Exemplo n.º 1
0
def encrypt(key_id, inp):
    """
    Encrypt ``inp`` string using given private key ID.

    :param key_id: private key id to be used
    :param inp: raw binary input string to be encrypted

    Return encrypted string.
    """
    if key_id == 'master':  # master
        if _Debug:
            lg.out(_DebugLevel, 'my_keys.encrypt  payload of %d bytes using my master key' % len(inp))
        return key.EncryptLocalPublicKey(inp)
    key_id = latest_key_id(key_id)
    if key_id == my_id.getGlobalID(key_alias='master'):  # [email protected]
        if _Debug:
            lg.out(_DebugLevel, 'my_keys.encrypt  payload of %d bytes using my master key' % len(inp))
        return key.EncryptLocalPublicKey(inp)
    if key_id == my_id.getGlobalID():  # [email protected]
        if _Debug:
            lg.out(_DebugLevel, 'my_keys.encrypt  payload of %d bytes using my master key' % len(inp))
        return key.EncryptLocalPublicKey(inp)
    if key_id not in known_keys():
        raise Exception('key %s is unknown' % key_id)
    if known_keys()[key_id] is None:
        if not load_key(key_id):
            raise Exception('key load failed: %s' % key_id)
    key_object = known_keys()[key_id]
    if _Debug:
        lg.out(_DebugLevel, 'my_keys.encrypt  payload of %d bytes with key %s' % (len(inp), key_id, ))
    result = key_object.encrypt(inp)
    return result
Exemplo n.º 2
0
 def __init__(
     self,
     CreatorID=None,
     BackupID='',
     BlockNumber=0,
     SessionKey='',
     SessionKeyType=None,
     LastBlock=True,
     Data='',
     EncryptKey=None,
     DecryptKey=None,
 ):
     self.CreatorID = CreatorID
     if not self.CreatorID:
         self.CreatorID = my_id.getLocalID()
     self.BackupID = str(BackupID)
     self.BlockNumber = BlockNumber
     if callable(EncryptKey):
         self.EncryptedSessionKey = EncryptKey(SessionKey)
     elif isinstance(EncryptKey, basestring):
         self.EncryptedSessionKey = my_keys.encrypt(EncryptKey, SessionKey)
     else:
         self.EncryptedSessionKey = key.EncryptLocalPublicKey(SessionKey)
     self.SessionKeyType = SessionKeyType
     if not self.SessionKeyType:
         self.SessionKeyType = key.SessionKeyType()
     self.Length = len(Data)
     self.LastBlock = bool(LastBlock)
     self.EncryptedData = key.EncryptWithSessionKey(SessionKey,
                                                    Data)  # DataLonger
     self.Signature = None
     self.Sign()
     self.DecryptKey = DecryptKey
     if _Debug:
         lg.out(_DebugLevel, 'new data in %s' % self)
Exemplo n.º 3
0
def encrypt(key_id, inp):
    """
    Encrypt ``inp`` string using given private key ID.

    :param key_id: private key id to be used
    :param inp: raw input string to be encrypted

    Return encrypted string.
    """
    if key_id == 'master':  # master
        if _Debug:
            lg.out(
                _DebugLevel,
                'my_keys.encrypt  payload of %d bytes using my master key' %
                len(inp))
        return key.EncryptLocalPublicKey(inp)
    if key_id == my_id.getGlobalID(key_alias='master'):  # [email protected]
        if _Debug:
            lg.out(
                _DebugLevel,
                'my_keys.encrypt  payload of %d bytes using my master key' %
                len(inp))
        return key.EncryptLocalPublicKey(inp)
    if key_id == my_id.getGlobalID():  # [email protected]
        if _Debug:
            lg.out(
                _DebugLevel,
                'my_keys.encrypt  payload of %d bytes using my master key' %
                len(inp))
        return key.EncryptLocalPublicKey(inp)
    key_object = known_keys().get(key_id)
    if not key_object:
        lg.warn('key %s is unknown' % key_id)
        return None
    if _Debug:
        lg.out(
            _DebugLevel, 'my_keys.encrypt  payload of %d bytes with key %s' % (
                len(inp),
                key_id,
            ))
    # There is a bug in rsa.encrypt if there is a leading '\0' in the string.
    # See bug report in http://permalink.gmane.org/gmane.comp.python.cryptography.cvs/217
    # So we add a "1" in front now and in decrypt() we will remove it
    atuple = key_object.keyObject.encrypt('1' + inp, "")
    return atuple[0]
Exemplo n.º 4
0
 def __init__(
     self,
     CreatorID=None,
     BackupID='',
     BlockNumber=0,
     SessionKey='',
     SessionKeyType=None,
     LastBlock=True,
     Data=b'',
     EncryptKey=None,
     DecryptKey=None,
     EncryptedSessionKey=None,
     EncryptedData=None,
     Length=None,
     Signature=None,
 ):
     self.CreatorID = CreatorID
     if not self.CreatorID:
         self.CreatorID = my_id.getLocalID()
     if not isinstance(self.CreatorID, id_url.ID_URL_FIELD):
         self.CreatorID = id_url.field(self.CreatorID)
     self.BackupID = strng.to_text(BackupID)
     self.BlockNumber = BlockNumber
     self.LastBlock = bool(LastBlock)
     self.SessionKeyType = SessionKeyType or key.SessionKeyType()
     if EncryptedSessionKey:
         # this block to be decrypted after receiving
         self.EncryptedSessionKey = EncryptedSessionKey
     else:
         # this block to be encrypted before sending
         if callable(EncryptKey):
             self.EncryptedSessionKey = EncryptKey(SessionKey)
         elif strng.is_text(EncryptKey):
             self.EncryptedSessionKey = my_keys.encrypt(
                 EncryptKey, SessionKey)
         elif strng.is_bin(EncryptKey):
             self.EncryptedSessionKey = my_keys.encrypt(
                 strng.to_text(EncryptKey), SessionKey)
         else:
             self.EncryptedSessionKey = key.EncryptLocalPublicKey(
                 SessionKey)
     if EncryptedData and Length is not None:
         self.Length = Length
         self.EncryptedData = EncryptedData
     else:
         self.Length = len(Data)
         self.EncryptedData = key.EncryptWithSessionKey(
             SessionKey, Data, session_key_type=self.SessionKeyType)
     if Signature:
         self.Signature = Signature
     else:
         self.Signature = None
         self.Sign(signing_key=EncryptKey)
     self.DecryptKey = DecryptKey
     if _Debug:
         lg.out(_DebugLevel, 'new data in %s' % self)
Exemplo n.º 5
0
 def __init__(
     self,
     CreatorID=None,
     BackupID='',
     BlockNumber=0,
     SessionKey='',
     SessionKeyType=None,
     LastBlock=True,
     Data='',
     EncryptKey=None,
     DecryptKey=None,
     EncryptedSessionKey=None,
     EncryptedData=None,
     Length=None,
     Signature=None,
 ):
     self.CreatorID = CreatorID
     if not self.CreatorID:
         self.CreatorID = my_id.getLocalID()
     self.CreatorID = strng.to_bin(self.CreatorID)
     self.BackupID = strng.to_text(BackupID)
     self.BlockNumber = BlockNumber
     self.LastBlock = bool(LastBlock)
     self.SessionKeyType = SessionKeyType or key.SessionKeyType()
     if EncryptedSessionKey:
         self.EncryptedSessionKey = EncryptedSessionKey
     else:
         if callable(EncryptKey):
             self.EncryptedSessionKey = EncryptKey(SessionKey)
         elif isinstance(EncryptKey, six.string_types):
             self.EncryptedSessionKey = my_keys.encrypt(
                 EncryptKey, SessionKey)
         else:
             self.EncryptedSessionKey = key.EncryptLocalPublicKey(
                 SessionKey)
     if EncryptedData and Length:
         self.Length = Length
         self.EncryptedData = EncryptedData
     else:
         self.Length = len(Data)
         self.EncryptedData = key.EncryptWithSessionKey(SessionKey, Data)
     if Signature:
         self.Signature = Signature
     else:
         self.Signature = None
         self.Sign()
     self.DecryptKey = DecryptKey
     if _Debug:
         lg.out(_DebugLevel, 'new data in %s' % self)
Exemplo n.º 6
0
def encrypt(key_id, inp):
    """
    Encrypt ``inp`` string using given private key ID.

    :param key_id: private key id to be used
    :param inp: raw input string to be encrypted

    Return encrypted string.
    """
    if key_id == 'master':
        return key.EncryptLocalPublicKey(inp)
    if key_id == 'master$%s' % my_id.getGlobalID():
        return key.EncryptLocalPublicKey(inp)
    if key_id == my_id.getGlobalID():
        return key.EncryptLocalPublicKey(inp)
    key_object = known_keys().get(key_id)
    if not key_object:
        lg.warn('key %s is unknown' % key_id)
        return None
    # There is a bug in rsa.encrypt if there is a leading '\0' in the string.
    # See bug report in http://permalink.gmane.org/gmane.comp.python.cryptography.cvs/217
    # So we add a "1" in front now and in decrypt() we will remove it
    atuple = key_object.keyObject.encrypt('1' + inp, "")
    return atuple[0]