Example #1
0
        /// <summary>
        /// rips apart rawKey into public byte [] for RsaParameters class
        /// </summary>
        public Rsa(byte [] rawKey)
        {
            this.rawKey = rawKey;             //596

            pks          = new PUBLICKEYSTRUC();
            pks.bType    = rawKey[0];                        //7
            pks.bVersion = rawKey[1];                        //2
            pks.reserved = BitConverter.ToUInt16(rawKey, 2); //0
            pks.aiKeyAlg = BitConverter.ToUInt32(rawKey, 4); //41984

            kb = (KeyBlob)pks.bType;                         //PRIVATE
            c  = (Calg)pks.aiKeyAlg;                         //RSA_KEYX

            if (kb != KeyBlob.PUBLICKEYBLOB && kb != KeyBlob.PRIVATEKEYBLOB)
            {
                throw new Exception("unsupported blob type");
            }

            rpk        = new RSAPUBKEY();
            rpk.magic  = BitConverter.ToUInt32(rawKey, 8);  //843141970
            rpk.bitlen = BitConverter.ToUInt32(rawKey, 12); //1024
            rpk.pubexp = BitConverter.ToUInt32(rawKey, 16); //65537
            uint byteLen = rpk.bitlen / 8;                  //128

            this.SetSizeAndPosition(rpk.bitlen);

            //public
            Modulus  = Format.GetBytes(this.rawKey, modulusPos, modulusLen, true);
            Exponent = Format.GetBytes(this.rawKey, exponentPos, exponentLen, true);
            //private
            if (kb == KeyBlob.PRIVATEKEYBLOB)
            {
                this.P        = Format.GetBytes(this.rawKey, prime1Pos, prime1Len, true);
                this.Q        = Format.GetBytes(this.rawKey, prime2Pos, prime2Len, true);
                this.DP       = Format.GetBytes(this.rawKey, exponent1Pos, exponent1Len, true);
                this.DQ       = Format.GetBytes(this.rawKey, exponent2Pos, exponent2Len, true);
                this.InverseQ = Format.GetBytes(this.rawKey, coefficientPos, coefficientLen, true);
                this.D        = Format.GetBytes(this.rawKey, privateExponentPos, privateExponentLen, true);
            }
            else
            {
                this.P        = null;
                this.Q        = null;
                this.DP       = null;
                this.DQ       = null;
                this.InverseQ = null;
                this.D        = null;
            }
        }
Example #2
0
        /// <summary>
        /// rips apart rawKey into public byte [] for RsaParameters class
        /// </summary>
        public Rsa(byte [] rawKey)
        {
            this.rawKey = rawKey; //596

            pks = new PUBLICKEYSTRUC();
            pks.bType = rawKey[0]; //7
            pks.bVersion = rawKey[1]; //2
            pks.reserved = BitConverter.ToUInt16(rawKey, 2); //0
            pks.aiKeyAlg = BitConverter.ToUInt32(rawKey, 4); //41984

            kb = (KeyBlob) pks.bType; //PRIVATE
            c = (Calg) pks.aiKeyAlg; //RSA_KEYX

            if(kb != KeyBlob.PUBLICKEYBLOB && kb != KeyBlob.PRIVATEKEYBLOB)
                throw new Exception("unsupported blob type");

            rpk = new RSAPUBKEY();
            rpk.magic = BitConverter.ToUInt32(rawKey, 8); //843141970
            rpk.bitlen = BitConverter.ToUInt32(rawKey, 12); //1024
            rpk.pubexp = BitConverter.ToUInt32(rawKey, 16); //65537
            uint byteLen = rpk.bitlen / 8; //128

            this.SetSizeAndPosition(rpk.bitlen);

            //public
            Modulus = Format.GetBytes(this.rawKey, modulusPos, modulusLen, true);
            Exponent = Format.GetBytes(this.rawKey, exponentPos, exponentLen, true);
            //private
            if(kb == KeyBlob.PRIVATEKEYBLOB)
            {
                this.P = Format.GetBytes(this.rawKey, prime1Pos, prime1Len, true);
                this.Q = Format.GetBytes(this.rawKey, prime2Pos, prime2Len, true);
                this.DP = Format.GetBytes(this.rawKey, exponent1Pos, exponent1Len, true);
                this.DQ = Format.GetBytes(this.rawKey, exponent2Pos, exponent2Len, true);
                this.InverseQ = Format.GetBytes(this.rawKey, coefficientPos, coefficientLen, true);
                this.D = Format.GetBytes(this.rawKey, privateExponentPos, privateExponentLen, true);
            }
            else
            {
                this.P = null;
                this.Q = null;
                this.DP = null;
                this.DQ = null;
                this.InverseQ = null;
                this.D = null;
            }
        }
