Python3 RSA 长字符串分段加密解密

import base64

from Crypto import Random
from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
from Crypto.PublicKey import RSA


class RsaCode:
    rsa_private_key = """-----BEGIN RSA PRIVATE KEY-----
    MIICXAIBAAKBgQDvluFNiF8IrIsddK0OXBAvVBJH11OKvy9er1tRGn9yEJoHCJY3
    EU/xz2LasCK8AwgRIqGJbvDBgRa70c3QT9j+wPqNqqJCSoSEKifnDUk1RgUReJT6
    iqWaJyfM+WM3aHnKl61RZL4NV5qKe4CHMtaH/JtBCC/JzpuFER1P1IhCtQIDAQAB
    AoGAaFYQb68/k4twWbeB1YsKEVJPU7HV08pGWrmKztr3PTk1mnKG2BxV8DwcFJg3
    yCCZ1rx6FFuXxOzudYR8WIctO4wdsEbFky/cEGsfc6JJjiktmZaQ7MvobGNwnoFJ
    QvRxDd+5uD87JE19iBSgUpLVtXbv+pZxSpD70vitnMdSctECQQD66Z5HsuC8DUPu
    OLQHNN4ra5Op179Xlq7LiEFW4GaVgonw24kiLX23c7CK7295Rgxct1fwQKyuU9br
    n2uj8toDAkEA9HJ85BWlm2OfUm6VI3Q99rjlpCnhRyz70+sEtf7if1SpctVxNTkX
    UOnXlpPTohjAHNhzh9fa1hh/ySH9sRMu5wJAa//8uh3br/YBxFsx2lw+OPBQGe4c
    lSXtzPu0LCHg5f/PQhYs28I696jbV6IiGFA3Z/0e4/HiohLCUp9HJMWWYwJACE53
    pfyCUyRwfomZccn6bQ7dZtWxfQyvRgU/dLvDkJYc5/UO0sMs4qf/lnNRhrmWlaRZ
    UK1qF0pf1ULdbw360wJBAObrYopW2kvIlE09j9SEgNtgVsmfZlf85c4EAZrFJP/T
    8nMNKQGo92Gd3HvbjJ+ZBOP1IFt+FDAsXeSLWLAwJrg=
    -----END RSA PRIVATE KEY-----"""

    rsa_public_key = """-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvluFNiF8IrIsddK0OXBAvVBJH
    11OKvy9er1tRGn9yEJoHCJY3EU/xz2LasCK8AwgRIqGJbvDBgRa70c3QT9j+wPqN
    qqJCSoSEKifnDUk1RgUReJT6iqWaJyfM+WM3aHnKl61RZL4NV5qKe4CHMtaH/JtB
    CC/JzpuFER1P1IhCtQIDAQAB
    -----END PUBLIC KEY-----
    """

    def encrypt(self, msg):
        msg = msg.encode('utf-8')
        rsakey = RSA.importKey(self.rsa_public_key)
        cipher = Cipher_pkcs1_v1_5.new(rsakey)
        cipher_text = base64.b64encode(cipher.encrypt(msg))
        return cipher_text

    def decrypt(self, cipher_text):
        rsakey = RSA.importKey(self.rsa_private_key)
        cipher = Cipher_pkcs1_v1_5.new(rsakey)
        random_generator = Random.new().read
        text = cipher.decrypt(base64.b64decode(cipher_text), random_generator)
        return text.decode('utf8')

    def long_encrypt(self, msg):
        msg = msg.encode('utf-8')
        length = len(msg)
        default_length = 117
        # 公钥加密
        pubobj = Cipher_pkcs1_v1_5.new(RSA.importKey(self.rsa_public_key))
        # 长度不用分段
        if length < default_length:
            return base64.b64encode(pubobj.encrypt(msg))
        # 需要分段
        offset = 0
        res = []
        while length - offset > 0:
            if length - offset > default_length:
                res.append(pubobj.encrypt(msg[offset:offset + default_length]))
            else:
                res.append(pubobj.encrypt(msg[offset:]))
            offset += default_length
        byte_data = b''.join(res)
        return base64.b64encode(byte_data)

    def long_decrypt(self, msg):
        msg = base64.b64decode(msg)
        print(msg)
        length = len(msg)
        default_length = 128
        # 私钥解密
        priobj = Cipher_pkcs1_v1_5.new(RSA.importKey(self.rsa_private_key))
        # 长度不用分段
        if length < default_length:
            return b''.join(priobj.decrypt(msg, b'xyz'))
        # 需要分段
        offset = 0
        res = []
        while length - offset > 0:
            if length - offset > default_length:
                res.append(priobj.decrypt(msg[offset:offset + default_length], b'xyz'))
            else:
                res.append(priobj.decrypt(msg[offset:], b'xyz'))
            offset += default_length

        return b''.join(res).decode('utf8')


