public byte[] Encrypt(Stream msg, string key = null) { try { byte[] encryptedData; int blockSize = 0; using (var rsa = RSA.Create()) { blockSize = RSAParametersKey.GetMaxBlockSize(rsa, RSAEncryptionPadding.Pkcs1); RSAParametersKey.SetRSAKeys(rsa, key); if (msg.Length <= blockSize) { var buffer = new Byte[msg.Length]; msg.Read(buffer, 0, buffer.Length); return(rsa.Encrypt(buffer, RSAEncryptionPadding.Pkcs1)); } using (var plaiStream = new BufferedStream(msg)) { using (var crypStream = new MemoryStream()) { var offSet = 0; var inputLen = msg.Length;//总长 //循环 for (int i = 0; inputLen - offSet > 0; offSet = i * blockSize) { //剩余数量足够 if (inputLen - offSet > blockSize) { var buffer = new Byte[blockSize]; plaiStream.Read(buffer, 0, blockSize);//读取到数组中 var reslut = rsa.Encrypt(buffer, RSAEncryptionPadding.Pkcs1); crypStream.Write(reslut, 0, reslut.Length); } else { var buffer = new byte[inputLen - offSet]; plaiStream.Read(buffer, 0, buffer.Length); var reslut = rsa.Encrypt(buffer, RSAEncryptionPadding.Pkcs1); crypStream.Write(reslut, 0, reslut.Length); } ++i; } crypStream.Position = 0; encryptedData = crypStream.ToArray(); } } } return(encryptedData); } catch (Exception ex) { throw ex; } }
public CryptoServer() { RSAParametersKey.CreateKeys(AppDomain.CurrentDomain.BaseDirectory); var rsaKeys = RSAParametersKey.GetRSAKey(AppDomain.CurrentDomain.BaseDirectory); // CipherReply.Singleton.RSAPublicKeys = rsaKeys.PublicKey; CipherReply.Singleton.RSAPrivateKeys = rsaKeys.PrivateKey; // SrvSetting.IsAuthorization = true; }
static void Main(string[] args) { //AES string key = EncryptProviderKey.GetKeys(); string data = "我哎祖国c#?"; Console.WriteLine("数据:{0},Key:{1}", data, key); AesEncryptProvider provider = new AesEncryptProvider(); string result = provider.Encrypt(data, key); Console.WriteLine("加密后:{0},Key:{1}", result, key); AesDecryptProvider decryptProvider = new AesDecryptProvider(); result = decryptProvider.Decrypt(result, key); Console.WriteLine("解密后:{0},Key:{1}", result, key); // ManagerKeys.Singleton.GenerateKeys(AppDomain.CurrentDomain.BaseDirectory); var keyrsa = ManagerKeys.Singleton.GetRSAKey(AppDomain.CurrentDomain.BaseDirectory); ManagerKeys.Singleton.SetKey(null, keyrsa); ManagerKeys.Singleton.SaveLocal("d:", keyrsa.PublicKey); RsaEncryptProvider rsaEncrypt = new RsaEncryptProvider(); result = rsaEncrypt.Encrypt(data, keyrsa.PublicKey); Console.WriteLine("RSA加密后:{0}", result); RsaDecryptProvider rsaDecrypt = new RsaDecryptProvider(); result = rsaDecrypt.Decrypt(result, keyrsa.PrivateKey); Console.WriteLine("RSA解密后:{0}", result); // RSAParametersKey.CreateKeys(AppDomain.CurrentDomain.BaseDirectory); var rsaKey = RSAParametersKey.GetRSAKey(AppDomain.CurrentDomain.BaseDirectory); RsaSrvEncryptProvider rsaSrvEncrypt = new RsaSrvEncryptProvider(); result = rsaSrvEncrypt.Encrypt(data, rsaKey.PublicKey); Console.WriteLine("RSASRV加密后:{0}", result); RsaSrvDecryptProvider rsaSrvDecrypt = new RsaSrvDecryptProvider(); result = rsaSrvDecrypt.Decrypt(result, rsaKey.PrivateKey); Console.WriteLine("RSASRV解密后:{0}", result); Console.Read(); }
public byte[] Decrypt(byte[] msg, string sKey = null) { byte[] decryptedData; using (var rsa = RSA.Create()) { int blockSize = RSAParametersKey.GetMaxBlockSize(rsa, RSAEncryptionPadding.Pkcs1) + 11; RSAParametersKey.SetRSAKeys(rsa, sKey); using (var plaiStream = new MemoryStream(msg)) { using (var decrypStream = new MemoryStream()) { var offSet = 0; var inputLen = msg.Length; for (var i = 0; inputLen - offSet > 0; offSet = i * blockSize) { if (inputLen - offSet > blockSize) { var buffer = new byte[blockSize]; plaiStream.Read(buffer, 0, blockSize); //读取密文 var decrypData = rsa.Decrypt(buffer, RSAEncryptionPadding.Pkcs1); //解密 decrypStream.Write(decrypData, 0, decrypData.Length); } else { var buffer = new byte[inputLen - offSet]; plaiStream.Read(buffer, 0, buffer.Length); var decrypData = rsa.Decrypt(buffer, RSAEncryptionPadding.Pkcs1); decrypStream.Write(decrypData, 0, decrypData.Length); } ++i; } decrypStream.Position = 0; decryptedData = decrypStream.ToArray(); } } } return(decryptedData); }