Encrypt/decrypt text with asymmetric and symmetric hybrid way.
- Prepare RSA private/public keys. You could generate one if you don't have. e.g. $ openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048 $ openssl rsa -pubout -in private_key.pem -out public_key.pem
- Encrypt the text with public key.
var cipher = HybridCryptor.Encrypt(publicKeyPath, text);
- Decrypt the cipher with private key.
var text = HybridCryptor.Decrypt(privateKeyPath, cipher);
The idea of HybridCrypting is inspired by TLS/SSL handshake. Use asymmetric way to encrypt/decrypt the key cipher and symmetric way to encrypt/decrypt the real content. The benifit is to keep the process both secure(only public key needed for encrypt and the key is totally new generated every time) and efficiant(the key length is only 256, and the real long content is encrypted with symmetric way).
Steps of encrypting
- Generate a totally new key (length 256) for encrypting the real content later.
- Use asymmetric encrypting (public key encrypting) to encrypt the key and get a key cipher.
- Use the key to encrypt the real content with symmetric encrypting.
- Combine the key cipher and the encrypted content together and output the encrypted bytes
Steps of decrypting
- Split the encrypted bytes, get the key cipher and encrypted content.
- Use asymmetric decrypting (private key decrypting) to decrypt the cipher and get the key.
- Use the key to decrypt the encrypted content with symmetric decrypting. Then you get the real content back.
- Dotnet Core Framework
Install-Package HybridCrypting
- Dotnet - Dotnet Core Framework
- Nuget - Package Management
- Portable.BouncyCastle - BouncyCastle portable version
We use SemVer for versioning. For the versions available, see the tags on this repository.
This project is licensed under the MIT License - see the LICENSE.md file for details