python 實現私鑰加密公鑰解密

python 實現私鑰加密公鑰解密

業界廣泛的用法是公鑰用來加密,私鑰來解密,許多人殊不知道也能夠用私鑰加密,公鑰來解密html

基礎知識

對稱加密

非對稱加密

公私鑰的幾個常見格式

圖片來源: https://www.openssl.org/docs/...

圖片描述

使用私鑰加密

待編輯python

使用公鑰解密

參考文檔:linux

https://www.cnblogs.com/masak...
https://www.linuxidc.com/Linu...

python2.7 的實現

from rsa import PublicKey, common, transform, core

# 公鑰格式以下,若公鑰已是 RSAPublicKey 格式,則無需將 pub key 轉換爲 string
PUB_KEY_STRING = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsKfRext58G0buLDabQQNBVWEB1/B62PapiZ2tSiITw/3X4cI00QB6m7dryMqs7pKntUD3MTGeMCj9zwXX0kmqkrA8og0H0eOHQnAeuw671lkSVYnD1YVcICPv+fbJ1JL+DP3RkXuy0+V2iQC2GDQmfgTcKVowU4c+ToQIp0pUBQIDAQAB'

class DecryptByPublicKey(object):
    """
    使用 publib key來解密用primary key加密後生成的base64類型的密文
    返回解密後的數據
    """
    def __init__(self, encrypt_text):
        self.encrypt_text = encrypt_text

    @staticmethod
    def str2key(s):
        # 對字符串解碼, 解碼成功返回 模數和指數
        b_str = base64.b64decode(s)
        if len(b_str) < 162:
            return False
        hex_str = ''
        # 按位轉換成16進制
        for x in b_str:
            h = hex(ord(x))[2:]
            h = h.rjust(2, '0')
            hex_str += h
        # 找到模數和指數的開頭結束位置
        m_start = 29 * 2
        e_start = 159 * 2
        m_len = 128 * 2
        e_len = 3 * 2
        modulus = hex_str[m_start:m_start + m_len]
        exponent = hex_str[e_start:e_start + e_len]
        return modulus,exponent

    @staticmethod
    def f(cipher, PUBLIC_KEY):
        """
        decrypt msg by public key
        """
        public_key = PublicKey.load_pkcs1(PUBLIC_KEY)
        encrypted = transform.bytes2int(cipher)
        decrypted = core.decrypt_int(encrypted, public_key.e, public_key.n)
        text = transform.int2bytes(decrypted)
        if len(text) > 0 and text[0] == '\x01':
            pos = text.find('\x00')
            if pos > 0:
                return text[pos+1:]
            else:
                return None

    def pub_decrypt_with_pubkeystr(self):
        """
        將 base64 編碼的 pub_key 轉成 bio 對象,
        再將bio對象轉換成公鑰對象
        """
        # 將 pub key 轉換爲 string
        # Note: 若公鑰已是 RSAPublicKey 格式,則無需執行這一步 !
        try:
            key = self.str2key(PUB_KEY_STRING) # 將 base64 編碼的公鑰進行拆解,取出模數和指數
            if not key:
                raise Exception, "decode public key falid"
            modulus = int(key[0], 16)
            exponent = int(key[1], 16)
            rsa_pubkey = PublicKey(modulus, exponent) # 根據模數和指數生成 pubkey 對象
            self.pub_key = rsa_pubkey.save_pkcs1()    # 將 pubkey 對象導出爲 RSAPublicKey 格式的公鑰
        except Exception, e:
            assert False, "Invalid public_key"

    
        # 開始解密
        try:
            ret = self.f(self.encrypt_text.decode("base64"), self.pub_key)
        except Exception, e:
            self.error_info = str(e)
            assert False, "Decrypt by public key fails! Invalid encrypt_text"
        return ret

if __name__ == "__main__":
    encrypt_text = 'xxxxxx'  # encrypt_text 是被私鑰加密後的密文
    decrypt = DecryptByPublicKey(encrypt_text)
    result = decrypt.pub_decrypt_with_pubkeystr()
    print result

python3 的實現

