zoukankan      html  css  js  c++  java
  • AES_RSA _加密_调用


    # AES RSA 加密

    from Cryptodome.Cipher import AES
    from Cryptodome import Random
    import rsa
    from binascii import b2a_hex,a2b_hex #python 字符串与16进制互转
    #
    class MyAES():
    def __init__(self,key,mode=AES.MODE_CFB):
    '''
    :param key: 是秘钥
    :param mode: 默认
    '''
    self.key = self.check_key(key)
    self.mode = mode
    self.iv = Random.new().read(AES.block_size)

    def check_key(self,key): #检查key(秘钥)的类型
    '''
    长度是限制
    必须是bate类型
    :return:
    '''


    if isinstance(key,bytes): # 判断类型的方法:判断 key是不是bytes类型
    assert len(key) in [16,24,32] #判断他的长度与是不是16 24 32其中之一,
    return key
    elif isinstance(key,str):
    assert len(key.encode()) in [16,24,32]
    return key.encode()
    else:
    print('秘钥必须是str或者是bytes') #这个就比较轻飘飘
    raise Exception('秘钥必须是str或者是bytes') #主动的报错, 抛出异常(更加正式)

    def my_encrypt(self,data): #加密
    if isinstance(data,str): #判断是不是str类型
    data = data.encode()
    mycipher = AES.new(self.key,self.mode,self.iv)
    return b2a_hex(self.iv + mycipher.encrypt(data)).decode()


    def my_decrypt(self,data): #解密
    if isinstance(data,str):
    data = a2b_hex(data.encode())

    mydecrypt = AES.new(self.key,self.mode,data[:16]) #前16位
    return mydecrypt.decrypt(data[16:]).decode() #后16位

    class MyRSA(): #非对称加密
    def __init__(self,rsa_n,rsa_e='10001'):
    self.n = int(rsa_n,16) #进来一个秘钥
    self.e = int(rsa_e,16) #自带的加密类型
    self.pubkey = rsa.PublicKey(self.n,self.e) #进行混合杂交


    def my_encrypt(self,data):
    if isinstance(data,str): #判断data 是不是str类型
    data = data.encode()
    encry_text = rsa.encrypt(data,self.pubkey)
    return b2a_hex(encry_text).decode()


    这个py文件的名字叫 crypto_model

    *******************************************************************************************************
    #调用
    #   AES    RSA   加密
    #所以要用下面的方法调用
    from crypto_model import MyAES,MyRSA


    my_aes = MyAES('hello world haha') #公钥
    a = my_aes.my_encrypt('版帅长风')
    print(a) #打印加密的

    b = my_aes.my_decrypt(a)
    print(b) #打印解密的

    rsa_n = '8d7e6949d411ce14d7d233d7160f5b2cc753930caba4d5ad24f923a505253b9c39b09a059732250e56c594d735077cfcb0c3508e9f544f101bdf7e97fe1b0d97f273468264b8b24caaa2a90cd9708a417c51cf8ba35444d37c514a0490441a773ccb121034f29748763c6c4f76eb0303559c57071fd89234d140c8bb965f9725'
    my_rsa = MyRSA(rsa_n)
    c = my_rsa.my_encrypt("徐鹏曾鸣鸿")
    print(c) #打印


    crypto  ['krɪptəʊ]  详细X
    基本翻译
    n. 秘密赞同者;秘密党员
    网络释义
    crypto: 加密
    crypto chip: 密码芯片
    crypto terminal: 密码终端
  • 相关阅读:
    【转】IBatis.Net项目数据库SqlServer迁移至Oracle
    【转】远程桌面 剪切板复制文件失效解决方法
    为什么越学反而越蠢?碎片化学习是个骗局
    Spring MVC起步
    [1-1] 把时间当做朋友(李笑来)Chapter 1 【心智的力量】 摘录
    Java反射总结
    No enclosing instance of type Demo is accessible. Must qualify the allocation with an enclosing instance of type Demo (e.g. x.new A() where x is an instance of Demo).
    hiberbnate 缓存策略概述
    一分钟看你缺乏哪种维生素
    zrrx笔试题(一)——文件复制&日期天数差
  • 原文地址:https://www.cnblogs.com/yuanjia8888/p/9019784.html
Copyright © 2011-2022 走看看