message = """
话说凤姐儿自贾琏送黛玉往扬州去后,心中实在无趣,每到晚间不过同平儿说
笑一回,就胡乱睡了。这日夜间和平儿灯下拥炉,早命浓熏绣被,二人睡下,屈指
计算行程该到何处。不知不觉已交三鼓,平儿已睡熟了。凤姐方觉睡眼微蒙,恍惚
只见秦氏从外走进来,含笑说道:“婶娘好睡!我今日回去,你也不送我一程。因
娘儿们素日相好,我舍不得婶娘,故来别你一别。还有一件心愿未了,非告诉婶娘,
别人未必中用。”凤姐听了,恍惚问道:“有何心愿?只管托我就是了。”秦氏道:
“婶娘,你是个脂粉队里的英雄,连那些束带顶冠的男子也不能过你。你如何连两
句俗语也不晓得?常言:‘月满则亏,水满则溢。’又道是:‘登高必跌重。’如
今我们家赫赫扬扬,已将百载,一日倘或乐极生悲,若应了那句‘树倒猢狲散’的
俗语,岂不虚称了一世诗书旧族了?”凤姐听了此话,心胸不快,十分敬畏,忙问
道:“这话虑的极是,但有何法可以永保无虞?”秦氏冷笑道:“婶娘好痴也!‘否
极泰来’,荣辱自古周而复始,岂人力所能常保的?但如今能于荣时筹画下将来衰
时的世业,亦可以常远保全了。即如今日诸事俱妥,只有两件未妥,若把此事如此
一行,则后日可保无患了。”
"""

test = RsaCode()
res_en = test.long_encrypt(message)
print('res_en', res_en)
res_de = test.long_decrypt(res_en)
print('res_de', res_de)

PHP RSA 非对称加密解密

openssl genrsa -out test.key 1024
openssl rsa -in test.key -pubout -out test_pub.key

私钥

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDvluFNiF8IrIsddK0OXBAvVBJH11OKvy9er1tRGn9yEJoHCJY3
EU/xz2LasCK8AwgRIqGJbvDBgRa70c3QT9j+wPqNqqJCSoSEKifnDUk1RgUReJT6
iqWaJyfM+WM3aHnKl61RZL4NV5qKe4CHMtaH/JtBCC/JzpuFER1P1IhCtQIDAQAB
AoGAaFYQb68/k4twWbeB1YsKEVJPU7HV08pGWrmKztr3PTk1mnKG2BxV8DwcFJg3
yCCZ1rx6FFuXxOzudYR8WIctO4wdsEbFky/cEGsfc6JJjiktmZaQ7MvobGNwnoFJ
QvRxDd+5uD87JE19iBSgUpLVtXbv+pZxSpD70vitnMdSctECQQD66Z5HsuC8DUPu
OLQHNN4ra5Op179Xlq7LiEFW4GaVgonw24kiLX23c7CK7295Rgxct1fwQKyuU9br
n2uj8toDAkEA9HJ85BWlm2OfUm6VI3Q99rjlpCnhRyz70+sEtf7if1SpctVxNTkX
UOnXlpPTohjAHNhzh9fa1hh/ySH9sRMu5wJAa//8uh3br/YBxFsx2lw+OPBQGe4c
lSXtzPu0LCHg5f/PQhYs28I696jbV6IiGFA3Z/0e4/HiohLCUp9HJMWWYwJACE53
pfyCUyRwfomZccn6bQ7dZtWxfQyvRgU/dLvDkJYc5/UO0sMs4qf/lnNRhrmWlaRZ
UK1qF0pf1ULdbw360wJBAObrYopW2kvIlE09j9SEgNtgVsmfZlf85c4EAZrFJP/T
8nMNKQGo92Gd3HvbjJ+ZBOP1IFt+FDAsXeSLWLAwJrg=
-----END RSA PRIVATE KEY-----

