RSA signing in C#

隐身守侯 提交于 2019-12-12 03:25:56

问题


I need to make a software activation mechanism. So i ended up with this scheme: App creates a unique id, based on computer's hardware. Buyer emails this id to me. I sign it with my private key and i send back the signed string. App verifies string (decodes it with contained public key and compares it with hardware id).

So far i am done with hardware id and i have created the keys (1024bit) with openssl, that's two files private.pem and public.pem.

I worked with some code provided by msdn, but i don't quite understand how to import my keys into RSA algorithm. Some of the code follows, any help will be appreciated...

        try
        {
            // Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = CompID.Text;

            // Create byte arrays to hold original, encrypted, and decrypted data. 
            byte[] originalData = ByteConverter.GetBytes(dataString);
            byte[] signedData;

            // Create a new instance of the RSACryptoServiceProvider class  
            // and automatically create a new key-pair.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            // Export the key information to an RSAParameters object. 
            // You must pass true to export the private key for signing. 
            // However, you do not need to export the private key 
            // for verification.
            RSAParameters Key = RSAalg.ExportParameters(true);

            // Hash and sign the data.
            signedData = HashAndSignBytes(originalData, Key);
            ActCode.Text = Encoding.UTF8.GetString(signedData, 0, signedData.Length);                

            // Verify the data and display the result to the  
            // console. 
            if (VerifySignedHash(originalData, signedData, Key))
            {
                MessageBox.Show("The data was verified.");
            }
            else
            {
                MessageBox.Show("The data does not match the signature.");
            }

        }
        catch (ArgumentNullException)
        {
            MessageBox.Show("The data was not signed or verified");

        }

The methods:

    public static byte[] HashAndSignBytes(byte[] DataToSign, RSAParameters Key)
    {
        try
        {
            // Create a new instance of RSACryptoServiceProvider using the  
            // key from RSAParameters.  
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            RSAalg.ImportParameters(Key);

            // Hash and sign the data. Pass a new instance of SHA1CryptoServiceProvider 
            // to specify the use of SHA1 for hashing. 
            return RSAalg.SignData(DataToSign, new SHA1CryptoServiceProvider());
        }
        catch (CryptographicException e)
        {
            MessageBox.Show(e.Message);

            return null;
        }
    }

    public static bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, RSAParameters Key)
    {
        try
        {
            // Create a new instance of RSACryptoServiceProvider using the  
            // key from RSAParameters.
            RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();

            RSAalg.ImportParameters(Key);

            // Verify the data using the signature.  Pass a new instance of SHA1CryptoServiceProvider 
            // to specify the use of SHA1 for hashing. 
            return RSAalg.VerifyData(DataToVerify, new SHA1CryptoServiceProvider(), SignedData);

        }
        catch (CryptographicException e)
        {
            MessageBox.Show(e.Message);

            return false;
        }
    }

来源:https://stackoverflow.com/questions/28096261/rsa-signing-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!