Wednesday, May 31, 2017

RSA Sign and Verify with certificate file - C#


 public class RsaSha1Signing  
   {  
     private static RsaKeyParameters MakeKey(string keyFileName, bool isPrivateKey)  
     {  
       //var modulus = new Org.BouncyCastle.Math.BigInteger(modulusHexString, 16);  
       //var exponent = new Org.BouncyCastle.Math.BigInteger(exponentHexString, 16);  
       FileStream fs = new FileStream(keyFileName, FileMode.Open);  
       byte[] certBytes = new byte[fs.Length];  
       fs.Read(certBytes, 0, (Int32)fs.Length);  
       fs.Close();  
       X509Certificate2 cert1;  
       if (isPrivateKey) {  
         cert1 = new X509Certificate2(keyFileName, "123456");  
       }  
       else {   
         cert1 = new X509Certificate2();//.Import("");  
         cert1.Import(certBytes);  
       }  
       if (isPrivateKey)  
         return DotNetUtilities.GetRsaPublicKey((RSACryptoServiceProvider)cert1.PrivateKey);  
       return DotNetUtilities.GetRsaPublicKey((RSACryptoServiceProvider)cert1.PublicKey.Key);  
       //return new RsaKeyParameters(isPrivateKey, modulus, exponent);  
     }  
     public static string Sign(string data, string keyFileName)  
     {  
       /* Make the key */  
       RsaKeyParameters key = MakeKey(keyFileName, true);  
       /* Init alg */  
       ISigner sig = SignerUtilities.GetSigner("SHA1withRSA");  
       /* Populate key */  
       sig.Init(true, key);  
       /* Get the bytes to be signed from the string */  
       var bytes = Encoding.UTF8.GetBytes(data);  
       /* Calc the signature */  
       sig.BlockUpdate(bytes, 0, bytes.Length);  
       byte[] signature = sig.GenerateSignature();  
       /* Base 64 encode the sig so its 8-bit clean */  
       var signedString = Convert.ToBase64String(signature);  
       return signedString;  
     }  
     public static bool Verify(string data, string expectedSignature, string keyFileName)  
     {  
       /* Make the key */  
       RsaKeyParameters key = MakeKey(keyFileName, false);  
       /* Init alg */  
       ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");  
       /* Populate key */  
       signer.Init(false, key);  
       /* Get the signature into bytes */  
       var expectedSig = Convert.FromBase64String(expectedSignature);  
       /* Get the bytes to be signed from the string */  
       var msgBytes = Encoding.UTF8.GetBytes(data);  
       /* Calculate the signature and see if it matches */  
       signer.BlockUpdate(msgBytes, 0, msgBytes.Length);  
       return signer.VerifySignature(expectedSig);  
     }  
     public static string SignRsa(string stringToSign, string keyFileName, string privatekeyPassword)  
     {  
       var signed = string.Empty;  
       var ipCert = new X509Certificate2(keyFileName, privatekeyPassword);  
       var RSA = (RSACryptoServiceProvider)ipCert.PrivateKey;  
       var encoder = new ASCIIEncoding();  
       var binData = encoder.GetBytes(stringToSign);  
       byte[] binSignature;  
       using (var sha1 = new SHA1CryptoServiceProvider())  
         binSignature = RSA.SignData(binData, sha1);  
       //if (RSA.VerifyData(binData, new SHA1CryptoServiceProvider(), binSignature))  
       //{  
       signed = Convert.ToBase64String(binSignature);  
       //}          
       return signed;  
     }  
     public static bool VerifyRsa(string data, string expectedSignature, string keyFileName)  
     {  
       var ipCert = new X509Certificate2(keyFileName);  
       var RSA = (RSACryptoServiceProvider)ipCert.PublicKey.Key;  
       var encoder = new ASCIIEncoding();  
       var binData = encoder.GetBytes(data);  
       byte[] binSignature = Convert.FromBase64String(expectedSignature);  
       return RSA.VerifyData(binData, new SHA1CryptoServiceProvider(), binSignature);  
     }  

No comments:
Write comments