• C# RSA 加密解密


    公匙

    publicKeyStr = "MIGJAoGBALogQ4t/yzBSbKH6ScXA54pAeSjvC/R13RIif4jmTbcJLqGFaQmj5WQnnmJqFsCI8lnTr4dmHcmSPvNhtWnEAznHBhGftQqcDuPmQNROMfUB7PKMteSWPhI5FpRxIXTTZC6GU6/xOklVxy24+zqSqMyYX9BOD8MxetvW9HYsSufNAgMBAAE=";

    私匙

    privateKeyStr = "MIICXgIBAAKBgQC6IEOLf8swUmyh+knFwOeKQHko7wv0dd0SIn+I5k23CS6hhWkJo+VkJ55iahbAiPJZ06+HZh3Jkj7zYbVpxAM5xwYRn7UKnA7j5kDUTjH1AezyjLXklj4SORaUcSF002QuhlOv8TpJVcctuPs6kqjMmF/QTg/DMXrb1vR2LErnzQIDAQABAoGBALjzRItDMsJStVYDovkZ5jrQNFVDrQp/cG0L0NVVnNYMDsU8D5MfxNtQLye2KeOqccBnBJ40D9d0No+cgSuXNmepkQVPy83iSOcL4tbR8Hhhh3jSn8hdkwoRgpLH9dO5PsNqP6Yunx0d99NW5qwTROAtWpScwCSaBxu3LKVJA8IVAkEA6cvD2lzZbTlDxULPo8RUqJ40pWZzZSbxbx5xwN6nWHLXpTJ175NtBjqTV7iQyhwQuIM1Oe1FEPwbXQTH0nkl7wJBAMvNgmBlN4P2LE5ZTjtVG3jw7DHlKb53zh0Kgwss3WePlYMpkQfGcYYwz/0E7zihmhgiohYb7Czc/sYoSTX96gMCQQCdeL+sHoncRYveG8gbeCvFfvnaxgzHftlzjL/1toJk0sVo4XoKKfRvE5kWppvvZcPlY4rAP44biJMtrgChRTRFAkAcpM6tM6ygzGmfvWeEaUrKLbpUrOu4DGiYplCSsoSPuJt/6siqehackKG64wWoRPY+bCexyczg5VKdkS3w+7X3AkEAuh6w7qL687jibL70z1lNhXZkLijnMEoD6s9qhCklh+s6iTKjTQ5x7aTitUoMaxxOk7dTvF8fBDRDJywMfrLnuA==";

    加密

    private static string Encrypted(string data, string pubkey)
    {
    using RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    var publicKey = Convert.FromBase64String(pubkey);
    rsa.ImportRSAPublicKey(publicKey, out int bytesreadPublic);

    var dataArr = Encoding.UTF8.GetBytes(data);
    var maxBufferSize = rsa.KeySize / 8 - 11;

    if (dataArr.Length < maxBufferSize)
    {
    return Convert.ToBase64String(rsa.Encrypt(dataArr, false));
    }

    using MemoryStream msread = new MemoryStream(dataArr);
    using MemoryStream mswrite = new MemoryStream();

    var buffer = new byte[maxBufferSize];
    var len = msread.Read(buffer, 0, buffer.Length);
    while (len > 0)
    {
    mswrite.Write(rsa.Encrypt(buffer, false));
    buffer = new byte[maxBufferSize];
    len = msread.Read(buffer, 0, buffer.Length);
    }

    var resStr = Convert.ToBase64String(mswrite.ToArray());

    return resStr;
    }

    解密

    private static string Decrypted(string data, string prikey)
    {
    using RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
    var privateKey = Convert.FromBase64String(prikey);
    rsa.ImportRSAPrivateKey(privateKey, out int bytesreadPrivate);

    var dataArr = Convert.FromBase64String(data);
    var maxBufferSize = rsa.KeySize / 8;

    if (dataArr.Length < maxBufferSize)
    {
    return Encoding.UTF8.GetString(rsa.Decrypt(dataArr, false));
    }

    using MemoryStream msread = new MemoryStream(dataArr);
    using MemoryStream mswrite = new MemoryStream();

    var buffer = new byte[maxBufferSize];
    var len = msread.Read(buffer, 0, buffer.Length);
    while (len > 0)
    {
    mswrite.Write(rsa.Decrypt(buffer, false));
    buffer = new byte[maxBufferSize];
    len = msread.Read(buffer, 0, buffer.Length);
    }

    var resStr = Encoding.UTF8.GetString(mswrite.ToArray());

    return resStr;
    }

  • 相关阅读:
    通信收发单元
    WOSA协议(转)
    CcTalk (网络协议)(转)
    0x和H都表示十六进制有什么区别吗?
    select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET(转)
    &是什么运算符(转)
    NDK的Paths and Symbols设定
    android JNI调用(转)
    scrapy-redis基础和介绍
    scrapy-redis介绍(一)
  • 原文地址:https://www.cnblogs.com/lovewl2/p/11612646.html
Copyright © 2020-2023  润新知