# 系統庫
import six
import logging
import coloredlogs
# 第三方庫
import rsa
import base64

p = logging.getLogger()
console_formatter = logging.StreamHandler()
console_formatter.setFormatter(coloredlogs.ColoredFormatter('%(asctime)s - %(module)-14s[line:%(lineno)3d] - %(levelname)-8s: %(message)s'))
p.addHandler(console_formatter)
p.setLevel(logging.DEBUG)

PUB_KEY_STRING = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCsKfRext58G0buLDabQQNBVWEB1/B62PapiZ2tSiITw/3X4cI00QB6m7dryMqs7pKntUD3MTGeMCj9zwXX0kmqkrA8og0H0eOHQnAeuw671lkSVYnD1YVcICPv+fbJ1JL+DP3RkXuy0+V2iQC2GDQmfgTcKVowU4c+ToQIp0pUBQIDAQAB'


class DecryptByPublicKey(object):
    """
        先產生模數因子
        而後生成rsa公鑰
        再使用rsa公鑰去解密傳入的加密str
    """
    def __init__(self, encrypt_text):
        self._encrypt_text = encrypt_text
        self._pub_string_key = PUB_KEY_STRING
        # 使用公鑰字符串求出模數和因子
        self._modulus = None   # 模數
        self._exponent = None  # 因子
        # 使用PublicKey(模數,因子)算出公鑰
        self._pub_rsa_key = None

    def _gen_modulus_exponent(self, s) ->int:
        p.debug("Now base64 decode pub key,return modulus and exponent")
        # 對字符串解碼, 解碼成功返回 模數和指數
        b_str = base64.b64decode(s)
        if len(b_str) < 162:
            return False
        hex_str = b_str.hex()
        # 找到模數和指數的開頭結束位置
        m_start = 29 * 2
        e_start = 159 * 2
        m_len = 128 * 2
        e_len = 3 * 2
        self._modulus = int(hex_str[m_start:m_start + m_len], 16)
        self._exponent = int(hex_str[e_start:e_start + e_len], 16)

    def _gen_rsa_pubkey(self):
        # 將pub key string 轉換爲 pub rsa key
        p.debug("Now turn key string to rsa key")
        try:
            rsa_pubkey = rsa.PublicKey(self._modulus, self._exponent)
            # 賦值到_pub_rsa_key
            self._pub_rsa_key = rsa_pubkey.save_pkcs1()
            # p.debug("self._pub_rsa_key:{}".format(self._pub_rsa_key))
        except Exception as e:
            p.error(e)
            p.error("Invalid public_key")
            raise e

    def decode(self) ->str:
        """
        decrypt msg by public key
        """
        p.debug("Now decrypt msg by public rsa key")
        b64decoded_encrypt_text = base64.b64decode(self._encrypt_text)
        public_key = rsa.PublicKey.load_pkcs1(self._pub_rsa_key)
        encrypted = rsa.transform.bytes2int(b64decoded_encrypt_text)
        decrypted = rsa.core.decrypt_int(encrypted, public_key.e, public_key.n)
        # p.debug('decrypted: {}'.format(decrypted))
        decrypted_bytes = rsa.transform.int2bytes(decrypted)
        # 這裏使用了six庫的iterbytes()方法去模擬python2對bytes的輪詢
        if len(decrypted_bytes) > 0 and list(six.iterbytes(decrypted_bytes))[0] == 1:
            try:
                raw_info = decrypted_bytes[decrypted_bytes.find(b'\x00')+1:]
            except Exception as e:
                p.error(e)
                raise e
        return raw_info.decode("utf-8")

    def decrypt(self) -> str:
        """
        先產生模數因子
        而後生成rsa公鑰
        再使用rsa公鑰去解密
        """
        self._gen_modulus_exponent(self._pub_string_key)
        self._gen_rsa_pubkey()
        ret = self.decode()
        return ret


if __name__ == "__main__":
    encrypt_text = 'xxxxxx'  # encrypt_text 是被私鑰加密後的密文
    result = DecryptByPublicKey(encrypt_text).decrypt()
    p.info(result)
相關文章
相關標籤/搜索