.net5 RSA
<PackageReference Include="BouncyCastle.NetCore" Version="1.8.8" />
一、后端
public static class RSASignatureEncrypterHelp { /// <summary> /// 生成PEM格式的公钥和密钥 /// </summary> /// <param name="strength">长度</param> /// <returns>Item1:公钥;Item2:私钥;</returns> public static (string publicKey, string privateKey) CreateRSAKey(int strength = 1024) { RsaKeyPairGenerator r = new RsaKeyPairGenerator(); r.Init(new KeyGenerationParameters(new SecureRandom(), strength)); AsymmetricCipherKeyPair keys = r.GenerateKeyPair(); TextWriter privateTextWriter = new StringWriter(); PemWriter privatePemWriter = new PemWriter(privateTextWriter); privatePemWriter.WriteObject(keys.Private); privatePemWriter.Writer.Flush(); TextWriter publicTextWriter = new StringWriter(); PemWriter publicPemWriter = new PemWriter(publicTextWriter); publicPemWriter.WriteObject(keys.Public); publicPemWriter.Writer.Flush(); return (publicTextWriter.ToString(), privateTextWriter.ToString()); } /// <summary> /// RSA解密 /// </summary> /// <param name="privateKey">私钥</param> /// <param name="decryptstring">待解密的字符串(Base64)</param> /// <returns>解密后的字符串</returns> public static string RSADecrypt(this string decryptstring, string privateKey) { using (TextReader reader = new StringReader(privateKey)) { dynamic key = new PemReader(reader).ReadObject(); var rsaDecrypt = new Pkcs1Encoding(new RsaEngine()); if (key is AsymmetricKeyParameter) { key = (AsymmetricKeyParameter)key; } else if (key is AsymmetricCipherKeyPair) { key = ((AsymmetricCipherKeyPair)key).Private; } rsaDecrypt.Init(false, key); //这里加密是true;解密是false byte[] entData = Convert.FromBase64String(decryptstring); entData = rsaDecrypt.ProcessBlock(entData, 0, entData.Length); return Encoding.UTF8.GetString(entData); } } /// 加密 /// </summary> /// <param name="publicKey">公钥</param> /// <param name="encryptstring">待加密的字符串</param> /// <returns>加密后的Base64</returns> public static string RSAEncrypt(this string encryptstring, string publicKey) { using (TextReader reader = new StringReader(publicKey)) { AsymmetricKeyParameter key = new PemReader(reader).ReadObject() as AsymmetricKeyParameter; Pkcs1Encoding pkcs1 = new Pkcs1Encoding(new RsaEngine()); pkcs1.Init(true, key);//加密是true;解密是false; byte[] entData = Encoding.UTF8.GetBytes(encryptstring); entData = pkcs1.ProcessBlock(entData, 0, entData.Length); return Convert.ToBase64String(entData); } } }
二、前端
import { JSEncrypt } from 'jsencrypt';
//new一个对象 let encrypt = new JSEncrypt() //设置公钥 encrypt.setPublicKey(publicKey) //data是要加密的数据,此处不用注意+号,因为rsa自己本身已经base64转码了,不存在+,全部是二进制数据 let result = encrypt.encrypt(password) return result;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?