Created
October 21, 2016 02:15
-
-
Save makah/2c2ad477c05099add3f99c81c6cd71ec 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 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