$privateKeyFilePath = '-----BEGIN RSA PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJDArkki2k3+xhze
v5emztqFtUhOzVlSveQ5dOLC4nNlLg8r81I9xBaXB7m4rb6lVjLgUuAXKUJ8y7yH
eE02s695cR/ZD0fierrMKMkmTFZIqOVYchn15N8EPz5e8WFirtP+Kaol4jDOaLCP
CSIs8QrQE6kEC7WDBi2/0mCkNI7PAgMBAAECgYB0oZuMODXXZDGyb1PGWFQRRGyl
n0Db+MwiCJ2CXG5jdiHffZUnLbdCUFycKw5rLwK+KXr9LgxDkxQBitHGvQ2XXO8w
v/2MQT658Eb49iQxZfuuhFFN8YIJZ4oFVyQQWXyCRAopyi/B0Eh6LTTTPjC/udPX
MoC0vSZl93Db8KPdyQJBANLPOnmyQmua6za/fFhWl13lsa+Ko73LhGi6dpiBjdpw
j0hp6NV/7yGCvHJFUr0hc9p5/lVJHkeAS/QtxF1IqXMCQQCvyGVL5uXj4didkO50
4HDgpPAgdROCZ6KrrNIf+RnCDn9Ffa1oA+n5cnno4trZVAYLU4yzZ63tQ/Z81ZfS
+h41AkBPXuyyUzaE0zBKTbBghkG5fbj30egyloTE9aefZe/l1clsx0t9zwxW/qU7
FPTA9u5qzNHAhKYc36Y5Sl4LjUcXAkBX50mopEXQKI+Pc/ubHOW1oSWnxYRFERhK
63iEnqgf3+oLUSbXPiXSJUoLiO5SAe+n2FcjHDTg0ry/fnyW95cFAkEAyllpaHTu
V5FPt6J5wZcEUbZoZsxYGmfUEg6HUE8BbGgRWwaem94p668ILaqGtxGKLKgbWoTM
Z8ZM+5cR/+C+yA==
-----END RSA PRIVATE KEY-----';
* 公钥文件的路径
$publicKeyFilePath = '-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQwK5JItpN/sYc3r+Xps7ahbVI
Ts1ZUr3kOXTiwuJzZS4PK/NSPcQWlwe5uK2+pVYy4FLgFylCfMu8h3hNNrOveXEf
2Q9H4nq6zCjJJkxWSKjlWHIZ9eTfBD8+XvFhYq7T/imqJeIwzmiwjwkiLPEK0BOp
BAu1gwYtv9JgpDSOzwIDAQAB
-----END PUBLIC KEY-----';
extension_loaded('openssl') or die('php需要openssl扩展支持');
* 生成Resource类型的密钥,如果密钥文件内容被破坏,openssl_pkey_get_private函数返回false
$privateKey =openssl_pkey_get_private($privateKeyFilePath);
* 生成Resource类型的公钥,如果公钥文件内容被破坏,openssl_pkey_get_public函数返回false
$publicKey =openssl_pkey_get_public($publicKeyFilePath);
($privateKey) or die('密钥不可用');
($publicKey) or die('公钥不可用');
* 原数据
$originalData = 'test';
* 加密以后的数据,用于在网路上传输
$encryptData = '';
echo '原数据为:', $originalData, '
';
///用公钥加密
if (openssl_public_encrypt($originalData,$encryptData, $publicKey)) {
//Uu3FzghBLs3dnWEO8Gc7p1QGf2MH+VMddUk0ui2TQlcKbr+DMKRJfsTiQYh5N9Bu+kEyKG72f90l34rNJAkLruZJtuqRTfZ8vnG2P1UEkW+7063GVPz6o6mfrL46FN4j0mpIP6sAshvvUAGudlVTde2KT7bI8E0EqZU9D7WRKCI=
echo '加密成功,加密后数据(base64_encode后)为:', base64_encode($encryptData), '
';
echo '加密成功,加密后数据(bin2hex后)为:',bin2hex($encryptData), '
';
} else {
die('加密失败');
// $secret = 'iZEksazCdYhkbqM2kRMo9E+oSFCYnTkB0JGOMN5m3ZeJr8ShtrifvHMWQG605pMvO31oalcU8P8jzkEMLyCV0/ATc2AEF6cxwRujl315tB5RClq8aMYhcAlfOTZeuQHJPg3tM98snEVTrVsRJzr94VYcA394yyln+j+44+3wNaWMNSr+7H/H33XmFN9TcE70Jt1AC/PYNLr0an2DNwBtiDt4ttPfHF2iYJqtrH/K/UNxHd5uakpMKX7QRqEYq3Z6d6dCtlv76Ex2SVTWdNofqu7GbMMemo5hEfRf80q8O0o0Kqn3o6cQ13X7aWY/1pGhkfeoC0yauZx4wKrpdmjgd4lCHDlCVgwLiUdG/6U+ckd4CGu7cQtjfy7UKwOJkCqrB+AGbdjaIyiBGdkji39zf2eGJHRzXCVvN3nyRd/AQlwX0sDsICS+R26SNkDmKo8aWBQspzLUkPMZWGyFRy8cRnILYJwpr4Qhq7JKajHQkaAG6RLQ91NsSEhOGMunAQFver4hsk22NRAY5NugT8qOsUarALucT5fwlpDoOEF4lGpF4uLmTGSPMBrptRrPgWSdGs0p/G/pSx9INOgd5X5e/F12GLqIfZIA6JVyQL4E5AQ0S+aZPNdyEiL1Ufz5A5LcILue/PES5ot45LjynTLWZ2lMRiohBeAsEU57j4KCn8QTTiDdRss+qM7UaCrILUrE6C5/PMPOVHxvICsDYmTLY7nfY2M5cH/wPpuPzcbhWlLltk3wmgf34BugVa2mjq8l5x9EoWv+YDXBWqsegL3DpfhMfvAC2Lk1dsiXPpNLgaX+oY6Xy+0T9paCIWs1ZV8A7M2eNWZPMOOiXOZx+l4TTHNrsQCrUTQzCOu+prmerv6QMEKri+X0hfNmBMl9qodlLo3BI1JtDql/g/2qq1Aq8ETkZ9UznLETxwvvqosZjiobhHnKvlH9kaEjsK5XwGX9q4+yJVNAY5IB5DL3uFdnmnUr7GT1o/+8scJQ0WunBWroUYqpb1hr2p+RvzS8PgpA';
// $encryptData = base64_decode($secret);
// var_dump(strlen($encryptData));
///用私钥解密
$decryptData ='';
$crypto = '';
foreach (str_split($encryptData, 128) as $chunk) {
openssl_private_decrypt($chunk,$decryptData, $privateKey);
$crypto .= $decryptData;
echo '解密成功,解密后数据为:',$crypto, '
';
// if (openssl_private_decrypt($encryptData,$decryptData, $privateKey)) {
// echo '解密成功,解密后数据为:',$decryptData, '
';
// } else {
// die('解密成功');
JAVA代码:
public static void main(String[] args) throws Exception {
String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQwK5JItpN/sYc3r+Xps7ahbVITs1ZUr3kOXTiwuJzZS4PK/NSPcQWlwe5uK2+pVYy4FLgFylCfMu8h3hNNrOveXEf2Q9H4nq6zCjJJkxWSKjlWHIZ9eTfBD8+XvFhYq7T/imqJeIwzmiwjwkiLPEK0BOpBAu1gwYtv9JgpDSOzwIDAQAB";
String test = encrypt("test", getPublicKey(publicKey));
System.out.println(test);
String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJDArkki2k3+xhzev5emztqFtUhOzVlSveQ5dOLC4nNlLg8r81I9xBaXB7m4rb6lVjLgUuAXKUJ8y7yHeE02s695cR/ZD0fierrMKMkmTFZIqOVYchn15N8EPz5e8WFirtP+Kaol4jDOaLCPCSIs8QrQE6kEC7WDBi2/0mCkNI7PAgMBAAECgYB0oZuMODXXZDGyb1PGWFQRRGyln0Db+MwiCJ2CXG5jdiHffZUnLbdCUFycKw5rLwK+KXr9LgxDkxQBitHGvQ2XXO8wv/2MQT658Eb49iQxZfuuhFFN8YIJZ4oFVyQQWXyCRAopyi/B0Eh6LTTTPjC/udPXMoC0vSZl93Db8KPdyQJBANLPOnmyQmua6za/fFhWl13lsa+Ko73LhGi6dpiBjdpwj0hp6NV/7yGCvHJFUr0hc9p5/lVJHkeAS/QtxF1IqXMCQQCvyGVL5uXj4didkO504HDgpPAgdROCZ6KrrNIf+RnCDn9Ffa1oA+n5cnno4trZVAYLU4yzZ63tQ/Z81ZfS+h41AkBPXuyyUzaE0zBKTbBghkG5fbj30egyloTE9aefZe/l1clsx0t9zwxW/qU7FPTA9u5qzNHAhKYc36Y5Sl4LjUcXAkBX50mopEXQKI+Pc/ubHOW1oSWnxYRFERhK63iEnqgf3+oLUSbXPiXSJUoLiO5SAe+n2FcjHDTg0ry/fnyW95cFAkEAyllpaHTuV5FPt6J5wZcEUbZoZsxYGmfUEg6HUE8BbGgRWwaem94p668ILaqGtxGKLKgbWoTMZ8ZM+5cR/+C+yA==";
String decrypt = decrypt(test, getPrivateKey(privateKey));
System.out.println(decrypt);
* RSA解密
* @param data
* 待解密数据
* @param privateKey
* 私钥
* @return
public static String decrypt(String data, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] dataBytes = Base64.decodeBase64(data);
int inputLen = dataBytes.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_DECRYPT_BLOCK) {
cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);
} else {
cache = cipher.doFinal(dataBytes, offset, inputLen - offset);
out.write(cache, 0, cache.length);
offset = i * MAX_DECRYPT_BLOCK;
byte[] decryptedData = out.toByteArray();
out.close();
// 解密后的内容
return new String(decryptedData, "UTF-8");
* RSA加密
* @param data
* 待加密数据
* @param publicKey
* 公钥
* @return
public static String encrypt(String data, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
int inputLen = data.getBytes().length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offset = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offset > 0) {
if (inputLen - offset > MAX_ENCRYPT_BLOCK) {
cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);
} else {
cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);
out.write(cache, 0, cache.length);
offset = i * MAX_ENCRYPT_BLOCK;
byte[] encryptedData = out.toByteArray();
out.close();
// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
// 加密后的字符串
return new String(Base64.encodeBase64String(encryptedData));
在linux上生成privateKey,
openssl genrsa -out rsa_private_key.pem 1024
根据privateKey生成publicKey
openssl rsa -in rsa_private_key.pem -out rsa_public_key.pem -pubout
将java私钥或公钥转换成pem格式,php代码:
class entry {
var $pub_key;
function redPukey()
$pubKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJDArkki2k3+xhzev5emztqFtUhOzVlSveQ5dOLC4nNlLg8r81I9xBaXB7m4rb6lVjLgUuAXKUJ8y7yHeE02s695cR/ZD0fierrMKMkmTFZIqOVYchn15N8EPz5e8WFirtP+Kaol4jDOaLCPCSIs8QrQE6kEC7WDBi2/0mCkNI7PAgMBAAECgYB0oZuMODXXZDGyb1PGWFQRRGyln0Db+MwiCJ2CXG5jdiHffZUnLbdCUFycKw5rLwK+KXr9LgxDkxQBitHGvQ2XXO8wv/2MQT658Eb49iQxZfuuhFFN8YIJZ4oFVyQQWXyCRAopyi/B0Eh6LTTTPjC/udPXMoC0vSZl93Db8KPdyQJBANLPOnmyQmua6za/fFhWl13lsa+Ko73LhGi6dpiBjdpwj0hp6NV/7yGCvHJFUr0hc9p5/lVJHkeAS/QtxF1IqXMCQQCvyGVL5uXj4didkO504HDgpPAgdROCZ6KrrNIf+RnCDn9Ffa1oA+n5cnno4trZVAYLU4yzZ63tQ/Z81ZfS+h41AkBPXuyyUzaE0zBKTbBghkG5fbj30egyloTE9aefZe/l1clsx0t9zwxW/qU7FPTA9u5qzNHAhKYc36Y5Sl4LjUcXAkBX50mopEXQKI+Pc/ubHOW1oSWnxYRFERhK63iEnqgf3+oLUSbXPiXSJUoLiO5SAe+n2FcjHDTg0ry/fnyW95cFAkEAyllpaHTuV5FPt6J5wZcEUbZoZsxYGmfUEg6HUE8BbGgRWwaem94p668ILaqGtxGKLKgbWoTMZ8ZM+5cR/+C+yA==";
$pem = chunk_split($pubKey,64,"\n");//转换为pem格式的公钥
$pem = "-----BEGIN RSA PRIVATE KEY-----\n".$pem."-----END RSA PRIVATE KEY-----\n";
var_dump($pem);
$publicKey = openssl_pkey_get_public($pem);
return $publicKey;
$entry = new entry();
$entry->redPukey();
PHP代码:<?php $privateKeyFilePath = '-----BEGIN RSA PRIVATE KEY-----MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJDArkki2k3+xhzev5emztqFtUhOzVlSveQ5dOLC4nNlLg8r81I9xBaXB7m4rb6lVjLgUuAXKUJ8y7yHeE02s695cR/ZD0fierrMKMkmTFZIqOVYchn15N8E
由于Java的RSA加解密一般都是用RSA/ECB/PKCS1PADDING,导致Python一般的RSA加密库的加解密结果与Java的不兼容,Python下目前能与之兼容的RSA的库目前发现的只有一个,就是m2crypto。 这个库目前的问题是在windows系统下不能直接pip安装,需要自己编译,相当复杂。我提供的这个是已经编译好的,带安装文件,可以直接安装使用。
注意:这个编译好的m2crypto库目前只支持python3.8
Requirement:
This exercise is writing a program in Java using JCE which implements message authentication protocol shown as a variant b 9 of lecture notes on Message
Authentication and Hash Functions. You need to implement a program which models activities of two participants, Sender and Verifier.
Example for the program:
SenderSender SenderSender , it needs to do, it needs to do , it needs to do , it needs to do , it needs to do , it needs to do , it needs to do:
• takes a text of the message, in this example I use the message “test the prog.”
• calculates the message digest (hash function) using SHA-1 algorithm;
• generates a pair of RSA private/public keys; and encrypts the produced digest (hash) with a private key;
• passes the original message, encrypted digest and public key to the Verifier.
• recalculates a new digest from the text of the message he has recieved;
• compares these two digests.
If the message is transferrin
So, the program is able to verifier will detect the attack of Adversary, which is placed in between Sender and Verifier and may change either the original message, or encrypted digest, or both
//公钥(注意包括头(-----BEGIN PUBLIC KEY-----)尾(-----END PUBLIC KEY-----)标志)
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDbdq5NjtUEkAQ6wucPuhC0aRvSMsaX3GrhkwsLLdWZnVNVpkJRw
yPFq9HJNuntRw7P9Sb3Tk.
最近遇到一个项目,需要进行RSA的验签,RSA的密钥为2048位即256个字节长。
上游是先对一个文件进行SHA256做hash,得到32字节的摘要,然后进行填充,填充规则为前面两个字节为0x00,0x01,然后填充全FF,接下来是32字节的摘要数据,这样填充之后的总数据长度为256字节,然后使用RSA的私钥对这256字节进行签名,下发下来,下游需要对这个签名进行处理,得到原始摘要,然后跟自己计算的摘要比对,验证通过则合法。
涉及的知识点如下:
一、RSA签名算法的填充方式,常用的有如下三种
* RSA加密解密:私钥解密,公钥加密。
* RSA数字签名-俗称加签验签:私钥加签,公钥验签。
* RSA加密解密:【私钥解密,公钥加密】。RSA数字签名-俗称加签验签:私钥加签,公钥验签。
BouncyCastle:【私钥加密】【公钥解密】
BouncyCastle加签/验签
RSA算法属于非对称加密算法,非对称加密算法需要两个秘钥:公开密钥(publickey)和私有秘钥(privatekey).公开密钥和私有秘钥是一对,如果公开密钥对数据进行加密,只有用对应的私有秘钥才能解密;如果私有秘钥对数据进行加密那么只有用对应的公开密钥才能解密.因为加密解密使用的是两个不同的秘钥,所以这种算法叫做非对称加密算法.简单的说就是公钥加密私钥解密,私钥加密公钥解密
需要给PHP打开OpenSSL模块
* RSA算法属于非对称加密算法,非对称加密算法需要两个秘钥:公
* vectorKey: 加密的向量,16位字符串
public String encrypt(String content, String slatKey, String vectorKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Paddin
CSDN_GongJun:
百度通用文字识别 image format error (216201)错误解决
图吧灿嘛:
springboot中拦截器与跨域配置冲突解决
qq_39128323:
springboot中拦截器与跨域配置冲突解决
zzhorse78:
springboot中拦截器与跨域配置冲突解决
qq_33519314: