コード例 #1
0
        public string Encrypt(string message)
        {
            byte[]     getBytes     = conversion.ConvertToByteArray(message, Encoding.ASCII);
            string     plain_bits   = conversion.ToBinary(getBytes);
            BigInteger plain_number = conversion.BinaryToNumber(plain_bits);

            Ciphers = mathUtil.Fast_Exponent(plain_number, PUBLIC_KEY, N);
            string cipher_bits = Extension.BigIntegerExtensions.ToBinaryString(Ciphers);

            for (int i = 0; i < cipher_bits.Length % 8; i++)
            {
                cipher_bits = "0" + cipher_bits;
            }
            return(conversion.BinaryToString(cipher_bits));
        }
コード例 #2
0
        public string Encrypt(string message)
        {
            do
            {
                //K = mathUtil.RandomInRange(RandomNumberGenerator.Create(), 2, P - 2);
                K = mathUtil.GetRandomNumber(key_size);
            } while (K < 2 || K > P - 2);

            // string cipher_bits = conversion.StringToBinary(message);
            byte[] getBytes    = conversion.ConvertToByteArray(message, Encoding.ASCII);
            string cipher_bits = conversion.ToBinary(getBytes);

            int length = 0;

            if (cipher_bits.Length % key_size == 0)
            {
                length = cipher_bits.Length / key_size;
            }
            else
            {
                length = cipher_bits.Length / key_size + 1;
            }
            BigInteger[] plain_numbers = new BigInteger[length];
            for (int i = 0; i < plain_numbers.Length; i++)
            {
                string temp = "";
                if (cipher_bits.Length < key_size)
                {
                    temp        = cipher_bits.Substring(0, cipher_bits.Length);
                    cipher_bits = cipher_bits.Remove(0, cipher_bits.Length);
                }
                else
                {
                    temp        = cipher_bits.Substring(0, key_size);
                    cipher_bits = cipher_bits.Remove(0, key_size);
                }
                plain_numbers[i] = conversion.BinaryToNumber(temp);
            }
            // calculate c1, c2
            c1 = mathUtil.Fast_Exponent(G, K, P);

            c2 = new BigInteger[plain_numbers.Length];

            BigInteger expo = mathUtil.Fast_Exponent(PUBLIC_KEY, K, P);

            for (int i = 0; i < plain_numbers.Length; i++)
            {
                c2[i] = ((plain_numbers[i] % P) * expo) % P;
            }

            // change c2 into binary -> get cipher text
            string encrypt_txt = "";

            for (int i = 0; i < c2.Length; i++)
            {
                string temp = Extension.BigIntegerExtensions.ToBinaryString(c2[i]);
                for (int j = 0; j < temp.Length % 8; j++)
                {
                    temp = "0" + temp;
                }
                encrypt_txt += conversion.BinaryToString(temp);
            }
            return(encrypt_txt);
        }