Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 互质整数_C#_Cryptography - Fatal编程技术网

C# 互质整数

C# 互质整数,c#,cryptography,C#,Cryptography,我正在尝试为我的afinne代码创建互质整数: static void Main(string[] args) { string input = ""; string openMessage = ""; openMessage = openMessage.ToUpper(); string cryptedMessage = ""; int a;

我正在尝试为我的afinne代码创建互质整数:

 static void Main(string[] args)
        {
            string input = "";
            string openMessage = "";
            openMessage = openMessage.ToUpper();
            string cryptedMessage = "";
            int a;
            int b;
            int m;

            Console.WriteLine("Enter the modular size of the alphabet:");
            input = Console.ReadLine();
            m = int.Parse(input);

            Console.WriteLine("Enter first value for encrypting the message:");
            input = Console.ReadLine();
            a = int.Parse(input);
            while(a!=0 && m !=0)
            {
                if (a > m)
                {
                    a %= m;
                } else
                {
                    m %= a;
                }

            }
            Console.WriteLine("Enter second value for encrypting the message:");
            input = Console.ReadLine();
            b = int.Parse(input);

            Console.WriteLine("Enter the message you want to encrypt:");
            input = Console.ReadLine();
            openMessage = input.ToUpper();

            foreach (char letter in openMessage)
            {
                int letterNumber = (int)letter;
                letterNumber = (a * (letterNumber - 65) + b) % m; //Afinne Cipher math
                letterNumber = letterNumber + 65;
                char encryptedLetter = (char)letterNumber;
                cryptedMessage = cryptedMessage + encryptedLetter;
            }
            Console.WriteLine(cryptedMessage);

        }
有没有人建议怎么做

我想与
intm
inta
进行比较。我查了一下Google,发现在我的代码中实现起来有点困难。

确定两个非负整数的值的方法可以在C#中实现,如下所示

public int Gcd(int m, int n)
{
    var tmp = 0;
    if (m < n)
    {
        tmp = m;
        m = n;
        n = tmp;
    }
    while (n != 0)
    {
        tmp = m % n;
        m = n;
        n = tmp;
    }
    return m;
}

GCDs的欧几里德算法非常容易实现。为什么你觉得这很难?因为我还是一个编程新手,它的代码有点类似于你已经有的while循环,所以我不认为它涉及到任何你还不知道的东西。我还发现了biginger.ModPow();。欧几里德算法更好吗?
public void Reduce(ref int m, ref int n)
{
    var Gcd = Gcd(m, n);
    m /= Gcd;
    n /= Gcd;
}