示例#1
0
        private byte[] RSAExport(RSAParam key, eRSAKeyFormat format)
        {
            RSAWriter writer = new RSAWriter();

            /* output key type */
            writer.WriteByte((byte)format);

            /* output modulus  and exponent*/
            writer.WriteBignum(key.Modulus);
            writer.WriteBignum(key.Exponent);

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE)
            {
                writer.WriteBignum(key.D);
            }

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED)
            {
                writer.WriteBignum(key.DQ);
                writer.WriteBignum(key.DP);
                writer.WriteBignum(key.pQ);
                writer.WriteBignum(key.qP);
                writer.WriteBignum(key.P);
                writer.WriteBignum(key.Q);
            }

            return(writer.GetBuffer());
        }
示例#2
0
        public bool RSAImport(byte[] exportedkey)
        {
            RSAReader reader = new RSAReader(exportedkey);

            eRSAKeyFormat format = (eRSAKeyFormat)reader.ReadByte();
            RSAParam      key    = new RSAParam();

            /* input modulus  and exponent*/
            key.Modulus  = reader.ReadBignum();
            key.Exponent = reader.ReadBignum();

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE)
            {
                key.D = reader.ReadBignum();
            }

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED)
            {
                key.DQ = reader.ReadBignum();
                key.DP = reader.ReadBignum();
                key.pQ = reader.ReadBignum();
                key.qP = reader.ReadBignum();
                key.P  = reader.ReadBignum();
                key.Q  = reader.ReadBignum();
            }

            //skip version at end of buffer

            this.ImportParam(key);

            return(true);
        }
示例#3
0
        //for that it s strange because it s a quite different of DecryptValue and encryptvalue
        //but maybe it s the same thanks to math ;)
        //so need to do some math to check...
        public byte[] RSAExptmod(byte[] PacketIn, eRSAKeyFormat format)
        {
            BigInteger tmp, tmpa, tmpb;

            //todo more check key generate format packet in and out null...

            tmp = new BigInteger(PacketIn);

            /* are we using the private exponent */
            if (format == eRSAKeyFormat.PK_PRIVATE)
            {
                // m1 = c^dp mod p
                tmpa = tmp.ModPow(dp, p);
                tmpb = tmp.ModPow(dq, q);
                tmp  = (tmpa * qP + tmpb * pQ).ModPow(0, n);
            }
            else
            {
                tmp = tmp.ModPow(e, n);
            }

            /* convert it */
            return(tmp.GetBytes());
        }
示例#4
0
        public byte[] Export(eRSAKeyFormat type)
        {
            RSAWriter writer = new RSAWriter();

            byte[] exported_key_buffer;

            /* store packet header */
            /* store version number */
            writer.WriteByte(0x91);
            writer.WriteByte(0x00);            //low endian of version

            /* store section and subsection */
            writer.WriteByte(0x00);
            writer.WriteByte(0x00);

            RSAParam param = this.ExportParam(false);

            exported_key_buffer = RSAExport(param, eRSAKeyFormat.PK_PRIVATE_OPTIMIZED);
            int outlen = exported_key_buffer.Length;

            writer.WriteShort((ushort)outlen);
            writer.WriteShort((ushort)outlen);            //need to be get out?


            writer.Write(exported_key_buffer, 0, exported_key_buffer.Length);

            //protocole version
            writer.WriteByte(0);
            writer.WriteByte(0);
            writer.WriteByte(0);
            writer.WriteByte(1);
            writer.WriteByte(0);
            writer.WriteByte(1);

            return(writer.GetBuffer());
        }
        private byte[] RSAExport(RSAParam key,eRSAKeyFormat format)
        {
            RSAWriter writer = new RSAWriter();

            /* output key type */
            writer.WriteByte((byte)format);

            /* output modulus  and exponent*/
            writer.WriteBignum(key.Modulus);
            writer.WriteBignum(key.Exponent);

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED || format == eRSAKeyFormat.PK_PRIVATE)
            {
                writer.WriteBignum(key.D);
            }

            if (format == eRSAKeyFormat.PK_PRIVATE_OPTIMIZED)
            {
                writer.WriteBignum(key.DQ);
                writer.WriteBignum(key.DP);
                writer.WriteBignum(key.pQ);
                writer.WriteBignum(key.qP);
                writer.WriteBignum(key.P);
                writer.WriteBignum(key.Q);
            }

            return writer.GetBuffer();
        }
        //for that it s strange because it s a quite different of DecryptValue and encryptvalue
        //but maybe it s the same thanks to math ;)
        //so need to do some math to check...
        public byte[] RSAExptmod(byte[] PacketIn, eRSAKeyFormat format)
        {
            BigInteger tmp, tmpa, tmpb;

            //todo more check key generate format packet in and out null...

            tmp = new BigInteger(PacketIn);

            /* are we using the private exponent */
            if (format == eRSAKeyFormat.PK_PRIVATE )
            {
                // m1 = c^dp mod p
                tmpa = tmp.ModPow (dp, p);
                tmpb = tmp.ModPow (dq, q);
                tmp = (tmpa * qP + tmpb*pQ).ModPow (0, n);
            }
            else
            {
                tmp = tmp.ModPow (e, n);
            }

            /* convert it */
            return tmp.GetBytes();
        }
        public byte[] Export(eRSAKeyFormat type)
        {
            RSAWriter writer = new RSAWriter();
            byte[] exported_key_buffer;

            /* store packet header */
            /* store version number */
            writer.WriteByte(0x91);
            writer.WriteByte(0x00);//low endian of version

            /* store section and subsection */
            writer.WriteByte(0x00);
            writer.WriteByte(0x00);

            RSAParam param = this.ExportParam(false);

            exported_key_buffer = RSAExport(param,eRSAKeyFormat.PK_PRIVATE_OPTIMIZED);
            int outlen = exported_key_buffer.Length;
            writer.WriteShort((ushort)outlen);
            writer.WriteShort((ushort)outlen);//need to be get out?

            writer.Write(exported_key_buffer, 0, exported_key_buffer.Length);

            //protocole version
            writer.WriteByte(0);
            writer.WriteByte(0);
            writer.WriteByte(0);
            writer.WriteByte(1);
            writer.WriteByte(0);
            writer.WriteByte(1);

            return writer.GetBuffer();
        }