java RSA私钥加密、python公钥解密
python RSA公钥加密、java私钥解密
public static final String RSA = "RSA";
public static final String RSA_ECB_NO_PADDING = "RSA/ECB/PKCS1Padding";
public static final String AES_ECB_PKCS_5_PADDING = "AES/ECB/PKCS5Padding";
public static final String AES = "AES";
public static String rsaPrivateEncrypt(String privateKey, String data) {
if (StringUtils.isBlank(data)) {
return "";
}
try {
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(RSA_ECB_NO_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] bytes = cipher.doFinal(data.getBytes());
return Base64.encodeBase64String(bytes);
} catch (Exception e) {
log.error("rsaPrivateEncrypt error", e);
}
return null;
}
public static String rsaPrivateDecrypt(String privateKey, String data) {
if (StringUtils.isBlank(data)) {
return "";
}
try {
byte[] dataBytes = Base64.decodeBase64(data);
KeyFactory keyFactory = KeyFactory.getInstance(RSA);
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
Cipher cipher = Cipher.getInstance(RSA_ECB_NO_PADDING);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(dataBytes));
} catch (Exception e) {
log.error("rsaPrivateDecrypt error", e);
}
return null;
}
public static String aesEncrypt(String data, String key) {
if (StringUtils.isBlank(data)) {
return "";
}
try {
Cipher cipher = Cipher.getInstance(AES_ECB_PKCS_5_PADDING);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), AES));
return Base64.encodeBase64String(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
} catch (Exception e) {
log.error("aesDecryptData error", e);
}
return "";
}
public static String aesDecrypt(String data, String key) {
if (StringUtils.isBlank(data)) {
return "";
}
try {
Cipher cipher = Cipher.getInstance(AES_ECB_PKCS_5_PADDING);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), AES));
return new String(cipher.doFinal(Base64.decodeBase64(data)));
} catch (Exception e) {
log.error("aesDecryptData error", e);
}
return "";
}
import base64
import json
import random
import time
from urllib import parse
import requests
from Crypto.Cipher import AES, PKCS1_v1_5
from Crypto.PublicKey import RSA
from rsa import PublicKey, transform, core
def aes_encrypt(key, message):
padding = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
txt = AES.new(key.encode(encoding="utf8"), AES.MODE_ECB).encrypt(padding(message).encode(encoding="utf8"))
return base64.b64encode(txt)
def aes_decrypt(key, message):
unpadding = lambda s: s[:-ord(s[len(s) - 1:])]
base64_decrypted = base64.decodebytes(message.encode(encoding='utf-8'))
string = str(AES.new(key.encode(encoding="utf8"), AES.MODE_ECB).decrypt(base64_decrypted), encoding='utf-8')
return unpadding(string)
def decrypt_by_public_key(publickey, message):
rsa_public_key = PublicKey.load_pkcs1_openssl_der(base64.b64decode(publickey))
text_str = transform.bytes2int(base64.b64decode(message))
final_text = transform.int2bytes(core.decrypt_int(text_str, rsa_public_key.e, rsa_public_key.n))
final_qr_code = final_text[final_text.index(0) + 1:]
return final_qr_code.decode()
def encrypt_by_public_key(publickey, message):
byte = PKCS1_v1_5.new(RSA.importKey(base64.b64decode(publickey))).encrypt(message.encode(encoding="utf8"))
return base64.b64encode(byte).decode(encoding="utf8")