Exemple #1
0
        public byte[] RSADepad(byte[] Packetin)
        {
            RSAWriter writer = new RSAWriter(Packetin.Length / 3);

            writer.Write(Packetin, Packetin.Length / 3, Packetin.Length / 3);
            return(writer.GetBuffer());
        }
Exemple #2
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());
        }
Exemple #3
0
        public byte[] EncodeMythicRSAPacket(byte[] inMessage)
        {
            if (inMessage.Length == 0)
            {
                return(null);
            }

            int curInPtr  = 2;            //Input starts at byte 3
            int curOutPtr = 2;            //Output starts at byte 3 too

            RSAWriter writer = new RSAWriter();

            writer.WriteByte(0);
            writer.WriteByte(0);            //add length at end

            int blockLen = BLOCKLEN;

            byte[] paddedBlock = new byte[200];            //normaly it s 189 byte....
            int    cryptedBlockLen;

            while (curInPtr < inMessage.Length)
            {
                blockLen = BLOCKLEN;
                if (curInPtr + blockLen > inMessage.Length)
                {
                    blockLen = inMessage.Length - curInPtr;
                }

                //Pad a block of data
                MemoryStream stream = new MemoryStream();
                stream.Write(inMessage, curInPtr, blockLen);
                paddedBlock = RSAPad(stream.GetBuffer());


                curInPtr += blockLen;

                cryptedBlockLen = paddedBlock.Length - curOutPtr - 2;

                byte[] packet = RSAExptmod(paddedBlock, eRSAKeyFormat.PK_PUBLIC);

                writer.WriteShort((ushort)cryptedBlockLen);
                writer.Write(packet, 0, cryptedBlockLen);

                curOutPtr += cryptedBlockLen + 2;
            }
            writer.Seek(0, SeekOrigin.Begin);
            writer.WriteShort((ushort)curOutPtr);

            return(writer.GetBuffer());
        }
Exemple #4
0
        public byte[] DecodeMythicRSAPacket(byte[] inMessage)
        {
            if (inMessage == null)
            {
                return(null);
            }

            int curInPtr = 2;

            byte[] decryptedBlock = new byte[500];
            byte[] depaddedBlock  = new byte[500];

            RSAWriter writer = new RSAWriter();

            writer.WriteByte(0);
            writer.WriteByte(0);            //add length at end

            while (curInPtr < inMessage.Length)
            {
                if (curInPtr + 2 > inMessage.Length)
                {
                    return(null);
                }
                int curBlockLen = (inMessage[curInPtr] << 8) + inMessage[curInPtr + 1];
                curInPtr += 2;
                if (curBlockLen > 0)
                {
                    MemoryStream stream = new MemoryStream();
                    stream.Write(inMessage, curInPtr, curBlockLen);
                    decryptedBlock = RSAExptmod(stream.GetBuffer(), eRSAKeyFormat.PK_PRIVATE);

                    depaddedBlock = RSADepad(decryptedBlock);

                    writer.Write(depaddedBlock, 0, depaddedBlock.Length);
                }
                curInPtr += curBlockLen;
            }

            writer.Seek(0, SeekOrigin.Begin);
            writer.WriteShort((ushort)writer.Length);

            return(writer.GetBuffer());
        }
Exemple #5
0
        public byte[] RSAPad(byte[] Packetin)
        {
            if (Packetin.Length > 512)
            {
                return(null);
            }

            RSAWriter writer = new RSAWriter(Packetin.Length * 3);

            writer.WriteByte(0xFF);

            byte[] buf      = new byte[Packetin.Length * 2];
            Random m_random = new Random((int)DateTime.Now.Ticks);

            m_random.NextBytes(buf);

            writer.Write(buf, 0, Packetin.Length - 1);
            writer.Write(Packetin, 0, Packetin.Length);
            writer.Write(buf, Packetin.Length - 1, Packetin.Length - 1);
            writer.WriteByte(0xFF);

            return(writer.GetBuffer());
        }
        public byte[] DecodeMythicRSAPacket(byte[]inMessage)
        {
            if(inMessage==null) return null;

            int curInPtr = 2;

            byte[]decryptedBlock = new byte[500];
            byte[]depaddedBlock = new byte[500];

            RSAWriter writer = new RSAWriter();
            writer.WriteByte(0);
            writer.WriteByte(0);//add length at end

            while(curInPtr < inMessage.Length)
            {
                if(curInPtr+2 > inMessage.Length)
                    return null;
                int curBlockLen = (inMessage[curInPtr]<<8)+inMessage[curInPtr+1];
                curInPtr+=2;
                if(curBlockLen>0)
                {
                    MemoryStream stream = new MemoryStream();
                    stream.Write(inMessage,curInPtr,curBlockLen);
                    decryptedBlock = RSAExptmod( stream.GetBuffer(),eRSAKeyFormat.PK_PRIVATE);

                    depaddedBlock = RSADepad(decryptedBlock);

                    writer.Write(depaddedBlock,0,depaddedBlock.Length);
                }
                curInPtr+=curBlockLen;
            }

            writer.Seek(0, SeekOrigin.Begin);
            writer.WriteShort((ushort)writer.Length);

            return writer.GetBuffer();
        }
Exemple #7
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());
        }
        public byte[] EncodeMythicRSAPacket(byte[] inMessage)
        {
            if(inMessage.Length==0) return null;

            int curInPtr = 2; //Input starts at byte 3
            int curOutPtr = 2; //Output starts at byte 3 too

            RSAWriter writer = new RSAWriter();
            writer.WriteByte(0);
            writer.WriteByte(0);//add length at end

            int blockLen = BLOCKLEN;
            byte[] paddedBlock = new byte[200];//normaly it s 189 byte....
            int cryptedBlockLen;
            while(curInPtr < inMessage.Length)
            {
                blockLen = BLOCKLEN;
                if(curInPtr+blockLen > inMessage.Length)
                    blockLen = inMessage.Length - curInPtr;

                //Pad a block of data
                MemoryStream stream = new MemoryStream();
                stream.Write(inMessage,curInPtr,blockLen);
                paddedBlock =RSAPad(stream.GetBuffer());

                curInPtr += blockLen;

                cryptedBlockLen = paddedBlock.Length - curOutPtr - 2;

                byte[] packet = RSAExptmod( paddedBlock,eRSAKeyFormat.PK_PUBLIC);

                writer.WriteShort((ushort)cryptedBlockLen);
                writer.Write( packet,0,cryptedBlockLen);

                curOutPtr += cryptedBlockLen + 2;

            }
            writer.Seek(0, SeekOrigin.Begin);
            writer.WriteShort((ushort)curOutPtr);

            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();
        }
        public byte[] RSAPad(byte[] Packetin)
        {
            if (Packetin.Length > 512)
            {
                return null;
            }

            RSAWriter writer = new RSAWriter(Packetin.Length*3);

            writer.WriteByte(0xFF);

            byte[] buf =new byte[Packetin.Length*2];
            Random m_random = new Random((int)DateTime.Now.Ticks);
            m_random.NextBytes(buf);

            writer.Write(buf,0,Packetin.Length-1);
            writer.Write(Packetin,0,Packetin.Length);
            writer.Write(buf,Packetin.Length-1,Packetin.Length-1);
            writer.WriteByte(0xFF);

            return writer.GetBuffer();
        }
 public byte[] RSADepad(byte[] Packetin)
 {
     RSAWriter writer = new RSAWriter(Packetin.Length/3);
     writer.Write(Packetin,Packetin.Length/3,Packetin.Length/3);
     return writer.GetBuffer();
 }
        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();
        }