RSA encryption and decryption with PEM keys in C#
// Here you can find full description: https://thetial.com/rsa-encryption-and-decryption-net-core/
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Crypto.Parameters;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string plainText = @'Connect with me on linkedin: https://www.linkedin.com/in/dziwoki';
string cihperText = encrypt(plainText);
string decryptedCipherText = decrypt(cihperText);
Console.WriteLine('Encrypted text: {0}', cihperText);
Console.WriteLine('Decrypted text {0}. Encryption/Decryption was correct {1}',
decryptedCipherText, (plainText decryptedCipherText).ToString());
}
static string encrypt(string plainText) {
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PemReader pr = new PemReader(
(StreamReader)File.OpenText('./pem_public.pem')
);
RsaKeyParameters keys = (RsaKeyParameters)pr.ReadObject();
// Pure mathematical RSA implementation
// RsaEngine eng = new RsaEngine();
// PKCS1 v1.5 paddings
// Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
// PKCS1 OAEP paddings
OaepEncoding eng = new OaepEncoding(new RsaEngine());
eng.Init(true, keys);
int length = plainTextBytes.Length;
int blockSize = eng.GetInputBlockSize();
List<byte> cipherTextBytes = new List<byte>();
for (int chunkPosition = 0;
chunkPosition < length;
chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, length - chunkPosition);
cipherTextBytes.AddRange(eng.ProcessBlock(
plainTextBytes, chunkPosition, chunkSize
));
}
return Convert.ToBase64String(cipherTextBytes.ToArray());
}
static string decrypt(string cipherText) {
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PemReader pr = new PemReader(
(StreamReader)File.OpenText('./pem_private.pem')
);
AsymmetricCipherKeyPair keys = (AsymmetricCipherKeyPair)pr.ReadObject();
// Pure mathematical RSA implementation
// RsaEngine eng = new RsaEngine();
// PKCS1 v1.5 paddings
// Pkcs1Encoding eng = new Pkcs1Encoding(new RsaEngine());
// PKCS1 OAEP paddings
OaepEncoding eng = new OaepEncoding(new RsaEngine());
eng.Init(false, keys.Private);
int length = cipherTextBytes.Length;
int blockSize = eng.GetInputBlockSize();
List<byte> plainTextBytes = new List<byte>();
for (int chunkPosition = 0;
chunkPosition < length;
chunkPosition += blockSize)
{
int chunkSize = Math.Min(blockSize, length - chunkPosition);
plainTextBytes.AddRange(eng.ProcessBlock(
cipherTextBytes, chunkPosition, chunkSize
));
}
return Encoding.UTF8.GetString(plainTextBytes.ToArray());
}
}
}

commented Jan 29, 2017

Here you can find full description: RSA encryption and decryption with PEM keys in C#

The purpose of this code is to generate the CSR and the Private Key. The scenario is that we have some clients for whom we run websites and for the SSL component we want to have a page on our site. Bouncy Castle Cryptography Library 1.37 PREV CLASS NEXT CLASS: FRAMES NO FRAMES SUMMARY: NESTED. Public class RSAKeyPairGenerator. An RSA key pair generator. Constructor Summary; RSAKeyPairGenerator Method Summary AsymmetricCipherKeyPair: generateKeyPair. RSA Encryption can be achieved by following the below steps. Select an input message - For demonstration, we select to use a plain text message “Test message” Generate a RSA key pair - The bouncy castle type – RSAKeyPairGenerator has been used for generating the asymmetric key pair.

commented Oct 31, 2018

This is great stuff! Thanks

commented Mar 28, 2019

If client side uses the above code for RSA Encryption, a math very similar to the one which is written above would be required for RSA Decryption right ? Or some .NET framework classes like RSACryptoServiceProvider can Decrypt as well ?

commented Jun 18, 2019

Exactly what I've been trying to achieve. Thanks for posting this.

C Generate Rsa Public Private Key Pair Bouncy Castle Hill

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Chilkat • HOME • Android™ • Classic ASP • C • C++ • C# • Mono C# • .NET Core C# • C# UWP/WinRT • DataFlex • Delphi ActiveX • Delphi DLL • Visual FoxPro • Java • Lianja • MFC • Objective-C • Perl • PHP ActiveX • PHP Extension • PowerBuilder • PowerShell • PureBasic • CkPython • Chilkat2-Python • Ruby • SQL Server • Swift 2 • Swift 3/4 • Tcl • Unicode C • Unicode C++ • Visual Basic 6.0 • VB.NET • VB.NET UWP/WinRT • VBScript • Xojo Plugin • Node.js • Excel • Go

C Generate Rsa Public Private Key Pair Bouncy Castle In The World

Web API Categories
ASN.1
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks

Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

Uplay cd key generator download. C++ example code showing how to generate an RSA public/private key.

Chilkat C/C++ Library Downloads

C Generate Rsa Public Private Key Pair Bouncy Castle 2017

© 2000-2020 Chilkat Software, Inc. All Rights Reserved.

C Generate Rsa Public Private Key Pair Bouncy Castle In Scotland