<?php declare(strict_types=1); ini_set('display_errors', 'On'); error_reporting(-1); // 生成RSA密钥(公钥&私钥) $key = openssl_pkey_new(); // 不使用options参数,即RSA密钥所有配置均使用缺省(默认)值 $key instanceof OpenSSLAsymmetricKey || exit('生成RSA密钥(公钥&私钥)失败'); // 获取私钥 $private = ''; openssl_pkey_export($key, $private) || exit('获取私钥失败'); // 获取公钥 $details = openssl_pkey_get_details($key); $public = is_array($details) && isset($details['key']) ? (string)$details['key'] : false; is_string($public) || exit('获取公钥失败'); // 将RSA密钥(公钥&私钥)保存为*.pem文件 file_put_contents(__DIR__ . '/rsa_public_key.pem', $public); file_put_contents(__DIR__ . '/rsa_private_key.pem', $private); $bits = is_array($details) && isset($details['bits']) ? (int)$details['bits'] : false; is_int($bits) || exit('获取密钥长度失败'); echo "密钥长度:$bits"; // 密钥长度:2048 //========== 总结 ==========// // 1、生成RSA密钥时,在不指定参数的情况下,默认会生成PKCS#8格式、长度为2048的密钥。
<?php declare(strict_types=1); ini_set('display_errors', 'On'); error_reporting(-1); $publicKeyFile = __DIR__ . '/rsa_public_key.pem'; // 公钥文件路径 $publicKeyContents = file_get_contents($publicKeyFile); // 获取公钥的内容(即字符串) $publicKeyResource = openssl_pkey_get_public($publicKeyContents); // 获取公钥的资源(获取失败返回false) $publicKeyResource === false && exit('openssl_pkey_get_public() is false.'); // 获取密钥长度 $details = openssl_pkey_get_details($publicKeyResource); $details === false && exit('openssl_pkey_get_details() is false.'); $bits = $details['bits']; // 根据密钥长度算出可加密最大数据长度 $max = $bits / 8 - 11; // 说明:可加密最大数据长度受填充标志影响,使用“OPENSSL_PKCS1_PADDING”填充占11字节,使用“OPENSSL_PKCS1_OAEP_PADDING”填充占42字节, // 由于本演示在加密时不指定填充标志(即使用缺省值OPENSSL_PKCS1_PADDING),所以需要减去11才是可加密最大数据长度。 // 要加密的数据(使用超长数据演示) $data = '先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。'; $data .= '然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。'; $data .= '诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。'; $data .= '宫中府中,俱为一体,陟罚臧否,不宜异同。'; $data .= '若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。'; $data .= '侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。'; $data .= '愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。'; $data .= '将军向宠,性行淑均,晓畅军事,试用于昔日,先帝称之曰能,是以众议举宠为督。'; $data .= '愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。'; $data .= '亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。'; $data .= '先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。'; $data .= '侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。'; $data .= '臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。'; $data .= '先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。'; $data .= '后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。'; $data .= '先帝知臣谨慎,故临崩寄臣以大事也。'; $data .= '受命以来,夙夜忧叹,恐托付不效,以伤先帝之明,故五月渡泸,深入不毛。'; $data .= '今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。'; $data .= '此臣所以报先帝而忠陛下之职分也。'; $data .= '至于斟酌损益,进尽忠言,则攸之、祎、允之任也。'; $data .= '愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。'; $data .= '若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。'; $data .= '今当远离,临表涕零,不知所言。'; $encrypt = ''; // 用于保存最终密文的变量 // 按照可加密最大数据长度对数据进行切割,然后分段加密,从而实现超长数据加密 $dataSplit = str_split($data, $max); foreach ($dataSplit as $split) { $splitEncrypt = ''; openssl_public_encrypt($split, $splitEncrypt, $publicKeyResource); // 加密(不指定填充标志,即使用缺省值OPENSSL_PKCS1_PADDING) $encrypt .= $splitEncrypt; } $encrypt = base64_encode($encrypt); echo "明文:$data" . PHP_EOL; // 明文:先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。将军向宠,性行淑均,晓畅军事,试用于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐托付不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。今当远离,临表涕零,不知所言。 echo "密文:$encrypt" . PHP_EOL; // 密文:iYU4ixQezR6bQzAleaNsNTEh87YyO8gD06pSt3JxB7wki2UVWNNjv/DZmwuhyMWR6E6zkPH8eQaL97/ZhBEX943PoHFN+3sAyJuTUgaT3ihSM7wgP2Wj+vctWNJ2McKHoRYNG+jKuUe72HDMUFHFsEUwnzvSM021ZRpkD1sGXhe2O4aA2REbgtfT7zYzbWHV9jtvTI5+GUodZRoxZS2s2yewswUui8rpdaMWPTUIOYsIOB4cKOQtcGCShM2gC3FVDSeMwPMYAuURTJoGngdyN5rJfDcdL0jJ0LYI0tbOaW37gpD/undqhg6ZxdcYVWdhUQGW5zx2h3Q9nvHzzMUg3Yvvn4MeH0qTSuAqO+ZR6bMcWpgPJIzg//lPkH01styongdNrjAz/aXH9hnc5ckc2tiu9SYPz9qVroYhReKNlVk1xAX1E2KI6heGqntm57Iy5cjscXKZFBLeAIzXvS5jl1ovpotNQqwL1WcNfEecK20IJqgtuGnibyfC8UWzw2MBRSHSJdf78QvmJ1c9DlNpQHYPfn+d4WwmIJDNxmzML7s2MGXvtyISUzQqlFa+LRcz3jaavtMxnay0YCWhCK3ufsueBAzWJrTrANpfsEN30JuNlSRmSdtMQLVC/6GuarN+m12AEKl9cxC42mN4MplyM3WqezZM4oRfumeSF0rtynI/5uPaWDHpnsSwXKqBqe2sa5odwGqZD28J9CNakAwt/iZQCfkHN/FhaS4HwQMa/eHR/bVbNUEAnIJSx6WswwjJzyBjKEfP2N4PEnkvzcS0+Y6FALYglA+8j3EWKk7fgEi41fJ2/dR48P4CoTv6oOHgPd91gLEjyec7Ayfmb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neIZpSyg7lSItqwElYriyb7bNt/Xg98G2bVsqiOC8P6KgcPs+uZxx75QpALwas/4UDj67Z8/6pPWwCcgVLbWOI/AMPxx3RfXnV8TQ8ko33gRbT+kAgMXkEKLVGIBlYHaK5dSWquYEtWjnmH69A0kxqrRv3ekHKsEXCTVWr6T1Vk453amhnp+gaaf+noY4FY+gQ/+IdRMaeSRFmfnSLMlYq3mGYoeu71g3Ckl/cawzc+vKAjy//j4A5CUkeb3gS7Of9A6NV3educqOePY6od9Xc7ag/gNN9qOXnLw60x1Uu53jgRR8ZUjO22V0twXFMUWVwdE1dRybT6jRtyIdYWPiAu/kuVn1IUn3hOd7vyFvOCKHAhyhkTjhTYsueEcOIvYu1v52ZXbJq4cOzoSLqMuhPg==
package main import ( "bytes" "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/base64" "encoding/pem" "fmt" "os" "path/filepath" ) func main() { currentPath, err := filepath.Abs(".") if err != nil { panic("获取当前路径出错:" + err.Error()) } // 构造私钥文件的硬盘路径 privateKeyFile := currentPath + "/rsa_private_key.pem" // 打开私钥文件 var file *os.File file, err = os.Open(privateKeyFile) if err != nil { panic("打开私钥文件出错:" + err.Error()) } // 关闭私钥文件 defer func(file *os.File) { err = file.Close() if err != nil { panic("关闭私钥文件出错:" + err.Error()) } }(file) // 读取私钥文件 contents, _ := file.Stat() buffers := make([]byte, contents.Size()) _, err = file.Read(buffers) if err != nil { panic("读取私钥文件出错:" + err.Error()) } // 解析私钥文件 var privateKeyAny any pemDecode, _ := pem.Decode(buffers) // pem解码 privateKeyAny, err = x509.ParsePKCS8PrivateKey(pemDecode.Bytes) // x509解码 if err != nil { panic("解析私钥文件出错:" + err.Error()) } // 断言 privateKey, ok := privateKeyAny.(*rsa.PrivateKey) if !ok { panic("不是合法的私钥文件") } // 密文 ciphertext := "iYU4ixQezR6bQzAleaNsNTEh87YyO8gD06pSt3JxB7wki2UVWNNjv/DZmwuhyMWR6E6zkPH8eQaL97/ZhBEX943PoHFN+3sAy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YWPiAu/kuVn1IUn3hOd7vyFvOCKHAhyhkTjhTYsueEcOIvYu1v52ZXbJq4cOzoSLqMuhPg==" var ciphertextByte []byte ciphertextByte, err = base64.StdEncoding.DecodeString(ciphertext) if err != nil { panic("base64解码出错:" + err.Error()) } privateKeySize := privateKey.Size() // 私钥长度 ciphertextSize := len(ciphertextByte) // 密文长度 var offset = 0 var buffer = bytes.Buffer{} var decrypt []byte for offset < ciphertextSize { endIndex := offset + privateKeySize if endIndex > ciphertextSize { endIndex = ciphertextSize } decrypt, err = rsa.DecryptPKCS1v15(rand.Reader, privateKey, ciphertextByte[offset:endIndex]) if err != nil { panic("rsa解密出错:" + err.Error()) } buffer.Write(decrypt) offset = endIndex } fmt.Println("明文:" + string(buffer.Bytes())) // 明文:先帝创业未半而中道崩殂,今天下三分,益州疲弊,此诚危急存亡之秋也。然侍卫之臣不懈于内,忠志之士忘身于外者,盖追先帝之殊遇,欲报之于陛下也。诚宜开张圣听,以光先帝遗德,恢弘志士之气,不宜妄自菲薄,引喻失义,以塞忠谏之路也。宫中府中,俱为一体,陟罚臧否,不宜异同。若有作奸犯科及为忠善者,宜付有司论其刑赏,以昭陛下平明之理,不宜偏私,使内外异法也。侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必能裨补阙漏,有所广益。将军向宠,性行淑均,晓畅军事,试用于昔日,先帝称之曰能,是以众议举宠为督。愚以为营中之事,悉以咨之,必能使行阵和睦,优劣得所。亲贤臣,远小人,此先汉所以兴隆也;亲小人,远贤臣,此后汉所以倾颓也。先帝在时,每与臣论此事,未尝不叹息痛恨于桓、灵也。侍中、尚书、长史、参军,此悉贞良死节之臣,愿陛下亲之信之,则汉室之隆,可计日而待也。臣本布衣,躬耕于南阳,苟全性命于乱世,不求闻达于诸侯。先帝不以臣卑鄙,猥自枉屈,三顾臣于草庐之中,咨臣以当世之事,由是感激,遂许先帝以驱驰。后值倾覆,受任于败军之际,奉命于危难之间,尔来二十有一年矣。先帝知臣谨慎,故临崩寄臣以大事也。受命以来,夙夜忧叹,恐托付不效,以伤先帝之明,故五月渡泸,深入不毛。今南方已定,兵甲已足,当奖率三军,北定中原,庶竭驽钝,攘除奸凶,兴复汉室,还于旧都。此臣所以报先帝而忠陛下之职分也。至于斟酌损益,进尽忠言,则攸之、祎、允之任也。愿陛下托臣以讨贼兴复之效,不效,则治臣之罪,以告先帝之灵。若无兴德之言,则责攸之、祎、允等之慢,以彰其咎;陛下亦宜自谋,以咨诹善道,察纳雅言,深追先帝遗诏,臣不胜受恩感激。今当远离,临表涕零,不知所言。 }
Copyright © 2024 码农人生. All Rights Reserved