Created
July 29, 2018 19:43
-
-
Save rbrayb/0feebace5253f68ca234cecfc4ed7205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Security.Cryptography; | |
using Org.BouncyCastle.Crypto; | |
using Org.BouncyCastle.Crypto.Digests; | |
using Org.BouncyCastle.Crypto.Encodings; | |
using Org.BouncyCastle.Crypto.Engines; | |
using Org.BouncyCastle.Crypto.Parameters; | |
using Org.BouncyCastle.Security; | |
namespace BCMediumConsoleApp | |
{ | |
class Program | |
{ | |
static byte[] data = new byte[] {0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20}; | |
static byte[] privateKey; | |
static byte[] publicKey; | |
static byte[] encrypted; | |
static byte[] decrypted; | |
static byte[] signed; | |
static int keyLength = 2048; | |
static RSACryptoServiceProvider rsaCryptoProvider; | |
static CspParameters CSPParam; | |
static void Main(string[] args) | |
{ | |
try | |
{ | |
CSPParam = new CspParameters(1) { Flags = CspProviderFlags.UseMachineKeyStore }; | |
CreateRSAKeysMS(out privateKey, out publicKey); | |
encrypted = Encrypt(publicKey, data); | |
decrypted = Decrypt(privateKey, encrypted); | |
bool result = ByteArrayCompare(data, decrypted); | |
if (result) | |
Console.WriteLine("Encrypt / decrypt works"); | |
else | |
Console.WriteLine("Encrypt / decrypt fails"); | |
signed = Sign512(data, privateKey); | |
bool result1 = VerifySignature512(data, signed, publicKey); | |
if (result1) | |
Console.WriteLine("Signing works"); | |
else | |
Console.WriteLine("Signing fails"); | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine("Exception " + e.Message); | |
} | |
Console.ReadLine(); | |
} | |
static bool ByteArrayCompare(byte[] a1, byte[] a2) | |
{ | |
if (a1.Length != a2.Length) | |
return false; | |
for (int i = 0; i < a1.Length; i++) | |
if (a1[i] != a2[i]) | |
return false; | |
return true; | |
} | |
public static bool CreateRSAKeysMS(out byte[] privateKey, out byte[] publicKey) | |
{ | |
privateKey = null; | |
publicKey = null; | |
rsaCryptoProvider = new RSACryptoServiceProvider(keyLength, CSPParam); | |
publicKey = rsaCryptoProvider.ExportCspBlob(false); | |
privateKey = rsaCryptoProvider.ExportCspBlob(true); | |
return true; | |
} | |
public static byte[] Encrypt(byte[] publicKey, byte[] data) | |
{ | |
rsaCryptoProvider = new RSACryptoServiceProvider(keyLength, CSPParam); | |
rsaCryptoProvider.ImportCspBlob(publicKey); | |
RSAParameters parameters = rsaCryptoProvider.ExportParameters(false); | |
RsaKeyParameters key = DotNetUtilities.GetRsaPublicKey(parameters); | |
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha1Digest(), new Sha1Digest(), new byte[0]); | |
cipher.Init(true, key); | |
return cipher.ProcessBlock(data, 0, data.Length); | |
} | |
public static byte[] Decrypt(byte[] privateKey, byte[] data) | |
{ | |
rsaCryptoProvider = new RSACryptoServiceProvider(keyLength, CSPParam); | |
rsaCryptoProvider.ImportCspBlob(privateKey); | |
RSAParameters parameters = rsaCryptoProvider.ExportParameters(true); | |
AsymmetricCipherKeyPair keyPair = DotNetUtilities.GetRsaKeyPair(parameters); | |
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha1Digest(), new Sha1Digest(), new byte[0]); | |
cipher.Init(false, keyPair.Private); | |
return cipher.ProcessBlock(data, 0, data.Length); | |
} | |
public static byte[] Sign512(byte[] data, byte[] privateKey) | |
{ | |
var enhCsp = new RSACryptoServiceProvider().CspKeyContainerInfo; | |
var cspparams = new CspParameters(enhCsp.ProviderType, enhCsp.ProviderName); | |
rsaCryptoProvider = new RSACryptoServiceProvider(cspparams); | |
rsaCryptoProvider.ImportCspBlob(privateKey); | |
return rsaCryptoProvider.SignData(data, CryptoConfig.MapNameToOID("SHA512")); | |
} | |
public static bool VerifySignature512(byte[] data, byte[] signature, byte[] publicKey) | |
{ | |
var enhCsp = new RSACryptoServiceProvider().CspKeyContainerInfo; | |
var cspparams = new CspParameters(enhCsp.ProviderType, enhCsp.ProviderName); | |
rsaCryptoProvider = new RSACryptoServiceProvider(cspparams); | |
rsaCryptoProvider.ImportCspBlob(publicKey); | |
return rsaCryptoProvider.VerifyData(data, CryptoConfig.MapNameToOID("SHA512"), signature); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://medium.com/the-new-control-plane/using-bouncycastle-to-augment-the-net-rsacryptoserviceprovider-class-9850bc3f18a7