zoukankan      html  css  js  c++  java
  • C#下RSA算法的实现(适用于支付宝和易宝支付)

     

    C#下RSA算法的实现(适用于支付宝和易宝支付)

     
     

    RSA算法代码:

    [csharp] view plain copy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using System.IO;  
    5. using System.Security.Cryptography;  
    6.   
    7. namespace RSA.Class  
    8. {  
    9.     /// <summary>  
    10.     /// 类名:RSAFromPkcs8  
    11.     /// 功能:RSA加密、解密、签名、验签  
    12.     /// 详细:该类对Java生成的密钥进行解密和签名以及验签专用类,不需要修改  
    13.     /// 版本:3.0  
    14.     /// 日期:2013-07-08  
    15.     /// 说明:  
    16.     /// 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。  
    17.     /// </summary>  
    18.     public sealed class RSAFromPkcs8  
    19.     {  
    20.         /// <summary>  
    21.         /// 签名  
    22.         /// </summary>  
    23.         /// <param name="content">待签名字符串</param>  
    24.         /// <param name="privateKey">私钥</param>  
    25.         /// <param name="input_charset">编码格式</param>  
    26.         /// <returns>签名后字符串</returns>  
    27.         public static string sign(string content, string privateKey, string input_charset)  
    28.         {  
    29.             byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);  
    30.             RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);  
    31.             SHA1 sh = new SHA1CryptoServiceProvider();  
    32.             byte[] signData = rsa.SignData(Data, sh);  
    33.             return Convert.ToBase64String(signData);  
    34.         }  
    35.   
    36.         /// <summary>  
    37.         /// 验签  
    38.         /// </summary>  
    39.         /// <param name="content">待验签字符串</param>  
    40.         /// <param name="signedString">签名</param>  
    41.         /// <param name="publicKey">公钥</param>  
    42.         /// <param name="input_charset">编码格式</param>  
    43.         /// <returns>true(通过),false(不通过)</returns>  
    44.         public static bool verify(string content, string signedString, string publicKey, string input_charset)  
    45.         {  
    46.             bool result = false;  
    47.             byte[] Data = Encoding.GetEncoding(input_charset).GetBytes(content);  
    48.             byte[] data = Convert.FromBase64String(signedString);  
    49.             RSAParameters paraPub = ConvertFromPublicKey(publicKey);  
    50.             RSACryptoServiceProvider rsaPub = new RSACryptoServiceProvider();  
    51.             rsaPub.ImportParameters(paraPub);  
    52.             SHA1 sh = new SHA1CryptoServiceProvider();  
    53.             result = rsaPub.VerifyData(Data, sh, data);  
    54.             return result;  
    55.         }  
    56.   
    57.         /// <summary>  
    58.         /// 加密  
    59.         /// </summary>  
    60.         /// <param name="resData">需要加密的字符串</param>  
    61.         /// <param name="publicKey">公钥</param>  
    62.         /// <param name="input_charset">编码格式</param>  
    63.         /// <returns>明文</returns>  
    64.         public static string encryptData(string resData, string publicKey, string input_charset)  
    65.         {  
    66.             byte[] DataToEncrypt = Encoding.ASCII.GetBytes(resData);  
    67.             string result = encrypt(DataToEncrypt, publicKey, input_charset);  
    68.             return result;  
    69.         }  
    70.   
    71.   
    72.         /// <summary>  
    73.         /// 解密  
    74.         /// </summary>  
    75.         /// <param name="resData">加密字符串</param>  
    76.         /// <param name="privateKey">私钥</param>  
    77.         /// <param name="input_charset">编码格式</param>  
    78.         /// <returns>明文</returns>  
    79.         public static string decryptData(string resData, string privateKey, string input_charset)  
    80.         {  
    81.             byte[] DataToDecrypt = Convert.FromBase64String(resData);  
    82.             string result = "";  
    83.             for (int j = 0; j < DataToDecrypt.Length / 128; j++)  
    84.             {  
    85.                 byte[] buf = new byte[128];  
    86.                 for (int i = 0; i < 128; i++)  
    87.                 {  
    88.   
    89.                     buf[i] = DataToDecrypt[i + 128 * j];  
    90.                 }  
    91.                 result += decrypt(buf, privateKey, input_charset);  
    92.             }  
    93.             return result;  
    94.         }  
    95.  
    96.         #region 内部方法  
    97.   
    98.         private static string encrypt(byte[] data, string publicKey, string input_charset)  
    99.         {  
    100.             RSACryptoServiceProvider rsa = DecodePemPublicKey(publicKey);  
    101.             SHA1 sh = new SHA1CryptoServiceProvider();  
    102.             byte[] result = rsa.Encrypt(data, false);  
    103.               
    104.             return Convert.ToBase64String(result);  
    105.         }  
    106.   
    107.         private static string decrypt(byte[] data, string privateKey, string input_charset)  
    108.         {  
    109.             string result = "";  
    110.             RSACryptoServiceProvider rsa = DecodePemPrivateKey(privateKey);  
    111.             SHA1 sh = new SHA1CryptoServiceProvider();  
    112.             byte[] source = rsa.Decrypt(data, false);  
    113.             char[] asciiChars = new char[Encoding.GetEncoding(input_charset).GetCharCount(source, 0, source.Length)];  
    114.             Encoding.GetEncoding(input_charset).GetChars(source, 0, source.Length, asciiChars, 0);  
    115.             result = new string(asciiChars);  
    116.             //result = ASCIIEncoding.ASCII.GetString(source);  
    117.             return result;  
    118.         }  
    119.   
    120.         private static RSACryptoServiceProvider DecodePemPublicKey(String pemstr)  
    121.         {  
    122.             byte[] pkcs8publickkey;  
    123.             pkcs8publickkey = Convert.FromBase64String(pemstr);  
    124.             if (pkcs8publickkey != null)  
    125.             {  
    126.                 RSACryptoServiceProvider rsa = DecodeRSAPublicKey(pkcs8publickkey);  
    127.                 return rsa;  
    128.             }  
    129.             else  
    130.                 return null;  
    131.         }  
    132.           
    133.         private static RSACryptoServiceProvider DecodePemPrivateKey(String pemstr)  
    134.         {  
    135.             byte[] pkcs8privatekey;  
    136.             pkcs8privatekey = Convert.FromBase64String(pemstr);  
    137.             if (pkcs8privatekey != null)  
    138.             {  
    139.                 RSACryptoServiceProvider rsa = DecodePrivateKeyInfo(pkcs8privatekey);  
    140.                 return rsa;  
    141.             }  
    142.             else  
    143.                 return null;  
    144.         }  
    145.   
    146.         private static RSACryptoServiceProvider DecodePrivateKeyInfo(byte[] pkcs8)  
    147.         {  
    148.             byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };  
    149.             byte[] seq = new byte[15];  
    150.   
    151.             MemoryStream mem = new MemoryStream(pkcs8);  
    152.             int lenstream = (int)mem.Length;  
    153.             BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading  
    154.             byte bt = 0;  
    155.             ushort twobytes = 0;  
    156.   
    157.             try  
    158.             {  
    159.                 twobytes = binr.ReadUInt16();  
    160.                 if (twobytes == 0x8130)    //data read as little endian order (actual data order for Sequence is 30 81)  
    161.                     binr.ReadByte();    //advance 1 byte  
    162.                 else if (twobytes == 0x8230)  
    163.                     binr.ReadInt16();    //advance 2 bytes  
    164.                 else  
    165.                     return null;  
    166.   
    167.                 bt = binr.ReadByte();  
    168.                 if (bt != 0x02)  
    169.                     return null;  
    170.   
    171.                 twobytes = binr.ReadUInt16();  
    172.   
    173.                 if (twobytes != 0x0001)  
    174.                     return null;  
    175.   
    176.                 seq = binr.ReadBytes(15);        //read the Sequence OID  
    177.                 if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct  
    178.                     return null;  
    179.   
    180.                 bt = binr.ReadByte();  
    181.                 if (bt != 0x04)    //expect an Octet string  
    182.                     return null;  
    183.   
    184.                 bt = binr.ReadByte();        //read next byte, or next 2 bytes is  0x81 or 0x82; otherwise bt is the byte count  
    185.                 if (bt == 0x81)  
    186.                     binr.ReadByte();  
    187.                 else  
    188.                     if (bt == 0x82)  
    189.                         binr.ReadUInt16();  
    190.                 //------ at this stage, the remaining sequence should be the RSA private key  
    191.   
    192.                 byte[] rsaprivkey = binr.ReadBytes((int)(lenstream - mem.Position));  
    193.                 RSACryptoServiceProvider rsacsp = DecodeRSAPrivateKey(rsaprivkey);  
    194.                 return rsacsp;  
    195.             }  
    196.   
    197.             catch (Exception)  
    198.             {  
    199.                 return null;  
    200.             }  
    201.   
    202.             finally { binr.Close(); }  
    203.   
    204.         }  
    205.   
    206.         private static bool CompareBytearrays(byte[] a, byte[] b)  
    207.         {  
    208.             if (a.Length != b.Length)  
    209.                 return false;  
    210.             int i = 0;  
    211.             foreach (byte c in a)  
    212.             {  
    213.                 if (c != b[i])  
    214.                     return false;  
    215.                 i++;  
    216.             }  
    217.             return true;  
    218.         }  
    219.   
    220.         private static RSACryptoServiceProvider DecodeRSAPublicKey(byte[] publickey)  
    221.         {  
    222.             // encoded OID sequence for  PKCS #1 rsaEncryption szOID_RSA_RSA = "1.2.840.113549.1.1.1"  
    223.             byte[] SeqOID = { 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00 };  
    224.             byte[] seq = new byte[15];  
    225.             // ---------  Set up stream to read the asn.1 encoded SubjectPublicKeyInfo blob  ------  
    226.             MemoryStream mem = new MemoryStream(publickey);  
    227.             BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading  
    228.             byte bt = 0;  
    229.             ushort twobytes = 0;  
    230.   
    231.             try  
    232.             {  
    233.   
    234.                 twobytes = binr.ReadUInt16();  
    235.                 if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)  
    236.                     binr.ReadByte();    //advance 1 byte  
    237.                 else if (twobytes == 0x8230)  
    238.                     binr.ReadInt16();   //advance 2 bytes  
    239.                 else  
    240.                     return null;  
    241.   
    242.                 seq = binr.ReadBytes(15);       //read the Sequence OID  
    243.                 if (!CompareBytearrays(seq, SeqOID))    //make sure Sequence for OID is correct  
    244.                     return null;  
    245.   
    246.                 twobytes = binr.ReadUInt16();  
    247.                 if (twobytes == 0x8103) //data read as little endian order (actual data order for Bit String is 03 81)  
    248.                     binr.ReadByte();    //advance 1 byte  
    249.                 else if (twobytes == 0x8203)  
    250.                     binr.ReadInt16();   //advance 2 bytes  
    251.                 else  
    252.                     return null;  
    253.   
    254.                 bt = binr.ReadByte();  
    255.                 if (bt != 0x00)     //expect null byte next  
    256.                     return null;  
    257.   
    258.                 twobytes = binr.ReadUInt16();  
    259.                 if (twobytes == 0x8130) //data read as little endian order (actual data order for Sequence is 30 81)  
    260.                     binr.ReadByte();    //advance 1 byte  
    261.                 else if (twobytes == 0x8230)  
    262.                     binr.ReadInt16();   //advance 2 bytes  
    263.                 else  
    264.                     return null;  
    265.   
    266.                 twobytes = binr.ReadUInt16();  
    267.                 byte lowbyte = 0x00;  
    268.                 byte highbyte = 0x00;  
    269.   
    270.                 if (twobytes == 0x8102) //data read as little endian order (actual data order for Integer is 02 81)  
    271.                     lowbyte = binr.ReadByte();  // read next bytes which is bytes in modulus  
    272.                 else if (twobytes == 0x8202)  
    273.                 {  
    274.                     highbyte = binr.ReadByte(); //advance 2 bytes  
    275.                     lowbyte = binr.ReadByte();  
    276.                 }  
    277.                 else  
    278.                     return null;  
    279.                 byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };   //reverse byte order since asn.1 key uses big endian order  
    280.                 int modsize = BitConverter.ToInt32(modint, 0);  
    281.   
    282.                 byte firstbyte = binr.ReadByte();  
    283.                 binr.BaseStream.Seek(-1, SeekOrigin.Current);  
    284.   
    285.                 if (firstbyte == 0x00)  
    286.                 {   //if first byte (highest order) of modulus is zero, don't include it  
    287.                     binr.ReadByte();    //skip this null byte  
    288.                     modsize -= 1;   //reduce modulus buffer size by 1  
    289.                 }  
    290.   
    291.                 byte[] modulus = binr.ReadBytes(modsize);   //read the modulus bytes  
    292.   
    293.                 if (binr.ReadByte() != 0x02)            //expect an Integer for the exponent data  
    294.                     return null;  
    295.                 int expbytes = (int)binr.ReadByte();        // should only need one byte for actual exponent data (for all useful values)  
    296.                 byte[] exponent = binr.ReadBytes(expbytes);  
    297.   
    298.                 // ------- create RSACryptoServiceProvider instance and initialize with public key -----  
    299.                 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();  
    300.                 RSAParameters RSAKeyInfo = new RSAParameters();  
    301.                 RSAKeyInfo.Modulus = modulus;  
    302.                 RSAKeyInfo.Exponent = exponent;  
    303.                 RSA.ImportParameters(RSAKeyInfo);  
    304.                 return RSA;  
    305.             }  
    306.             catch (Exception)  
    307.             {  
    308.                 return null;  
    309.             }  
    310.   
    311.             finally { binr.Close(); }  
    312.   
    313.         }  
    314.   
    315.         private static RSACryptoServiceProvider DecodeRSAPrivateKey(byte[] privkey)  
    316.         {  
    317.             byte[] MODULUS, E, D, P, Q, DP, DQ, IQ;  
    318.   
    319.             // ---------  Set up stream to decode the asn.1 encoded RSA private key  ------  
    320.             MemoryStream mem = new MemoryStream(privkey);  
    321.             BinaryReader binr = new BinaryReader(mem);    //wrap Memory Stream with BinaryReader for easy reading  
    322.             byte bt = 0;  
    323.             ushort twobytes = 0;  
    324.             int elems = 0;  
    325.             try  
    326.             {  
    327.                 twobytes = binr.ReadUInt16();  
    328.                 if (twobytes == 0x8130)    //data read as little endian order (actual data order for Sequence is 30 81)  
    329.                     binr.ReadByte();    //advance 1 byte  
    330.                 else if (twobytes == 0x8230)  
    331.                     binr.ReadInt16();    //advance 2 bytes  
    332.                 else  
    333.                     return null;  
    334.   
    335.                 twobytes = binr.ReadUInt16();  
    336.                 if (twobytes != 0x0102)    //version number  
    337.                     return null;  
    338.                 bt = binr.ReadByte();  
    339.                 if (bt != 0x00)  
    340.                     return null;  
    341.   
    342.   
    343.                 //------  all private key components are Integer sequences ----  
    344.                 elems = GetIntegerSize(binr);  
    345.                 MODULUS = binr.ReadBytes(elems);  
    346.   
    347.                 elems = GetIntegerSize(binr);  
    348.                 E = binr.ReadBytes(elems);  
    349.   
    350.                 elems = GetIntegerSize(binr);  
    351.                 D = binr.ReadBytes(elems);  
    352.   
    353.                 elems = GetIntegerSize(binr);  
    354.                 P = binr.ReadBytes(elems);  
    355.   
    356.                 elems = GetIntegerSize(binr);  
    357.                 Q = binr.ReadBytes(elems);  
    358.   
    359.                 elems = GetIntegerSize(binr);  
    360.                 DP = binr.ReadBytes(elems);  
    361.   
    362.                 elems = GetIntegerSize(binr);  
    363.                 DQ = binr.ReadBytes(elems);  
    364.   
    365.                 elems = GetIntegerSize(binr);  
    366.                 IQ = binr.ReadBytes(elems);  
    367.   
    368.                 // ------- create RSACryptoServiceProvider instance and initialize with public key -----  
    369.                 RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();  
    370.                 RSAParameters RSAparams = new RSAParameters();  
    371.                 RSAparams.Modulus = MODULUS;  
    372.                 RSAparams.Exponent = E;  
    373.                 RSAparams.D = D;  
    374.                 RSAparams.P = P;  
    375.                 RSAparams.Q = Q;  
    376.                 RSAparams.DP = DP;  
    377.                 RSAparams.DQ = DQ;  
    378.                 RSAparams.InverseQ = IQ;  
    379.                 RSA.ImportParameters(RSAparams);  
    380.                 return RSA;  
    381.             }  
    382.             catch (Exception)  
    383.             {  
    384.                 return null;  
    385.             }  
    386.             finally { binr.Close(); }  
    387.         }  
    388.   
    389.         private static int GetIntegerSize(BinaryReader binr)  
    390.         {  
    391.             byte bt = 0;  
    392.             byte lowbyte = 0x00;  
    393.             byte highbyte = 0x00;  
    394.             int count = 0;  
    395.             bt = binr.ReadByte();  
    396.             if (bt != 0x02)        //expect integer  
    397.                 return 0;  
    398.             bt = binr.ReadByte();  
    399.   
    400.             if (bt == 0x81)  
    401.                 count = binr.ReadByte();    // data size in next byte  
    402.             else  
    403.                 if (bt == 0x82)  
    404.                 {  
    405.                     highbyte = binr.ReadByte();    // data size in next 2 bytes  
    406.                     lowbyte = binr.ReadByte();  
    407.                     byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };  
    408.                     count = BitConverter.ToInt32(modint, 0);  
    409.                 }  
    410.                 else  
    411.                 {  
    412.                     count = bt;        // we already have the data size  
    413.                 }  
    414.   
    415.   
    416.   
    417.             while (binr.ReadByte() == 0x00)  
    418.             {    //remove high order zeros in data  
    419.                 count -= 1;  
    420.             }  
    421.             binr.BaseStream.Seek(-1, SeekOrigin.Current);        //last ReadByte wasn't a removed zero, so back up a byte  
    422.             return count;  
    423.         }  
    424.  
    425.         #endregion  
    426.  
    427.         #region 解析.net 生成的Pem  
    428.         private static RSAParameters ConvertFromPublicKey(string pemFileConent)  
    429.         {  
    430.   
    431.             byte[] keyData = Convert.FromBase64String(pemFileConent);  
    432.             if (keyData.Length < 162)  
    433.             {  
    434.                 throw new ArgumentException("pem file content is incorrect.");  
    435.             }  
    436.             byte[] pemModulus = new byte[128];  
    437.             byte[] pemPublicExponent = new byte[3];  
    438.             Array.Copy(keyData, 29, pemModulus, 0, 128);  
    439.             Array.Copy(keyData, 159, pemPublicExponent, 0, 3);  
    440.             RSAParameters para = new RSAParameters();  
    441.             para.Modulus = pemModulus;  
    442.             para.Exponent = pemPublicExponent;  
    443.             return para;  
    444.         }  
    445.   
    446.         private static RSAParameters ConvertFromPrivateKey(string pemFileConent)  
    447.         {  
    448.             byte[] keyData = Convert.FromBase64String(pemFileConent);  
    449.             if (keyData.Length < 609)  
    450.             {  
    451.                 throw new ArgumentException("pem file content is incorrect.");  
    452.             }  
    453.   
    454.             int index = 11;  
    455.             byte[] pemModulus = new byte[128];  
    456.             Array.Copy(keyData, index, pemModulus, 0, 128);  
    457.   
    458.             index += 128;  
    459.             index += 2;//141  
    460.             byte[] pemPublicExponent = new byte[3];  
    461.             Array.Copy(keyData, index, pemPublicExponent, 0, 3);  
    462.   
    463.             index += 3;  
    464.             index += 4;//148  
    465.             byte[] pemPrivateExponent = new byte[128];  
    466.             Array.Copy(keyData, index, pemPrivateExponent, 0, 128);  
    467.   
    468.             index += 128;  
    469.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//279  
    470.             byte[] pemPrime1 = new byte[64];  
    471.             Array.Copy(keyData, index, pemPrime1, 0, 64);  
    472.   
    473.             index += 64;  
    474.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//346  
    475.             byte[] pemPrime2 = new byte[64];  
    476.             Array.Copy(keyData, index, pemPrime2, 0, 64);  
    477.   
    478.             index += 64;  
    479.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//412/413  
    480.             byte[] pemExponent1 = new byte[64];  
    481.             Array.Copy(keyData, index, pemExponent1, 0, 64);  
    482.   
    483.             index += 64;  
    484.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//479/480  
    485.             byte[] pemExponent2 = new byte[64];  
    486.             Array.Copy(keyData, index, pemExponent2, 0, 64);  
    487.   
    488.             index += 64;  
    489.             index += ((int)keyData[index + 1] == 64 ? 2 : 3);//545/546  
    490.             byte[] pemCoefficient = new byte[64];  
    491.             Array.Copy(keyData, index, pemCoefficient, 0, 64);  
    492.   
    493.             RSAParameters para = new RSAParameters();  
    494.             para.Modulus = pemModulus;  
    495.             para.Exponent = pemPublicExponent;  
    496.             para.D = pemPrivateExponent;  
    497.             para.P = pemPrime1;  
    498.             para.Q = pemPrime2;  
    499.             para.DP = pemExponent1;  
    500.             para.DQ = pemExponent2;  
    501.             para.InverseQ = pemCoefficient;  
    502.             return para;  
    503.         }  
    504.         #endregion  
    505.   
    506.     }  
    507. }  


    RSA算法测试代码:

    [csharp] view plain copy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Text;  
    4. using RSA.Class;  
    5.   
    6. namespace payapi_demo.RSA  
    7. {  
    8.     class TestRSA  
    9.     {  
    10.         static void Main(string[] arg)  
    11.         {  
    12.   
    13.             /**RSA加密测试,RSA中的密钥对通过SSL工具生成,生成命令如下: 
    14.              * 1 生成RSA私钥: 
    15.              * openssl genrsa -out rsa_private_key.pem 1024 
    16.              *2 生成RSA公钥 
    17.              * openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem 
    18.              * 
    19.              * 3 将RSA私钥转换成PKCS8格式 
    20.              * openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_pub_pk8.pem 
    21.              * 
    22.              * 直接打开rsa_private_key.pem和rsa_pub_pk8.pem文件就可以获取密钥对内容,获取密钥对内容组成字符串时,注意将换行符删除 
    23.              * */  
    24.   
    25.             string publickey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzOqfNunFxFtCZPlq7fO/jWwjqmTvAooVBB4y87BizSZ9dl/F7FpAxYc6MmX2TqivCvvORXgdlYdFWAhzXOnIUv9OGG///WPLe9TMs9kIwAZ/APUXauvC01oFLnYkzwPlAh0tQ1Au9arTE/OG1V1dKgf8BXHLPhKL4BmGBEUZBtQIDAQAB";  
    26.             string privatekey = "MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAPM6p826cXEW0Jk+Wrt87+NbCOqZO8CihUEHjLzsGLNJn12X8XsWkDFhzoyZfZOqK8K+85FeB2Vh0VYCHNc6chS/04Yb//9Y8t71Myz2QjABn8A9Rdq68LTWgUudiTPA+UCHS1DUC71qtMT84bVXV0qB/wFccs+EovgGYYERRkG1AgMBAAECgYEA2PmnPdgnYKnolfvQ9tXiLaBFGPpvGk4grz0r6FB5TF7N4rErwxECunq0xioaowK4HPc40qHd2SvkkWQ7FCjYIDsnMk1oOhxNKn0J3FG0n5Cg1/dFai4eoXHs/nKn3SVZ8YZC1T2cMtN2srectLqNqhB8aQEe8xmykyUlUpg/qmECQQD9vkwjUotG5oUUrOj6etcB4WcdyyH0FtThKgyoJUDwgBv6lGGzWyFJEREvp47IgV+FgC7zeP2mL4MhgnD3tNCZAkEA9WRrjOLBNc379XZpoDsH7rZjobVvhnTrEuRDx/whqZ+vk64EPrEW81XYh647bAbJlFn2jPhY+IUHkrxFEFT/fQJBAMoLNOULXQtfkqgb5odMONeue0Ul8itB4tBHgzyALW1TFPQ6InGGJsLfbCfd67uMCFts7fXAaXhibK/KBdm3iEECQQChwVAjzlUN4nnzk9qMhFz2PcPvFGovd2J9UXpcmRaXeWuDLXIe4Rz/ydaxmWgSDWdTIvoicpIzP31+fBwKZ/0BAkEAy0bh4weKmYF29//rK0sxmY8RtqkQeFrwWbqx1daa1w0DfWlNSvy47zyW1G5/AdZU6JSpXxlxdlM/HSDw+v7kcA==";  
    27.   
    28.             //加密字符串  
    29.             string data = "yibao";  
    30.   
    31.             Console.WriteLine("加密前字符串内容:"+data);  
    32.             //加密  
    33.             string encrypteddata = RSAFromPkcs8.encryptData(data, publickey, "UTF-8");  
    34.             Console.WriteLine("加密后的字符串为:" + encrypteddata);  
    35.             Console.WriteLine("解密后的字符串内容:" + RSAFromPkcs8.decryptData(encrypteddata, privatekey, "UTF-8"));  
    36.   
    37.             Console.WriteLine("***********");  
    38.   
    39.             //解密  
    40.             string endata = "LpnnvnfA72VnyjboX/OsCPO6FOFXeEnnsKkI7aAEQyVAPfCTfQ43ZYVZVqnADDPMW7VhBXJWyQMAGw2Fh9sS/XLHmO5XW94Yehci6JrJMynePgtIiDysjNA+UlgSTC/MlResNrBm/4MMSPvq0qLwScgpZDynhLsVZk+EQ6G8wgA=";  
    41.             string datamw = RSAFromPkcs8.decryptData(endata, privatekey, "UTF-8");  
    42.             Console.WriteLine("静态加密后的字符串为:" + endata);  
    43.             Console.WriteLine("解密后的字符串内容:" + datamw);  
    44.   
    45.             //签名  
    46.             string signdata = "YB010000001441234567286038508081299";  
    47.             Console.WriteLine("签名前的字符串内容:" + signdata);  
    48.             string sign = RSAFromPkcs8.sign(signdata, privatekey, "UTF-8");  
    49.             Console.WriteLine("签名后的字符串:" + sign);  
    50.   
    51.             Console.ReadLine();  
    52.         }  
    53.     }  
    54. }  
  • 相关阅读:
    9. MySQL
    python第四课
    python第三课pycharm的使用
    python第二课
    python第一课
    Python3基础-网络编程(8)
    Python3基础-异常处理(7)
    Python3基础-面向对象(6)
    Python3基础-模块和包(5)
    Python3基础-文件处理(4)
  • 原文地址:https://www.cnblogs.com/webenh/p/6929758.html
Copyright © 2011-2022 走看看