欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

RSA加密方法

程序员文章站 2022-06-19 16:41:42
/// /// RSA加密 /// /// /// /// public static str ......

/// <summary>
        /// rsa加密
        /// </summary>
        /// <param name="data"></param>
        /// <param name="privatekey"></param>
        /// <returns></returns>

        public static string rsasigncharset(string data, string privatekey)
        {

            byte[] signaturebytes = null;

            rsacryptoserviceprovider rsacsp = loadcertificatestring(privatekey, "");

            byte[] databytes = encoding.utf8.getbytes(data);

            signaturebytes = rsacsp.signdata(databytes, "sha256");
            return convert.tobase64string(signaturebytes);
        }

        private static rsacryptoserviceprovider loadcertificatestring(string strkey, string signtype)
        {
            byte[] data = null;
            //读取带
            //ata = encoding.default.getbytes(strkey);
            data = convert.frombase64string(strkey);
            //data = getpem("rsa private key", data);
            try
            {
                rsacryptoserviceprovider rsa = decodersaprivatekey(data, signtype);
                return rsa;
            }
            catch (exception ex)
            {
                //    throw new aopexception("encryptcontent = woshihaoren,zheshiyigeceshi,wanerde", ex);
            }
            return null;
        }

        private static rsacryptoserviceprovider decodersaprivatekey(byte[] privkey, string signtype)
        {
            byte[] modulus, e, d, p, q, dp, dq, iq;

            // --------- set up stream to decode the asn.1 encoded rsa private key ------
            memorystream mem = new memorystream(privkey);
            binaryreader binr = new binaryreader(mem);  //wrap memory stream with binaryreader for easy reading
            byte bt = 0;
            ushort twobytes = 0;
            int elems = 0;
            try
            {
                twobytes = binr.readuint16();
                if (twobytes == 0x8130) //data read as little endian order (actual data order for sequence is 30 81)
                    binr.readbyte();    //advance 1 byte
                else if (twobytes == 0x8230)
                    binr.readint16();    //advance 2 bytes
                else
                    return null;

                twobytes = binr.readuint16();
                if (twobytes != 0x0102) //version number
                    return null;
                bt = binr.readbyte();
                if (bt != 0x00)
                    return null;


                //------ all private key components are integer sequences ----
                elems = getintegersize(binr);
                modulus = binr.readbytes(elems);

                elems = getintegersize(binr);
                e = binr.readbytes(elems);

                elems = getintegersize(binr);
                d = binr.readbytes(elems);

                elems = getintegersize(binr);
                p = binr.readbytes(elems);

                elems = getintegersize(binr);
                q = binr.readbytes(elems);

                elems = getintegersize(binr);
                dp = binr.readbytes(elems);

                elems = getintegersize(binr);
                dq = binr.readbytes(elems);

                elems = getintegersize(binr);
                iq = binr.readbytes(elems);


                // ------- create rsacryptoserviceprovider instance and initialize with public key -----
                cspparameters cspparameters = new cspparameters();
                cspparameters.flags = cspproviderflags.usemachinekeystore;

                int bitlen = 1024;
                if ("rsa2".equals(signtype))
                {
                    bitlen = 2048;
                }

                rsacryptoserviceprovider rsa = new rsacryptoserviceprovider(bitlen, cspparameters);
                rsaparameters rsaparams = new rsaparameters();
                rsaparams.modulus = modulus;
                rsaparams.exponent = e;
                rsaparams.d = d;
                rsaparams.p = p;
                rsaparams.q = q;
                rsaparams.dp = dp;
                rsaparams.dq = dq;
                rsaparams.inverseq = iq;
                rsa.importparameters(rsaparams);
                return rsa;
            }
            catch (exception ex)
            {
                return null;
            }
            finally
            {
                binr.close();
            }
        }

        private static int getintegersize(binaryreader binr)
        {
            byte bt = 0;
            byte lowbyte = 0x00;
            byte highbyte = 0x00;
            int count = 0;
            bt = binr.readbyte();
            if (bt != 0x02)        //expect integer
                return 0;
            bt = binr.readbyte();

            if (bt == 0x81)
                count = binr.readbyte();    // data size in next byte
            else
                if (bt == 0x82)
                {
                    highbyte = binr.readbyte();    // data size in next 2 bytes
                    lowbyte = binr.readbyte();
                    byte[] modint = { lowbyte, highbyte, 0x00, 0x00 };
                    count = bitconverter.toint32(modint, 0);
                }
                else
                {
                    count = bt;        // we already have the data size
                }

            while (binr.readbyte() == 0x00)
            {    //remove high order zeros in data
                count -= 1;
            }
            binr.basestream.seek(-1, seekorigin.current);        //last readbyte wasn't a removed zero, so back up a byte
            return count;
        }