Example #3
0
        public void BuildRawKey(bool privateKey)
        {
            //build up rawKey byte[]
            uint rsaMagic = 0;
            int  caSize   = 0;
            uint bitLen   = (uint)this.Modulus.Length * 8;

            if (privateKey == true)
            {
                kb       = KeyBlob.PRIVATEKEYBLOB;
                caSize   = 20 + (9 * ((int)bitLen / 16));
                rsaMagic = 0x32415352; //ASCII encoding of "RSA2"
            }
            else                       //public
            {
                kb       = KeyBlob.PUBLICKEYBLOB;
                caSize   = 20 + ((int)bitLen / 8);
                rsaMagic = 0x31415352;                 //ASCII encoding of "RSA1"
            }

            rawKey = new byte[caSize];

            //PUBLICKEYSTRUC
            rawKey[0] = (byte)kb;             //bType
            rawKey[1] = (byte)2;              //bVersion
            //reserved 2,3
            c = Calg.RSA_KEYX;
            byte [] baKeyAlg = BitConverter.GetBytes((uint)c);            //aiKeyAlg
            Buffer.BlockCopy(baKeyAlg, 0, rawKey, 4, 4);

            pks          = new PUBLICKEYSTRUC();
            pks.bType    = rawKey[0];
            pks.bVersion = rawKey[1];
            pks.reserved = BitConverter.ToUInt16(rawKey, 2);
            pks.aiKeyAlg = BitConverter.ToUInt32(rawKey, 4);

            //RSAPUBKEY
            byte [] baMagic = BitConverter.GetBytes(rsaMagic);           //magic
            Buffer.BlockCopy(baMagic, 0, rawKey, 8, 4);
            byte [] baBitlen = BitConverter.GetBytes(bitLen);            //bitlen
            Buffer.BlockCopy(baBitlen, 0, rawKey, 12, 4);

            this.SetSizeAndPosition(bitLen);
            Format.SetBytes(this.rawKey, exponentPos, exponentLen, this.Exponent, true);             //pubexp

            rpk        = new RSAPUBKEY();
            rpk.magic  = BitConverter.ToUInt32(rawKey, 8);
            rpk.bitlen = BitConverter.ToUInt32(rawKey, 12);
            rpk.pubexp = BitConverter.ToUInt32(rawKey, 16);
            uint byteLen = rpk.bitlen / 8;

            //public
            Format.SetBytes(this.rawKey, modulusPos, modulusLen, this.Modulus, true);
            Format.SetBytes(this.rawKey, exponentPos, exponentLen, this.Exponent, true);
            //private
            if (privateKey == true)
            {
                Format.SetBytes(this.rawKey, prime1Pos, prime1Len, this.P, true);
                Format.SetBytes(this.rawKey, prime2Pos, prime2Len, this.Q, true);
                Format.SetBytes(this.rawKey, exponent1Pos, exponent1Len, this.DP, true);
                Format.SetBytes(this.rawKey, exponent2Pos, exponent2Len, this.DQ, true);
                Format.SetBytes(this.rawKey, coefficientPos, coefficientLen, this.InverseQ, true);
                Format.SetBytes(this.rawKey, privateExponentPos, privateExponentLen, this.D, true);
            }
            else
            {
                this.P        = null;
                this.Q        = null;
                this.DP       = null;
                this.DQ       = null;
                this.InverseQ = null;
                this.D        = null;
            }
        }
Example #4
0
        public void BuildRawKey(bool privateKey)
        {
            //build up rawKey byte[]
            uint rsaMagic = 0;
            int caSize = 0;
            uint bitLen = (uint) this.Modulus.Length * 8;

            if(privateKey == true)
            {
                kb = KeyBlob.PRIVATEKEYBLOB;
                caSize = 20 + (9 * ((int)bitLen / 16));
                rsaMagic = 0x32415352; //ASCII encoding of "RSA2"
            }
            else //public
            {
                kb = KeyBlob.PUBLICKEYBLOB;
                caSize = 20 + ((int)bitLen / 8);
                rsaMagic = 0x31415352; //ASCII encoding of "RSA1"
            }

            rawKey = new byte[caSize];

            //PUBLICKEYSTRUC
            rawKey[0] = (byte) kb; //bType
            rawKey[1] = (byte) 2; //bVersion
            //reserved 2,3
            c = Calg.RSA_KEYX;
            byte [] baKeyAlg = BitConverter.GetBytes((uint)c);//aiKeyAlg
            Buffer.BlockCopy(baKeyAlg, 0, rawKey, 4, 4);

            pks = new PUBLICKEYSTRUC();
            pks.bType = rawKey[0];
            pks.bVersion = rawKey[1];
            pks.reserved = BitConverter.ToUInt16(rawKey, 2);
            pks.aiKeyAlg = BitConverter.ToUInt32(rawKey, 4);

            //RSAPUBKEY
            byte [] baMagic = BitConverter.GetBytes(rsaMagic);//magic
            Buffer.BlockCopy(baMagic, 0, rawKey, 8, 4);
            byte [] baBitlen = BitConverter.GetBytes(bitLen);//bitlen
            Buffer.BlockCopy(baBitlen, 0, rawKey, 12, 4);

            this.SetSizeAndPosition(bitLen);
            Format.SetBytes(this.rawKey, exponentPos, exponentLen, this.Exponent, true); //pubexp

            rpk = new RSAPUBKEY();
            rpk.magic = BitConverter.ToUInt32(rawKey, 8);
            rpk.bitlen = BitConverter.ToUInt32(rawKey, 12);
            rpk.pubexp = BitConverter.ToUInt32(rawKey, 16);
            uint byteLen = rpk.bitlen / 8;

            //public
            Format.SetBytes(this.rawKey, modulusPos, modulusLen, this.Modulus, true);
            Format.SetBytes(this.rawKey, exponentPos, exponentLen, this.Exponent, true);
            //private
            if(privateKey == true)
            {
                Format.SetBytes(this.rawKey, prime1Pos, prime1Len, this.P, true);
                Format.SetBytes(this.rawKey, prime2Pos, prime2Len, this.Q, true);
                Format.SetBytes(this.rawKey, exponent1Pos, exponent1Len, this.DP, true);
                Format.SetBytes(this.rawKey, exponent2Pos, exponent2Len, this.DQ, true);
                Format.SetBytes(this.rawKey, coefficientPos, coefficientLen, this.InverseQ, true);
                Format.SetBytes(this.rawKey, privateExponentPos, privateExponentLen, this.D, true);
            }
            else
            {
                this.P = null;
                this.Q = null;
                this.DP = null;
                this.DQ = null;
                this.InverseQ = null;
                this.D = null;
            }
        }