public AsymmetricEncryption() { HA = GetHashAlgorithm(); RSACSP = GetRSACryptoServiceProvider(); DecryptCount = RSACSP.KeySize / 8; EncryptCount = DecryptCount - 11; PublicKeyBase64String = RSACSP.ExportCspBlob(false).ToBase64String(); }
public AsymmetricEncryption(byte[] publicKeyBlob, byte[] privateKeyBlob) { HA = GetHashAlgorithm(); RSACSP = GetRSACryptoServiceProvider(); RSACSP.ImportCspBlob(publicKeyBlob); RSACSP.ImportCspBlob(privateKeyBlob); DecryptCount = RSACSP.KeySize / 8; EncryptCount = DecryptCount - 11; PublicKeyBase64String = publicKeyBlob.ToBase64String(); }
/// <summary> /// 使用RSA进行非对称解密,使用默认私钥 /// </summary> /// <param name="inputBuffer">待解密的字节数组</param> /// <returns>解密后的字节数组</returns> public byte[] Decrypt(byte[] inputBuffer) { if (inputBuffer.Length <= DecryptCount) { return(RSACSP.Decrypt(inputBuffer, false)); } else { List <byte> list = new List <byte>(); MemoryStream ms = new MemoryStream(inputBuffer); byte[] buffer = new byte[EncryptCount]; while (ms.Read(buffer, 0, EncryptCount) > 0) { list.AddRange(RSACSP.Decrypt(buffer, false)); } return(list.ToArray()); } }
/// <summary> /// 使用RSA进行非对称加密 /// </summary> /// <param name="inputBuffer">待加密的字节数组</param> /// <param name="keyBlob">RSA公钥Blob字节数组</param> /// <returns>加密后的字节数组</returns> public byte[] Encrypt(byte[] inputBuffer, byte[] keyBlob) { using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider()) { RSA.ImportCspBlob(keyBlob); if (inputBuffer.Length <= EncryptCount) { return(RSA.Encrypt(inputBuffer, false)); } else { List <byte> list = new List <byte>(); MemoryStream ms = new MemoryStream(inputBuffer); byte[] buffer = new byte[EncryptCount]; while (ms.Read(buffer, 0, EncryptCount) > 0) { list.AddRange(RSACSP.Encrypt(buffer, false)); } return(list.ToArray()); } } }