コード例 #1
0
        public bool Decipher(string route, out byte[] Message, RSAkey PrivateKey)
        {
            List <byte> toReturn = new List <byte>();

            using (FileStream fs = File.OpenRead(route))
            {
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    int counter = 0;
                    while (counter < fs.Length)
                    {
                        byte[] ByteArray  = reader.ReadBytes(24);
                        int    iterations = 1;
                        for (int i = 0; i < ByteArray.Length;)
                        {
                            List <byte> toDecimal = new List <byte>();

                            for (int k = i; k < 8 * iterations; k++)
                            {
                                toDecimal.Add(ByteArray[k]);
                                i++;
                            }
                            iterations++;
                            string     Decimal  = Encoding.ASCII.GetString(toDecimal.ToArray());
                            BigInteger toBinary = CihperByte(Convert.ToInt32(Decimal, 16), PrivateKey);
                            toReturn.Add(Convert.ToByte((int)toBinary));
                        }
                        counter += 24;
                    }
                }
            }
            Message = toReturn.ToArray();
            return(true);
        }
コード例 #2
0
        public bool GetKeys(int p_Number, int q_Number, out RSAkey PrivateKey, out RSAkey PublicKey)
        {
            if (IsPrimeNumber(p_Number) && IsPrimeNumber(q_Number))
            {
                BigInteger N_number = p_Number;
                N_number = N_number * q_Number;
                if (N_number > int.MaxValue)
                {
                    PrivateKey = null;
                    PublicKey  = null;
                    return(false);
                }
                if (N_number < 255)
                {
                    PrivateKey = null;
                    PublicKey  = null;
                    return(false);
                }

                phi = (p_Number - 1);
                phi = phi * (q_Number - 1);
                if (phi > 2)
                {
                    Get_e();
                    if (e_number != -1)
                    {
                        Get_d();
                        if (d == e_number)
                        {
                            d += N_number;
                        }
                        PrivateKey         = new RSAkey();
                        PrivateKey.modulus = N_number;
                        PrivateKey.power   = d;
                        PublicKey          = new RSAkey();
                        PublicKey.modulus  = N_number;
                        PublicKey.power    = e_number;
                        return(true);
                    }
                    else
                    {
                        PrivateKey = null;
                        PublicKey  = null;
                        return(false);
                    }
                }
                else
                {
                    PrivateKey = null;
                    PublicKey  = null;
                    return(false);
                }
            }
            else
            {
                PrivateKey = null;
                PublicKey  = null;
                return(false);
            }
        }
コード例 #3
0
        public bool Cipher(string route, out byte[] cipheredMsg, RSAkey PublicKey)
        {
            StringBuilder sb = new StringBuilder();

            using (FileStream fs = File.OpenRead(route))
            {
                using (BinaryReader reader = new BinaryReader(fs))
                {
                    int counter = 0;
                    while (counter < fs.Length)
                    {
                        byte[] ByteArray = reader.ReadBytes(3);

                        for (int i = 0; i < ByteArray.Length; i++)
                        {
                            BigInteger toBinary = CihperByte(ByteArray[i], PublicKey);
                            string     Binary   = Convert.ToString((int)toBinary, 16);
                            sb.Append(Binary.PadLeft(8, '0'));
                        }
                        counter += 3;
                    }
                }
            }
            cipheredMsg = Encoding.ASCII.GetBytes(sb.ToString());
            return(true);
        }
コード例 #4
0
        public BigInteger CihperByte(int toCipher, RSAkey key)
        {
            BigInteger res = BigInteger.ModPow(toCipher, key.power, key.modulus);

            return(res);
        }