最近在用python写长连接的东西,两三周前才开始接触python,所以一直在看书,写起代码来有点痛苦,基本上是跪着在摸索的。前一篇博客的protobuf
(之前写android 只是接json,所以没对这个没什么概念)所以只能去github上看readme,readme好长,配合文档看了一个多小时,拉代码下来拉了两个多小时,这几天写python基本上是这个状态….
python
上的RSA其实用起来挺简单的,找到文档之后就很好解决了
简单包了一下,因为服务器是直接要n
,e
的,所以n
, e
我都拿出来了,代码如下
# https://www.dlitz.net/software/pycrypto/api/2.6/
import ast
from Crypto.PublicKey import RSA
from Crypto import Random
from codecs import encode
from Crypto.PublicKey.RSA import construct
from binascii import unhexlify
class RSAUtil(object):
def __init__(self):
self._key = None
self._public_key = None
self._n = None
self._e = None
def generate(self):
random_generator = Random.new().read
self._key = RSA.generate(1024, random_generator)
self._public_key = self._key.publickey()
self._n = self._key.n
self._e = self._key.e
def create(self, modulus, public_exponent):
self._e = int(public_exponent)
self._n = modulus
self._public_key = construct((int(encode(modulus, 'hex'), 16), public_exponent))
def public_key(self):
return self._public_key.exportKey()
def private_key(self):
if self._key is None:
raise ValueError("not private key,func generate() should be call before this")
return self._key.exportKey()
def public_exponent(self):
if self._e is None:
raise ValueError("not private key,func generate() should be call before this")
return self._e
def modulus(self):
if self._n is None:
raise ValueError("not private key,func generate() should be call before this")
return str(self.long_to_bytes(self._n))
def encode(self, data):
if self._public_key is None:
raise ValueError("not private key,func generate() should be call before this")
return self._public_key.encrypt(data, None)[0]
def decode(self, encrypts):
if self._key is None:
raise ValueError("not private key,func generate() should be call before this")
temp = (encrypts,)
return self._key.decrypt(ast.literal_eval(str(temp)))
def long_to_bytes(self, value, endianness='big'):
# https://stackoverflow.com/questions/8730927/convert-python-long-int-to-fixed-size-byte-array
width = value.bit_length()
width += 8 - ((width % 8) or 8)
fmt = '%%0%dx' % (width // 4)
s = unhexlify(fmt % value)
if endianness == 'little':
# see http://stackoverflow.com/a/931095/309233
s = s[::-1]
return s
测试代码:
if __name__ == "__main__":
# from Tool import RSAUtil
t = RSAUtil()
t.generate()
raw = "hello yeshen"
# encode
print raw
transfer = t.encode(raw)
# decode
print transfer
print t.decode(transfer)
# clone
clone = RSAUtil()
clone.create(t.modulus(), t.public_exponent())
transfer_clone = clone.encode(raw)
print transfer_clone
print t.decode(transfer_clone)
输出:
hello yeshen V��Y�j�����8�����yd�C%%
I5��5b�� ��n���S?ƭ#l���/����(�����`����(8�<dMX0�o8I��Å�
hello yeshen V��Y�j�����8�����yd�C%%
I5��5b�� ��n���S?ƭ#l���/����(�����`����(8�<dMX0�o8I��Å�
hello yeshen
from codecs import encode
from Crypto.PublicKey.RSA import construct
from binascii import unhexlify
import rsa
class RSAUtil(object):
def __init__(self):
self._public_key = None
self._private_key = None
self._n = None
self._e = None
def generate(self):
public_key, private_key = rsa.newkeys(1024)
self._public_key = public_key
self._private_key = private_key
self._n = public_key.n
self._e = public_key.e
def create(self, modulus, public_exponent):
self._e = int(public_exponent)
self._n = modulus
self._public_key = construct((int(encode(modulus, 'hex'), 16), public_exponent))
def public_exponent(self):
if self._e is None:
raise ValueError("not private key,func generate() should be call before this")
return self._e
def modulus(self):
if self._n is None:
raise ValueError("not private key,func generate() should be call before this")
return str(self.long_to_bytes(self._n))
def decode(self, encrypts):
if self._private_key is None:
raise ValueError("not private key,func generate() should be call before this")
return rsa.decrypt(encrypts, self._private_key)
def long_to_bytes(self, value, endianness='big'):
# https://stackoverflow.com/questions/8730927/convert-python-long-int-to-fixed-size-byte-array
width = value.bit_length()
width += 8 - ((width % 8) or 8)
fmt = '%%0%dx' % (width // 4)
s = unhexlify(fmt % value)
if endianness == 'little':
# see http://stackoverflow.com/a/931095/309233
s = s[::-1]
return s
文章浏览阅读3.8k次,点赞61次,收藏41次。华为Could API人工智能系列——文本合成MP3音频_api-huacloud.net
文章浏览阅读9.9k次。map的基本使用:map函数一手包办了序列操作,参数传递和结果保存等一系列的操作。from multiprocessing.dummy import Poolpoop = Pool(4) # 4代表电脑是多少核的results = pool.map(爬取函数,网址列表)from multiprocessing.dummy import Pool as ThreadPool..._multiprocessing map
文章浏览阅读773次。1、效果2、应用场景:表单文字对齐、按钮内均匀分布…3、源码<div style="width:400px;background-color:#eee;text-align-last:justify"> 水平均匀分布 </div> <div style="width:400px;background-color:green;text-align:center"> 居中 </div> <div style="_垂直居中 text-align-last:center
文章浏览阅读305次。今日授课目标能够理解SpringBoot的设计初衷,开发环境要求能够搭建SpringBoot的开发工程能够理解SpringBoot的配置文件常见配置ApplactionContext.xml导包配置文件api(new对象)能够使用SpringBoot整合MyBatis,整合Redis进行缓存,整合RestTemplate发送Http请求能够使用SpringBoot进行简单代码测试能够打包部署SpringBoot项目学习今日内容,必备基础知识:Spring的对象ioc容器
文章浏览阅读3w次,点赞56次,收藏92次。IDEA翻译插件Translation的安装,百度翻译的应用ID和秘钥申请下载插件使用方法字体和接口切换开通百度翻译api注意事项大家平时肯定遇到过这种情况,不管从抖音还是快手或者其他地方获得的一碗毒鸡汤,下肚以后心潮澎湃,凌晨一点也要翻开电脑,奋发图强到势不可挡,立志要成为技术大神。可当打开了编程工具,点进代码底层,通篇的陌生单词还是给了自己一盆冷水。嗯,我其实可以看点搞笑的段子。IDEA实时翻译插件Translation,今天他来了,这款翻译插件支持谷歌、有道和百度api接口,使用的时候我们只需_idea百度翻译密钥
文章浏览阅读2.2k次,点赞6次,收藏16次。在一个目录下有多个cpp文件,和头文件main.cpphead.h。_cmakelists.txt
文章浏览阅读6.9k次。仅作为记录,大佬请跳过。运行python程序时,出现UnicodeDecodeError: 'utf-8' codec can't decode bytes in position 1022-1023: unexpected end of data解决方案(博主未使用,多次运行就又好了)转载:传送门在开头输入:#!/usr/bin/env Python# coding=utf-8..._unicodedecodeerror: 'utf-8' codec can't decode bytes in position 1022-1023:
文章浏览阅读5.5k次。【实例简介】华为服务器服务工程师的培训教材,其中针对华为服务器BMC管理模块的配置及使用做了详细的介绍。目录1.BMC介绍2.BMC远程管理功能介绍HUAWEI TECHNOLOGIES CO, TDHuawei ConfidentialHUAWEI目录1.BMC介绍1.1BMC简介1.2BMC功能模块1.3BMC运行环境2.BMC远程管理功能介绍HUAWEI TECHNOLOGIES CO, T..._华为服务器如果通过bmc网络安装操作系统
文章浏览阅读2.4k次。如下面的图片所示,我们在自动化测试中,需要得到view控件的内容,但是因为某些原因,app不是让你轻易得到数据,这里提供一个思路来提取数据,当然不是最好的,大家有好的想法,欢迎指出。思路:长按内容。 在出现复制标签后,点击复制。 从剪切板读取内容。代码: UiObject res0 = new UiObject(new UiSelector().resourc..._android.view.view内容无法提取
文章浏览阅读4.8w次,点赞13次,收藏8次。使用VScode 调试的时候提示Unable to start debugging. Launch options string provided by the project system is invalid. Unable to determine path to debugger. Please specify the "MIDebuggerPath" option.提示这个错误。..._unable to start debugging. launch options string provided by the project sys
文章浏览阅读3.8k次,点赞9次,收藏22次。图像分类和目标检测中的正负样本划分以及架构理解_图像配准数据的正负样本
文章浏览阅读53次。原文地址:http://maoyidao.iteye.com/blog/1663193一个仅仅部署在4台服务器上的服务,每秒向Database写入数据超过100万行数据,每分钟产生超过1G的数据。而每台服务器(8核12G)上CPU占用不到100%,load不超过5。这是怎么做到呢?下面将给你描述这个架构,它的核心是一个高效缓冲区设计,我们对它的要求是:1,该缓存区要尽量简单2,尽量避免..._java高性能框架 dis