des rsa加密解密,与c++混合使用. 第一步分python

一次偶然的机会需要开发服务器和QT版本的客户端,在其中需要用到常见的加密解密算法,其中纠结了许久,最后项目做完后做出一些总结。

python 下使用Des库非常的简单

1.Des加密解密 


from pyDes import *

import binascii

ret="hello world !" #需要加密的字符串

secret_key = "12345678" #密钥

iv = secret_key  #向量

k = des(secret_key, CBC, iv, pad=None,padmode=PAD_PKCS5)#得到DES 对象

en = k.encrypt(ret) #加密所需字符串 得到一个二进制的加密en

ret =binascii.b2a_hex(en) #将二进制转化为传输需要的16进制hex 文本,用于网络传输

  2.Des 解密

from pyDes import *

import binascii

#1.将上上面得到的hex 文本进行转化为二进制

data=binascii.a2b_hex(ret)

secret_key = "12345678"

iv = secret_key

k = des(secret_key, CBC, iv, pad=None,padmode=PAD_PKCS5)

src=k.decrypt(data) #得到解码后的文件

rsa加密解密运算:

rsa 生成密钥

import rsa

# 生成密钥
(pubkey, privkey) = rsa.newkeys(1024)

# =================================
# 密钥保存
# =================================
with open('public.pem','w+') as f:
    f.write(pubkey.save_pkcs1().decode())

with open('private.pem','w+') as f:
    f.write(privkey.save_pkcs1().decode())

rsa 应用

import rsa 
# from Crypto.PublicKey import RSA as rsa
# from Crypto.Cipher import PKCS1_v1_5
import binascii
import json

s_privkey=b'-----BEGIN RSA PRIVATE KEY-----\n\
MIICYQIBAAKBgQCSgVkZKHBVA/Fwdio4N2H1tjXTuwUW1ep9Vdzb3alBGJY575i2\n\
N4mVxjvgCTyZjnj0s9QCA+I7mvFpKShTglHHdfEOvO+7T/p246Iq9VGfwstYfdTE\n\
CNMiMzX1HdEfcZccEe65IJc8OfeoDHVPHvaGkxfU00kJA/rcdswJOn9hvQIDAQAB\n\
AoGAMRs4ES2z0Vs7/1UsO6GcrS6BBlJGl2vmaFJycCPxSoeCOzMrPdCJxnEmuqZ9\n\
8H6H3oW0R2Sj3RTHXFdWFi+Xg9PYOEiNg2/HrcvPBG0+/abreDpvIV3Xj6eqEK8i\n\
WCS0aQqfehW+QIfITdU4jRI1XjDdbHDAHY+7/ZsKdtyq0cECRQC4aWst/K7N0GMZ\n\
DTCx1KJNGTzII8fzPiCvkEaT/22UY/GH9muDVyXDgThwez3c+Wtax+TmuScOe06R\n\
0J86bj8X/kIATQI9AMtg1TGuLSlr58PuSfZLe7psBsv1UBfaXde43mtte1jaVpyF\n\
6i9Tprw4S+Y7hUqfUxbE6QL6anEpIyQfMQJFALRrPtcRHhxyQmHiC8AP3nI7vMG6\n\
cAdEw0nAh6yH/DdvyIBS0EEBZArwQ72YdZ/ZzfRVGVe50cJTVT1HtGDAN65LrVsJ\n\
AjxmhoQzLUkbDoih3yht/xN6oSaz/xDHVY/lRg7qPSDnY/oxodiyzwrI6+BnSi4h\n\
tjveyJlmpPKv5TsP5KECRQCnQ4/U07F91Vr8X33x1LlT4h66SRZm+PRodaNw0gIS\n\
Nps6pDX5CrA0FnLypO878JDTL0q/0mUx5otsfJo3IGF0jCaywQ==\n\
-----END RSA PRIVATE KEY-----\n'

s_pulicKey=b'-----BEGIN RSA PUBLIC KEY-----\n\
MIGJAoGBAKmV1KA9fjRrySfFbgozIDD0n19rL1pEUmM7wdho5qjl2w+wMlpxlqeO\n\
YdbdMwtufbOekFQ4IcffORQfIynBpPeR12F5gQkC0sG1O8HkKdiDS88gcpVVKX1D\n\
O77gW9p45Na5ZvajSYMo+4SIQeGWn5y8rCUfNfi3Vh/HeterMiHNAgMBAAE=\n\
-----END RSA PUBLIC KEY-----'



class  jiami_rsa:
    def __getKey(self): 
        try:
            with open('common_static/key/public.pem','r') as f:
            #   print(f.read())
            #   print("-----------")
              self.pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
              #print("pubkey"+str(self.pubkey))
        except :
            self.pubkey=None
        try:
            with open('common_static/key/private.pem','r') as f:
                # printf(f.read().encode())
                self.privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
        except :
            self.privkey=None
        # with open('common_static/key/public.pem','r') as f:
        #     self.pubkey = rsa.PublicKey.load_pkcs1(f.read().encode())
        # with open('common_static/key/private.pem','r') as f:
        #     self.privkey = rsa.PrivateKey.load_pkcs1(f.read().encode())
        return (self.pubkey,self.privkey)


    def __init__(self):
        # print("init")
        # self.__getKey()# 从文件见中获取密钥
        # print(s_privkey)
        self.privkey=rsa.PrivateKey.load_pkcs1(s_privkey)#直接从上面字符串中获取密钥

        self.pubkey=rsa.PublicKey.load_pkcs1(s_pulicKey)
        # print("self.pubkey"+str(self.pubkey))
        # print("self.privkey"+str(self.privkey))

    def __jiami(self,message=None):
        msg1=""
        # print("self"+str(self.pubkey))
        if self.pubkey is not None:
            try:
               msg1 = rsa.encrypt(message.encode(), self.pubkey) #公钥加密
            #    print(str(msg1))
            except:
                print("__jiami except")
                return None
        #print(self.pubkey)
        #print("out"+str(msg1))
        return msg1
    
    def __jiemi(self,msg=None):
        message=None 
        # print("self"+str(self.privkey))
        if self.privkey is not None:
            try:
                message = rsa.decrypt(msg, self.privkey).decode()#私钥解密
            except:
                print(" __jiemi except")
                message=None
        return message
    
    def RcvfromClient(self,message=None):
        return self.__jiemi(message)
    def SendtoClient(self,message):
        # print(message)
        return  self.__jiami(message)

    

if __name__=="__main__":
    myrsa=jiami_rsa()
    msg=myrsa.SendtoClient("this is test")
    
    jmsg=myrsa.RcvfromClient(msg)
    
    print(msg)

 rsa 验签

message = '这是重要指令:...'

#print(privkey)
# 公司经理私钥签名
message2=''
crypto_email_text = rsa.sign(message.encode(), privkey, 'SHA-256')
try:
    message2=rsa.verify(message.encode(), crypto_email_text, pubkey)
except :
   print('error')
    #业务员同时收到指令明文、密文,然后用公钥验证,进行身份确认
  
print(message2)

 

你可能感兴趣的:(python,rsa,des,linux)