我试图为我的afinne代码创建一个Coprime整数:
C:\Windows\System32\
有没有人建议如何做到这一点?
我想与 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);
}
和int m
进行比较。我查看了Google并找到this,但看起来很难实现我的代码。
答案 0 :(得分:1)
用于确定两个非负整数的euclidean algorithm的greatest common divisor可以用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;
}
基于此实现,可以使用以下函数减少分数m/n
。
public void Reduce(ref int m, ref int n)
{
var Gcd = Gcd(m, n);
m /= Gcd;
n /= Gcd;
}