السلام عليكم
مثال عن خوارزمية
Affine Cipher-Encryption
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Encyption
{
class AffineCipher
{
/////////// return Greatest comman Divisor////
public int GCD(int x, int y)
{
if (y == 0)
return x;
else
return GCD(y, x % y);
}
///////////// return m` by using Extended Euclid method/////////////
public int getInverse(int m, int n)
{
int s0 = 1 , s1 = 0;
int t0 = 0 , t1 = 1;
// save this number in varibale
int r0 = m;
int r1 = n;
// q1 is the qoution
int q = r0 / r1;
int r2 = r0 % r1;
int s2 , t2;
while ( r2 > 0 )
{
s2 = s0 - (s1 * q);
s0 = s1;
s1 = s2;
t2 = t0 - (t1 * q);
t0 = t1;
t1 = t2;
r0 = r1;
r1 = r2;
q = r0 / r1;
r2 = r0 % r1;
}
return s1 ;
}
/////////// check to see if GCD(m,n) == 1 , return true if that , false if not////////
public bool checkKey(int m, int n)
{
if ( GCD(m,n) == 1 )
return true;
else
return false;
}
/////////// cencryption////////
public string encryption( string plainText, int m, int key, int n)
{
string str2 = "";
for (int i=0; i<plainText.Length; i++)
{
int x = ( ( ( ((int)plainText - 65) * m) + key ) % n ) + 65;
str2 += (char) x;
}
Console.WriteLine(str2);
return str2;
}
/////////// decryption////////
public string decryption(string cipherText, int m, int key, int n)
{
string str2 = "";
int inverse_M = getInverse(m,n);
for (int i=0; i<cipherText.Length; i++)
{
int x = ((inverse_M * (((int)cipherText - 65) - key)) % n) + 65;
// int x1 = 0;
//int x1= x ;
str2 += (char)x;
}
Console.WriteLine(str2);
return str2;
}
}
}
using System;
الشرح مرفق مع ملف
PPT B)