公钥

-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvluFNiF8IrIsddK0OXBAvVBJH
11OKvy9er1tRGn9yEJoHCJY3EU/xz2LasCK8AwgRIqGJbvDBgRa70c3QT9j+wPqN
qqJCSoSEKifnDUk1RgUReJT6iqWaJyfM+WM3aHnKl61RZL4NV5qKe4CHMtaH/JtB
CC/JzpuFER1P1IhCtQIDAQAB
-----END PUBLIC KEY-----

方法

function rsa()
{

	$private_key = "-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDvluFNiF8IrIsddK0OXBAvVBJH11OKvy9er1tRGn9yEJoHCJY3
EU/xz2LasCK8AwgRIqGJbvDBgRa70c3QT9j+wPqNqqJCSoSEKifnDUk1RgUReJT6
iqWaJyfM+WM3aHnKl61RZL4NV5qKe4CHMtaH/JtBCC/JzpuFER1P1IhCtQIDAQAB
AoGAaFYQb68/k4twWbeB1YsKEVJPU7HV08pGWrmKztr3PTk1mnKG2BxV8DwcFJg3
yCCZ1rx6FFuXxOzudYR8WIctO4wdsEbFky/cEGsfc6JJjiktmZaQ7MvobGNwnoFJ
QvRxDd+5uD87JE19iBSgUpLVtXbv+pZxSpD70vitnMdSctECQQD66Z5HsuC8DUPu
OLQHNN4ra5Op179Xlq7LiEFW4GaVgonw24kiLX23c7CK7295Rgxct1fwQKyuU9br
n2uj8toDAkEA9HJ85BWlm2OfUm6VI3Q99rjlpCnhRyz70+sEtf7if1SpctVxNTkX
UOnXlpPTohjAHNhzh9fa1hh/ySH9sRMu5wJAa//8uh3br/YBxFsx2lw+OPBQGe4c
lSXtzPu0LCHg5f/PQhYs28I696jbV6IiGFA3Z/0e4/HiohLCUp9HJMWWYwJACE53
pfyCUyRwfomZccn6bQ7dZtWxfQyvRgU/dLvDkJYc5/UO0sMs4qf/lnNRhrmWlaRZ
UK1qF0pf1ULdbw360wJBAObrYopW2kvIlE09j9SEgNtgVsmfZlf85c4EAZrFJP/T
8nMNKQGo92Gd3HvbjJ+ZBOP1IFt+FDAsXeSLWLAwJrg=
-----END RSA PRIVATE KEY-----";

	$public_key = "-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDvluFNiF8IrIsddK0OXBAvVBJH
11OKvy9er1tRGn9yEJoHCJY3EU/xz2LasCK8AwgRIqGJbvDBgRa70c3QT9j+wPqN
qqJCSoSEKifnDUk1RgUReJT6iqWaJyfM+WM3aHnKl61RZL4NV5qKe4CHMtaH/JtB
CC/JzpuFER1P1IhCtQIDAQAB
-----END PUBLIC KEY-----
";


	//获取所有支持算法,cipher 就是密码,算法计算的意思
	//$methods = openssl_get_cipher_methods();
	//var_dump($methods);

	$data = "原始数据为: 用私钥加密origin data1";
	//$method = "AES-128-CBC";

	//通过私钥加密,生成$crypted;
	openssl_private_encrypt($data, $crypted, $private_key);

	// 由于php 进行openssl_public_encrypt 加密后返回的是二进制数据,需要对其返回的加密后的数据进行二进制16进制编码base64_encode才可以显示,$crypted为加密后的串
	$crypted = base64_encode($crypted);

	echo "私钥加密后的结果为:" . $crypted . "<br/>";

	//相应的:加密后生产的16进制加密字符串需要进行base64_decode进行解密后在进行openssl_private_decrypt
	$crypted = base64_decode($crypted);


	openssl_public_decrypt($crypted, $decrypted, $public_key);

	echo "用公钥解密的结果为" . ($decrypted) . "<br/>";
	echo "===================我是分割线=============="."<br/>";
	$data = "用公钥加密origin data2\n";
	//$method = "AES-128-CBC";

	//通过公钥加密,生成$crypted;
	openssl_public_encrypt($data, $crypted, $public_key);

	// 由于php 进行openssl_public_encrypt 加密后返回的是二进制数据,需要对其返回的加密后的数据进行二进制16进制编码base64_encode才可以显示,$crypted为加密后的串
	$crypted = base64_encode($crypted);

	echo "公钥加密后的结果为:" . $crypted . "<br/>";

	//相应的:加密后生产的16进制加密字符串需要进行base64_decode进行解密后在进行openssl_private_decrypt
	$crypted = base64_decode($crypted);

	openssl_private_decrypt($crypted, $decrypted, $private_key);

	echo "用私钥解密的结果为" . ($decrypted) . "<br/>";
}

