Skip to content

Instantly share code, notes, and snippets.

@makah
Created October 21, 2016 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makah/2c2ad477c05099add3f99c81c6cd71ec to your computer and use it in GitHub Desktop.
Save makah/2c2ad477c05099add3f99c81c6cd71ec to your computer and use it in GitHub Desktop.
using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.IO;
using System.Xml;
using OpenbusAPI.Logger;
namespace Security
{
public static class Crypto
{
public static RSACryptoServiceProvider ReadPrivateKey(String privateKeyFileName) {
StreamReader key = File.OpenText(privateKeyFileName);
String pemstr = key.ReadToEnd().Trim();
key.Close();
RSACryptoServiceProvider rsa = RSAPKCS8KeyFormatter.DecodePEMKey(pemstr);
if (rsa == null)
Log.CRYPTO.Fatal("...");
return rsa;
}
public static X509Certificate2 ReadCertificate(String certificateFile) {
return new X509Certificate2(certificateFile);
}
public static byte[] GenerateAnswer(byte[] challenge, RSACryptoServiceProvider privateKey, X509Certificate2 acsCertificate) {
byte[] plainChallenge = Decrypt(privateKey, challenge);
return Encrypt(acsCertificate, plainChallenge);
}
#region Internal Members
private static byte[] Decrypt(RSACryptoServiceProvider privateKey, byte[] data) {
return privateKey.Decrypt(data, false);
}
private static byte[] Encrypt(X509Certificate2 acsCertificate,
byte[] plainData) {
RSACryptoServiceProvider RsaProvider = acsCertificate.PublicKey.Key as
RSACryptoServiceProvider;
return RsaProvider.Encrypt(plainData, false);
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment