Example #1
0
        private byte[] pkcs1unpad(byte[] Src, int N, Pkcs1PadType Type)
        {
            int I = 0;

            while (I < Src.Length && Src[I] == 0)
            {
                ++I;
            }

            if (Src.Length - I != N - 1 || Src[I] > 2)
            {
                Console.WriteLine("PKCS#1 unpad: i={0}, expected src[i]==[0,1,2], got src[i]={1}", I, Src[I].ToString("X"));

                return(null);
            }

            ++I;

            while (Src[I] != 0)
            {
                if (++I >= Src.Length)
                {
                    Console.WriteLine("PKCS#1 unpad: i={0}, src[i-1]!=0 (={1})", I, Src[I - 1].ToString("X"));
                }
            }

            byte[] Bytes = new byte[Src.Length - I - 1];

            for (int P = 0; ++I < Src.Length; P++)
            {
                Bytes[P] = Src[I];
            }

            return(Bytes);
        }
Example #2
0
File: RsaKey.cs Project: sgf/Yupi
        private byte[] DoDecrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                BigInteger c = new BigInteger(src);

                BigInteger m = method(c);

                if (m == 0)
                {
                    return(null);
                }

                int    bl   = GetBlockSize();
                byte[] data = m.ToByteArray();

                Array.Reverse(data);

                byte[] bytes = Pkcs1Unpad(data, bl, type);

                return(bytes);
            }
            catch
            {
                return(null);
            }
        }
Example #3
0
        private byte[] pkcs1pad(byte[] src, int n, Pkcs1PadType type)
        {
            byte[] bytes = new byte[n];

            int i = src.Length - 1;

            while (i >= 0 && n > 11)
            {
                bytes[--n] = src[i--];
            }

            bytes[--n] = 0;
            while (n > 2)
            {
                byte x = 0;
                switch (type)
                {
                case Pkcs1PadType.FullByte: x = 0xFF; break;

                case Pkcs1PadType.RandomByte: x = Randomizer.NextByte(1, 255); break;
                }
                bytes[--n] = x;
            }

            bytes[--n] = (byte)type;
            bytes[--n] = 0;
            return(bytes);
        }
Example #4
0
        private byte[] DoEncrypt(DoCalculateionDelegate Method, byte[] Src, Pkcs1PadType type)
        {
            try
            {
                int        Bl          = GetBlockSize();
                byte[]     PaddedBytes = pkcs1pad(Src, Bl, type);
                BigInteger M           = new BigInteger(PaddedBytes);

                if (M == 0)
                {
                    return(null);
                }

                BigInteger C = Method(M);

                if (C == 0)
                {
                    return(null);
                }

                return(C.getBytes());
            }
            catch
            {
                return(null);
            }
        }
Example #5
0
        private byte[] pkcs1pad(byte[] Src, int N, Pkcs1PadType Type)
        {
            byte[] Bytes = new byte[N];
            int    I     = Src.Length - 1;

            while (I >= 0 && N > 11)
            {
                Bytes[--N] = Src[I--];
            }

            Bytes[--N] = 0;

            while (N > 2)
            {
                byte x = 0;

                switch (Type)
                {
                case Pkcs1PadType.FullByte: x = 0xFF; break;

                case Pkcs1PadType.RandomByte: x = Randomizer.NextByte(1, 255); break;
                }

                Bytes[--N] = x;
            }

            Bytes[--N] = (byte)Type;
            Bytes[--N] = 0;

            return(Bytes);
        }
Example #6
0
        private byte[] Pkcs1pad(byte[] src, int n, Pkcs1PadType type)
        {
            var bytes = new byte[n];

            var i = src.Length - 1;

            while (i >= 0 && n > 11)
            {
                bytes[--n] = src[i--];
            }

            bytes[--n] = 0;
            while (n > 2)
            {
                byte x = 0;
                switch (type)
                {
                case Pkcs1PadType.FullByte: x = 0xFF; break;

                case Pkcs1PadType.RandomByte: x = EncryptionUtilities.NextByte(1, 255); break;
                }
                bytes[--n] = x;
            }

            bytes[--n] = (byte)type;
            bytes[--n] = 0;
            return(bytes);
        }
Example #7
0
        private byte[] DoEncrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                int bl = this.GetBlockSize();

                byte[]     paddedBytes = this.pkcs1pad(src, bl, type);
                BigInteger m           = new BigInteger(paddedBytes);
                if (m == 0)
                {
                    return(null);
                }

                BigInteger c = method(m);
                if (c == 0)
                {
                    return(null);
                }

                return(c.getBytes());
            }
            catch
            {
                return(null);
            }
        }
Example #8
0
File: RsaKey.cs Project: sgf/Yupi
        private byte[] DoEncrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                int bl = GetBlockSize();

                byte[] paddedBytes = Pkcs1Pad(src, bl, type);

                Array.Reverse(paddedBytes);

                BigInteger m = new BigInteger(paddedBytes);

                if (m == 0)
                {
                    return(null);
                }

                BigInteger c    = method(m);
                byte[]     data = c.ToByteArray();

                if (data[data.Length - 1] == 0)
                {
                    Array.Resize(ref data, data.Length - 1);
                }

                Array.Reverse(data);

                return(c == 0 ? null : data);
            }
            catch
            {
                return(null);
            }
        }
Example #9
0
        private byte[] pkcs1unpad(byte[] src, int n, Pkcs1PadType type)
        {
            int i = 0;

            while (i < src.Length && src[i] == 0)
            {
                ++i;
            }

            if (src.Length - i != n - 1 || src[i] > 2)
            {
                Console.WriteLine("PKCS#1 unpad: i={0}, expected src[i]==[0,1,2], got src[i]={1}", i, src[i].ToString("X"));
                return(null);
            }

            ++i;

            while (src[i] != 0)
            {
                if (++i >= src.Length)
                {
                    Console.WriteLine("PKCS#1 unpad: i={0}, src[i-1]!=0 (={1})", i, src[i - 1].ToString("X"));
                }
            }

            byte[] bytes = new byte[src.Length - i - 1];
            for (int p = 0; ++i < src.Length; p++)
            {
                bytes[p] = src[i];
            }

            return(bytes);
        }
Example #10
0
        private byte[] DoDecrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                var c = new BigInteger(src);
                var m = method(c);
                if (m == 0)
                {
                    return(null);
                }

                var bl    = GetBlockSize();
                var bytes = pkcs1unpad(m.getBytes(), bl);
                return(bytes);
            }
            catch
            {
                return(null);
            }
        }
Example #11
0
        private byte[] DoDecrypt(DoCalculateionDelegate Method, byte[] Src, Pkcs1PadType Type)
        {
            try
            {
                BigInteger C = new BigInteger(Src);
                BigInteger M = Method(C);

                if (M == 0)
                {
                    return(null);
                }

                int    Bl    = GetBlockSize();
                byte[] Bytes = pkcs1unpad(M.getBytes(), Bl, Type);

                return(Bytes);
            }
            catch
            {
                return(null);
            }
        }
Example #12
0
        private byte[] DoDecrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                BigInteger c = new BigInteger(src);
                BigInteger m = method(c);
                if (m == 0)
                {
                    return(null);
                }

                int bl = this.GetBlockSize();

                byte[] bytes = this.pkcs1unpad(m.getBytes(), bl, type);

                return(bytes);
            }
            catch
            {
                return(null);
            }
        }
Example #13
0
        private byte[] DoEncrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                int bl = GetBlockSize();

                byte[] paddedBytes = Pkcs1Pad(src, bl, type);

                Array.Reverse(paddedBytes);

                BigInteger m = new BigInteger(paddedBytes);

                if (m == 0)
                    return null;

                BigInteger c = method(m);
                byte[] data = c.ToByteArray();

                if (data[data.Length - 1] == 0)
                    Array.Resize(ref data, data.Length - 1);

                Array.Reverse(data);

                return c == 0 ? null : data;
            }
            catch
            {
                return null;
            }
        }
Example #14
0
        private byte[] pkcs1pad(byte[] src, int n, Pkcs1PadType type)
        {
            byte[] bytes = new byte[n];

            int i = src.Length - 1;
            while (i >= 0 && n > 11)
            {
                bytes[--n] = src[i--];
            }

            bytes[--n] = 0;
            while (n > 2)
            {
                byte x = 0;
                switch (type)
                {
                    case Pkcs1PadType.FullByte: x = 0xFF; break;
                    case Pkcs1PadType.RandomByte: x = Randomizer.NextByte(1, 255); break;
                }
                bytes[--n] = x;
            }

            bytes[--n] = (byte)type;
            bytes[--n] = 0;
            return bytes;
        }
Example #15
0
        private static byte[] Pkcs1Pad(IList<byte> src, int n, Pkcs1PadType type)
        {
            byte[] bytes = new byte[n];

            int i = src.Count - 1;
            while (i >= 0 && n > 11)
                bytes[--n] = src[i--];

            bytes[--n] = 0;
            while (n > 2)
            {
                byte x = 0;
                switch (type)
                {
                    case Pkcs1PadType.FullByte:
                        x = 0xFF;
                        break;

                    case Pkcs1PadType.RandomByte:
                        x = Randomizer.NextByte(1, 255);
                        break;
                }
                bytes[--n] = x;
            }

            bytes[--n] = (byte) type;
            bytes[--n] = 0;

            return bytes;
        }
Example #16
0
        private byte[] DoEncrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                int bl = this.GetBlockSize();

                byte[] paddedBytes = this.pkcs1pad(src, bl, type);
                BigInteger m = new BigInteger(paddedBytes);
                if (m == 0)
                {
                    return null;
                }

                BigInteger c = method(m);
                if (c == 0)
                {
                    return null;
                }

                return c.getBytes();
            }
            catch
            {
                return null;
            }
        }
Example #17
0
        private static byte[] Pkcs1Unpad(byte[] src, int n, Pkcs1PadType type)
        {
            int i = 0;
            while (i < src.Length && src[i] == 0)
                ++i;

            if (src.Length - i != n - 1 || src[i] > 2)
            {
                Console.WriteLine("PKCS#1 unpad: i={0}, expected src[i]==[0,1,2], got src[i]={1}", i,
                    src[i].ToString("X"));
                return null;
            }

            ++i;

            while (src[i] != 0)
                if (++i >= src.Length)
                    Console.WriteLine("PKCS#1 unpad: i={0}, src[i-1]!=0 (={1})", i, src[i - 1].ToString("X"));

            byte[] bytes = new byte[src.Length - i - 1];
            for (int p = 0; ++i < src.Length; p++)
                bytes[p] = src[i];

            return bytes;
        }
Example #18
0
        private byte[] DoDecrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                BigInteger c = new BigInteger(src);

                BigInteger m = method(c);

                if (m == 0)
                    return null;

                int bl = GetBlockSize();
                byte[] data = m.ToByteArray();

                Array.Reverse(data);

                byte[] bytes = Pkcs1Unpad(data, bl, type);

                return bytes;
            }
            catch
            {
                return null;
            }
        }
Example #19
0
        private byte[] DoDecrypt(DoCalculateionDelegate method, byte[] src, Pkcs1PadType type)
        {
            try
            {
                BigInteger c = new BigInteger(src);
                BigInteger m = method(c);
                if (m == 0)
                {
                    return null;
                }

                int bl = this.GetBlockSize();

                byte[] bytes = this.pkcs1unpad(m.getBytes(), bl, type);

                return bytes;
            }
            catch
            {
                return null;
            }
        }