小编ant*_*tes的帖子

使用证书加密

我对所有这些加密事件都很陌生,我正在尝试使用一个简单的应用来加密给定的字符串.这是我的代码:

public static X509Certificate2 getPublicKey()
{
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

    X509Certificate2 cert2 = new X509Certificate2("c:\\certificate.cer");

    return cert2;
}


public static string cipherRequest(byte[] stringToEncrypt)
{
    X509Certificate2 certificate = getPublicKey();

    RSACryptoServiceProvider rsa = certificate.PublicKey.Key as RSACryptoServiceProvider;

    byte[] cryptedData = rsa.Encrypt(stringToEncrypt, true);

    return Convert.ToBase64String(cryptedData);
}

public static void Main()
{

    try
    {

        ASCIIEncoding ByteConverter = new ASCIIEncoding();

        byte[] test = ByteConverter.GetBytes("stringtoencrypt");

        string first = cipherRequest(test);
        string second= cipherRequest(test);

        Console.WriteLine("first: {0}", first);
        Console.WriteLine("second: {0}", second);

    }
    catch(CryptographicException e)
    {
        Console.WriteLine(e.Message);
    }

}
Run Code Online (Sandbox Code Playgroud)

因此每次我调用 …

c# certificate public-key-encryption

7
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

certificate ×1

public-key-encryption ×1