Electron 报错 require is not defined

var electron = require('electron');
var app = electron.app
var path = require('path');

var BrowserWindow = electron.BrowserWindow

var mainWindow = null;
app.on('ready',function(){
    mainWindow = new BrowserWindow({
        width:600,
        height:600,
        webPreferences: {
            nodeIntegration: true
            }
    })
    // mainWindow.loadFile('index.html');
    mainWindow.loadURL(path.join('file:',__dirname,'index.html'))
    console.log('file:',__dirname,'index.html')
    mainWindow.on('closed',()=>{
        mainWindow=null; 
    })
    mainWindow.webContents.openDevTools();
})

在主进程 main.js 创建窗口的时候添 下面代码即可

webPreferences: {nodeIntegration: true}

Electron 热更新和electron-builder 打包

http://www.weilai.info/rear_end/339.html

Shell 脚本记录

监控服务器状态发送给 Incoming Webhooks

#!/bin/bash  

STATE=`top -b -n 1 | grep 'B Mem'`
URL='https://hook.bearychat.com/=bwGFg/incoming/xxxxxxxxxx'
TIME=$(date "+%Y-%m-%d %H:%M:%S")
curl ${URL} -H 'Content-Type: application/json' -d '{"text": "腾讯云服务器状态","attachments": [{"title": "内存 '"${TIME}"'","text":"'"${STATE}"'","color": "#ffa500"}]}'

清理 buff/cache

sync && echo 1 > /proc/sys/vm/drop_caches
sync && echo 2 > /proc/sys/vm/drop_caches
sync && echo 3 > /proc/sys/vm/drop_caches

开机执行 shell 脚本

vim /etc/rc.d/rc.local
# 添加
sh /root/sh/auto.sh

在 centos 7 中,/etc/rc.d/rc.local 文件的权限被降低了,开机的时候执行在自己的脚本是不能起动一些服务的,执行下面的命令可以文件标记为可执行的文件

chmod +x /etc/rc.d/rc.local 

selenium 内嵌窗口定位滚动

Selenium 配合 Chromedriver 爬取艺龙评价时,存在内嵌窗口,直接 JS 无法滚动,这里使用如下操作方法

if spider.name == 'elong':
	for i in range(2):
		rand = random.uniform(1, 4)
		time.sleep(rand)
		drag = self.driver.find_element_by_xpath("//li[@class='s_bdt'][last()-1]")
		self.driver.execute_script("arguments[0].scrollIntoView();", drag)
		time.sleep(2)
else:
	for i in range(2):
		rand = random.uniform(1, 4)
		time.sleep(rand)
		js = "window.scrollTo(0,document.body.scrollHeight)"
		self.driver.execute_script(js)
print('Chrome driver success...')