0

Web サービスで暗​​号化と復号化を使用し始めています。私はRSACryptoServiceProviderを使用していますが、Encrypt & Decryptメソッドを使用しても問題はありません。

ただし、新しい SHA1CryptoServiceProvider()を暗号化方法としてSignDataメソッドを使用しようとすると、元のデータを復元できません。私はそれらを確認することしかできません。署名されたデータを取得することは本当に不可能ですか? もしそうなら、署名プロセス全体の目的は何ですか? また、特定のアルゴリズムでデータを暗号化する別の可能性はありますか?

編集:コードを投稿していますが、これは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();
}
4

1 に答える 1

1

SHA1 はハッシュ関数であるため、特定のハッシュを持つメッセージを計算することはできません。つまり、メッセージに署名/署名解除することはできず、署名と検証のみが可能です。

于 2013-07-26T09:52:54.717 に答える