1

ElGamal暗号化を実装したい。学校の仕事でこれが必要ですが、復号化を実行したい場合、最後のステップは常に0です。(b / Math.Pow(a、x))%primenumberの原因は常に1未満です。

キーの生成は次のとおりです。

public void GenerateKey() {
    this.x = 3;
    this.prvocislo = PrimeGen.findPrimes(29).Max(); //prime number
    this.g = this.prvocislo % 12;
    this.y = Convert.ToInt32(Math.Pow(this.g, this.x) % this.prvocislo);
    this.k = 23;//601}

暗号化機能は次のとおりです。

public string Encrypt(string word) {
            List<string> words = new List<string>();

            words = PrimeGen.SplitToArray(word, 2);

            string encrypted="";

            string sss = PrimeGen.GetStringFromBytes(PrimeGen.GetBytesFromInt(PrimeGen.GetIntFromBytes(PrimeGen.GetBytesFromString("ah")))); //returns ah so conversion works
            foreach (string s in words)
            {
                int a = Convert.ToInt32(Math.Pow(g,k) % prvocislo);
                int b = Convert.ToInt32((Math.Pow(y, k) * PrimeGen.GetIntFromBytes(PrimeGen.GetBytesFromString(s))) % prvocislo);
                string aS = PrimeGen.GetStringFromBytes(PrimeGen.INT2LE(a + posun));
                string bS = PrimeGen.GetStringFromBytes(PrimeGen.INT2LE(b + posun));
                encrypted = encrypted + aS + bS;
            }
            return encrypted;

        }

これが私の復号化機能です:

   public string Decrypt(string ElgamalEncrypted) {
            string decrypted = "";
            for (int i = 0; i < ElgamalEncrypted.Length; i = i + 2) {
                string aS = ElgamalEncrypted.Substring(i, 2);
                string bS = ElgamalEncrypted.Substring(i + 2, 2);
                int a = PrimeGen.GetIntFromBytes(PrimeGen.GetBytesFromString(aS)) - posun;
                int b = PrimeGen.GetIntFromBytes(PrimeGen.GetBytesFromString(bS)) - posun;
                if(b==0) b=1;
                if (a == 0) a = 1;
                decrypted=decrypted+PrimeGen.GetStringFromBytes(PrimeGen.GetBytesFromInt(Convert.ToInt32(((b/Math.Pow(a,x))%prvocislo))));


            }
            return decrypted;
        }
4

1 に答える 1

2

Math.Pow(base, exponent) % modulus剰余累乗に使用しています。浮動小数点数は暗号が必要とする大きな整数を表すことができないため、これは機能しません。System.Numerics.BigInteger.ModPow(base, exponent, modulus)代わりに使用してください。

右辺の剰余乗法逆数で乗算する代わりに、整数除算を使用しているため、除算はおそらく機能しません。

于 2013-01-06T14:25:39.433 に答える