python,js前后端aes,ras基础加解密方案 前端js: aes.js:https://blog.csdn.net/yingbaoyu/article/details/95761177 ras.js:https://blog.csdn.net/m0_46370117/article/details/120721155
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script src="./aes.js"></script>
<script src="./ras.js"></script>
<script type="text/javascript">
var crypt_key = "047ADGJMQTW0369D";
var crypt_iv = "131b0c8a7a6e072e";
function encrypt(data) {
let aes_key = CryptoJS.enc.Utf8.parse(crypt_key);
let new_iv = CryptoJS.enc.Utf8.parse(crypt_iv);
encrypted = CryptoJS.AES.encrypt(data, aes_key, {
iv: new_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString()
}
function decrypt(data) {
let aes_key = CryptoJS.enc.Utf8.parse(crypt_key);
let aes_iv = CryptoJS.enc.Utf8.parse(crypt_iv);
let baseResult=CryptoJS.enc.Base64.parse(data);
let ciphertext=CryptoJS.enc.Base64.stringify(baseResult);
let decryptResult = CryptoJS.AES.decrypt(ciphertext,aes_key, {
iv: aes_iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
resData = decryptResult.toString(CryptoJS.enc.Utf8).toString();
return resData;
}
var PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOHL4ZVqQoSEw5L2ZRJ4ElU8J9Je06q7\
Bm2k0z9eWptZVLpBn6gqUlx3B+TG7Qe1pVF9MZITJDtf7vO9z5IB/KcCAwEAAQ==";
var PRIVATE_KEY = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA4cvhlWpChITDkvZl\
EngSVTwn0l7TqrsGbaTTP15am1lUukGfqCpSXHcH5MbtB7WlUX0xkhMkO1/u873P\
kgH8pwIDAQABAkEAtwJGvpG3hC0PTwhB92w2f7sYE5wkNxcLKMcVKSFSkuGYQJvs\
XsF2OwQz+GTHg7yrojRxqbmvUwsyUAitP3xDIQIhAP49ElvirfYuJ5nKbeOI6N0p\
HeOuEPdL76e0Pc4huNWJAiEA41xc/yYRi+afoXm4pV0e46JQWGVxachDdeAM+m4q\
5K8CIQDC83W2e9XFppK7PRySk5Nss3C1++4Kk3IL+QxDmlwvcQIgTlaDwHIod5DG\
Dcg349pbC0ng7yQ82JrlC5dgQWV2KAcCIQDycLmOTBZlkgTWFOKZEmdkcS4e1zzf\
l76ALiaGWWsjsA==";
function ras_encrypt(data){
var encrypt = new JSEncrypt();
encrypt.setPublicKey('-----BEGIN PUBLIC KEY-----\n' + PUBLIC_KEY + '\n-----END PUBLIC KEY-----');
var encrypted = encrypt.encrypt(data);
return encrypted;
}
function ras_decrypt(data){
var decrypt = new JSEncrypt();
decrypt.setPrivateKey('-----BEGIN RSA PRIVATE KEY-----\n'+PRIVATE_KEY+'\n-----END RSA PRIVATE KEY-----');
var uncrypted = decrypt.decrypt(data);
return uncrypted;
}
function l1() {
var encrypted = ras_encrypt("大使馆dsaf.';");
console.log(encrypted);
var uncrypted = ras_decrypt(encrypted);
console.log(uncrypted);
}
function l2() {
var encrypted = encrypt("大使馆dsaf.';");
console.log(encrypted);
var uncrypted = decrypt(encrypted);
console.log(uncrypted);
}
</script>
</head>
<body>
<input type="button" id="l1" value="login1" onclick="l1()">
<input type="button" id="l2" value="login2" onclick="l2()">
</body>
</html>
后端python: 安装:pip install pycryptodome
import base64
from Crypto.Cipher import AES
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5 as PKCS1_cipher
PUBLIC_KEY = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOHL4ZVqQoSEw5L2ZRJ4ElU8J9Je06q7\
Bm2k0z9eWptZVLpBn6gqUlx3B+TG7Qe1pVF9MZITJDtf7vO9z5IB/KcCAwEAAQ=="
PRIVATE_KEY = "MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA4cvhlWpChITDkvZl\
EngSVTwn0l7TqrsGbaTTP15am1lUukGfqCpSXHcH5MbtB7WlUX0xkhMkO1/u873P\
kgH8pwIDAQABAkEAtwJGvpG3hC0PTwhB92w2f7sYE5wkNxcLKMcVKSFSkuGYQJvs\
XsF2OwQz+GTHg7yrojRxqbmvUwsyUAitP3xDIQIhAP49ElvirfYuJ5nKbeOI6N0p\
HeOuEPdL76e0Pc4huNWJAiEA41xc/yYRi+afoXm4pV0e46JQWGVxachDdeAM+m4q\
5K8CIQDC83W2e9XFppK7PRySk5Nss3C1++4Kk3IL+QxDmlwvcQIgTlaDwHIod5DG\
Dcg349pbC0ng7yQ82JrlC5dgQWV2KAcCIQDycLmOTBZlkgTWFOKZEmdkcS4e1zzf\
l76ALiaGWWsjsA=="
class DeRasCrypt:
def __init__(self, k1, k2):
self.public_key = k1
self.private_key = k2
def ras_encrypt(self, msg):
public_key = RSA.importKey('-----BEGIN PUBLIC KEY-----\n' + self.public_key + '\n-----END PUBLIC KEY-----')
cipher = PKCS1_cipher.new(public_key)
encrypt_text = base64.b64encode(cipher.encrypt(bytes(msg.encode("utf8"))))
return encrypt_text.decode('utf-8')
def ras_decrypt(self, encrypt_msg):
private_key = RSA.importKey('-----BEGIN RSA PRIVATE KEY-----\n'+self.private_key+'\n-----END RSA PRIVATE KEY-----')
cipher = PKCS1_cipher.new(private_key)
back_text = cipher.decrypt(base64.b64decode(encrypt_msg), 0)
return back_text.decode('utf-8')
msg = "大使馆dsaf.';"
d = DeRasCrypt(PUBLIC_KEY, PRIVATE_KEY)
encrypt_text = d.ras_encrypt(msg)
decrypt_text = d.ras_decrypt(encrypt_text)
print (encrypt_text)
print (decrypt_text)
class DeAesCrypt:
def __init__(self, key, vi):
self.key = key.encode()
self.iv = vi.encode()
def decrypt(self, data):
"""AES-128-CBC解密"""
real_data = base64.b64decode(data)
my_aes = AES.new(self.key, AES.MODE_CBC, self.iv)
decrypt_data = my_aes.decrypt(real_data)
return decrypt_data.decode()[0: -ord(decrypt_data[-1:])]
def add_to_16(self,text):
pad = 16 - len(text.encode('utf-8')) % 16
text = text + pad * chr(pad)
return text.encode('utf-8')
def encrypt(self, data):
"""AES-128-CBC加密"""
text = self.add_to_16(data)
cryptor = AES.new(self.key,AES.MODE_CBC, self.iv)
cipher_text = cryptor.encrypt(text)
return base64.b64encode(cipher_text).decode('utf-8')
d = DeAesCrypt("047ADGJMQTW0369D", "131b0c8a7a6e072e")
encrypt_text = d.encrypt(msg)
decrypt_text = d.decrypt(encrypt_text)
print (encrypt_text)
print (decrypt_text)
直接下载: https://download.csdn.net/download/qq_38778838/79739069
|