Created
July 29, 2018 19:42
-
-
Save rbrayb/cd2c73d1d521d6d32dc07e7dbb945d3e to your computer and use it in GitHub Desktop.
This file contains hidden or 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 System.Text; | |
using Org.BouncyCastle.Crypto; | |
using Org.BouncyCastle.Crypto.Digests; | |
using Org.BouncyCastle.Crypto.Encodings; | |
using Org.BouncyCastle.Crypto.Engines; | |
using Org.BouncyCastle.Crypto.Generators; | |
using Org.BouncyCastle.Crypto.Parameters; | |
using Org.BouncyCastle.Security; | |
namespace BCConsoleApp | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
SHA512Managed hash = new SHA512Managed(); | |
SecureRandom randomNumber = new SecureRandom(); | |
byte[] encodingParam = hash.ComputeHash(Encoding.UTF8.GetBytes(randomNumber.ToString())); | |
string inputMessage = "Test Message"; | |
UTF8Encoding utf8enc = new UTF8Encoding(); | |
byte[] inputBytes = utf8enc.GetBytes(inputMessage); | |
RsaKeyPairGenerator rsaKeyPairGnr = new RsaKeyPairGenerator(); | |
rsaKeyPairGnr.Init(new KeyGenerationParameters(new SecureRandom(), 2048)); | |
AsymmetricCipherKeyPair keyPair = rsaKeyPairGnr.GenerateKeyPair(); | |
RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public; | |
RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private; | |
IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine(), new Sha512Digest(), encodingParam); | |
cipher.Init(true, publicKey); | |
byte[] ciphered = cipher.ProcessBlock(inputBytes, 0, inputMessage.Length); | |
cipher.Init(false, privateKey); | |
byte[] deciphered = cipher.ProcessBlock(ciphered, 0, ciphered.Length); | |
string decipheredText = utf8enc.GetString(deciphered); | |
if (decipheredText == "Test Message") | |
Console.WriteLine("Encrypt / decrypt works"); | |
else | |
Console.WriteLine("Encrypt / decrypt fails"); | |
var result = Sign(inputMessage, privateKey); | |
bool result1 = Verify(inputMessage, result, publicKey); | |
if (result1) | |
Console.WriteLine("Signing works"); | |
else | |
Console.WriteLine("Signing fails"); | |
Console.ReadLine(); | |
} | |
public static String Sign(String data, RsaKeyParameters privateKey) | |
{ | |
ISigner sign = SignerUtilities.GetSigner("SHA512withRSA"); | |
sign.Init(true, privateKey); | |
var bytes = Encoding.UTF8.GetBytes(data); | |
sign.BlockUpdate(bytes, 0, bytes.Length); | |
byte[] signature = sign.GenerateSignature(); | |
var signed = Convert.ToBase64String(signature); | |
return signed; | |
} | |
public static bool Verify(String data, String expectedSignature, RsaKeyParameters publicKey) | |
{ | |
ISigner sign = SignerUtilities.GetSigner("SHA512withRSA"); | |
sign.Init(false, publicKey); | |
var expectedSign = Convert.FromBase64String(expectedSignature); | |
var bytes = Encoding.UTF8.GetBytes(data); | |
sign.BlockUpdate(bytes, 0, bytes.Length); | |
return sign.VerifySignature(expectedSign); | |
} | |
} | |
} |
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/bouncycastle-cryptography-ce2a41bf1c92