Question

I am starting to use encryption and decryption in my web service. I am using the RSACryptoServiceProvider and when using the Encrypt & Decrypt methods, I have no problem.

However, as soon as I try to use the SignData method with new SHA1CryptoServiceProvider() as encryption method, I am unable to recover the original data. I am only able to verify them. Is it really not possible to retrieve the signed data? If so, what is the purpose of the whole signing process? And is there another possibility how to encrypt data by a certain algorithm?

EDIT: I am posting the code, which is just an altered example from MSDN

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

        string dataString = "Data to Encrypt";

        //Create byte arrays to hold original, encrypted, and decrypted data. 
        byte[] dataToEncrypt = ByteConverter.GetBytes(dataString);
        byte[] encryptedData;
        byte[] signedData;
        byte[] decryptedData;
        byte[] unsignedData;
        var fileName = ConfigurationManager.AppSettings["certificate"];
        var password = ConfigurationManager.AppSettings["password"];
        var certificate = new X509Certificate2(fileName, password);

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

        //Display the origianl data to the console.
        Console.WriteLine("Original Data: {0}", dataString);

        //Encrypt the byte array and specify no OAEP padding.   
        //OAEP padding is only available on Microsoft Windows XP or 
        //later.  
        encryptedData = RSAalg.Encrypt(dataToEncrypt, false);
        signedData = RSAalg.SignData(dataToEncrypt, new SHA1CryptoServiceProvider());

        //Display the encrypted data to the console. 
        Console.WriteLine("Encrypted Data: {0}", ByteConverter.GetString(encryptedData));
        Console.WriteLine("Signed Data: {0}", ByteConverter.GetString(signedData));

        //Pass the data to ENCRYPT and boolean flag specifying  
        //no OAEP padding.
        decryptedData = RSAalg.Decrypt(encryptedData, false);
    //In the next line I get the error of wrong data
        unsignedData = RSAalg.Decrypt(signedData, false);

        //Display the decrypted plaintext to the console. 
        Console.WriteLine("Decrypted plaintext: {0}", ByteConverter.GetString(decryptedData));
        Console.WriteLine("Unsigned plaintext: {0}", ByteConverter.GetString(unsignedData));
    }
    catch (CryptographicException e)
    {
        //Catch this exception in case the encryption did 
        //not succeed.
        Console.WriteLine(e.Message);

    }

    Console.Read();
}
Was it helpful?

Solution

SHA1 is a hash function, so you cant compute a message that has a given hash. In other words, you cant sign/unsign the message, you only can sign and verify it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top