RSA: using public key for decryption in .NET?

I am having problems with RSA encryption / decryption capabilities for MS.NET:

It seems, .NET does not support using a private key to encrypt and a corresponding public key to decrypt. (As I understand it, it somehow violates the security of the algorithm.)

Good, but, for example, when I sign the assembly during construction, it seems that the compiler simply does this : "The compiler encrypts the digest using the 1024-bit private key from the public key pair file.

So, if I cannot convince RSACryptoServiceProvider to use the public key for decryption, how can I achieve something similar, for example, a compiler?

I just want to encrypt several bytes with my private key and decrypt it using the public key for some non-critical task. If the software developer succeeds in breaking this scheme, I will live. I just want the non-specialized John Doe not talk.

Any advice on this would be appreciated.

hello berntie

Edit: Use SignData () and VerifySign () have been suggested, but then I can compare hashes for equality. However, I need to get the original input that was encrypted / signed.

+3
source share
2 answers

.Net , "" SignData() RSACryptoServiceProvider. , , , , .

, , , - , .

. SignData, , .

+2

, , . modulusString Java.

public static string Decrypt(string text, string modulusString)
{
    var modulus = BigInteger.Parse(modulusString);
    var exponent = BigInteger.Parse("65537");

    var encryptBytes = Convert.FromBase64String(text);

    if (publicKey.Modulus.Length > 309) // long enough key to decrypt short message
    {
        return Decrypt(encryptBytes, exponent, modulus);
    }

    string result = string.Empty;
    int i = 0;
    while (i < encryptBytes.Length) // for short key, must decrypt section-by-section
    {
        var temp = new byte[Math.Min(encryptBytes.Length, 128)];
        Array.Copy(encryptBytes, i, temp, 0, temp.Length);
        result += Decrypt(temp, exponent, modulus);
        i += 128;
    }
    return result;
}

private static string Decrypt(byte[] encryptBytes, BigInteger exponent, BigInteger modulus)
{
    Array.Reverse(encryptBytes); // BigIntenger need little-endian
    if ((encryptBytes[encryptBytes.Length - 1] & 0x80) > 0) // make positive
    {
        var temp = new byte[encryptBytes.Length];
        Array.Copy(encryptBytes, temp, encryptBytes.Length);
        encryptBytes = new byte[temp.Length + 1];
        Array.Copy(temp, encryptBytes, temp.Length);
    }
    var value = new BigInteger(encryptBytes);

    var result = BigInteger.ModPow(value, exponent, modulus);
    byte[] resultBytes = result.ToByteArray();
    Array.Reverse(resultBytes);

    int index = Array.FindIndex(resultBytes, b => b == 0) + 1;
    return Encoding.UTF8.GetString(resultBytes, index, resultBytes.Length - index);
}
-1

All Articles