python中rsa解密_Python中RSA的加解密

# coding: utf-8

from __future__ import unicode_literals

import base64

import os

import six

from Crypto import Random

from Crypto.PublicKey import RSA

class PublicKeyFileExists(Exception): pass

class RSAEncryption(object):

PRIVATE_KEY_FILE_PATH = None

PUBLIC_KEY_FILE_PATH = None

def encrypt(self, message):

public_key = self._get_public_key()

public_key_object = RSA.importKey(public_key)

random_phrase = 'M'

encrypted_message = public_key_object.encrypt(self._to_format_for_encrypt(message), random_phrase)[0]

# use base64 for save encrypted_message in database without problems with encoding

return base64.b64encode(encrypted_message)

def decrypt(self, encoded_encrypted_message):

encrypted_message = base64.b64decode(encoded_encrypted_message)

private_key = self._get_private_key()

private_key_object = RSA.importKey(private_key)

decrypted_message = private_key_object.decrypt(encrypted_message)

return six.text_type(decrypted_message, encoding='utf8')

def generate_keys(self):

"""Be careful rewrite your keys"""

random_generator = Random.new().read

key = RSA.generate(1024, random_generator)

private, public = key.exportKey(), key.publickey().exportKey()

if os.path.isfile(self.PUBLIC_KEY_FILE_PATH):

raise PublicKeyFileExists('Файл с публичным ключом существует. Удалите ключ')

self.create_directories()

with open(self.PRIVATE_KEY_FILE_PATH, 'w') as private_file:

private_file.write(private)

with open(self.PUBLIC_KEY_FILE_PATH, 'w') as public_file:

public_file.write(public)

return private, public

def create_directories(self, for_private_key=True):

public_key_path = self.PUBLIC_KEY_FILE_PATH.rsplit('/', 1)

if not os.path.exists(public_key_path):

os.makedirs(public_key_path)

if for_private_key:

private_key_path = self.PRIVATE_KEY_FILE_PATH.rsplit('/', 1)

if not os.path.exists(private_key_path):

os.makedirs(private_key_path)

def _get_public_key(self):

"""run generate_keys() before get keys """

with open(self.PUBLIC_KEY_FILE_PATH, 'r') as _file:

return _file.read()

def _get_private_key(self):

"""run generate_keys() before get keys """

with open(self.PRIVATE_KEY_FILE_PATH, 'r') as _file:

return _file.read()

def _to_format_for_encrypt(value):

if isinstance(value, int):

return six.binary_type(value)

for str_type in six.string_types:

if isinstance(value, str_type):

return value.encode('utf8')

if isinstance(value, six.binary_type):

return value

使用KEYS_DIRECTORY = settings.SURVEY_DIR_WITH_ENCRYPTED_KEYS

class TestingEncryption(RSAEncryption):

PRIVATE_KEY_FILE_PATH = KEYS_DIRECTORY + 'private.key'

PUBLIC_KEY_FILE_PATH = KEYS_DIRECTORY + 'public.key'

# django/flask

from django.core.files import File

class ProductionEncryption(RSAEncryption):

PUBLIC_KEY_FILE_PATH = settings.SURVEY_DIR_WITH_ENCRYPTED_KEYS + 'public.key'

def _get_private_key(self):

"""run generate_keys() before get keys """

from corportal.utils import global_elements

private_key = global_elements.request.FILES.get('private_key')

if private_key:

private_key_file = File(private_key)

return private_key_file.read()

message = 'Hello мой friend'

encrypted_mes = ProductionEncryption().encrypt(message)

decrypted_mes = ProductionEncryption().decrypt(message)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值