简体   繁体   English

如何计算仿射密码的模乘逆

[英]How to calculate the modular multiplicative inverse for the Affine Cipher

I am trying to create a small software that does the Affine Cipher, which means that K1 and the amount of letters in the alphabet (using m for this number) must be coprime, that is gcd(k1, m) == 1 . 我正在尝试创建一个用于执行仿射密码的小型软件,这意味着K1和字母表中字母的数量(使用m表示该数字)必须是互质的,即gcd(k1, m) == 1

Basically it's like this: 基本上是这样的:

I have a plaintext: hey 我有一个明文:

I have K1: 7 我有K1: 7

I have K2: 5 我的K2: 5

Plaintext in numerical format is: 8 5 25 数字格式的纯文本为: 8 5 25

8 - from h (the position in the alphabet) and ** 5 25** goes the same for e and y 8-从h(字母的位置)和** 5 25 **开始, ey相同

Encrypted: 7 13 18 加密: 7 13 18

Which is the formula: 公式如下:

k1 * 8 + k2 mod 27 = 7 k1 * 8 + k2 mod 27 = 7

k1 * 5 + k2 mod 27 = 13 k1 * 5 + k2 mod 27 = 13

k1 * 25 + k2 mod 27 = 18 k1 * 25 + k2 mod 27 = 18

I have a function that crypts this but I don't know how to decrypt. 我有一个可以对此加密的函数,但是我不知道如何解密。

For example I have 7 for h. 例如,我的h为7。 I want to get the number 8 back again, knowing 7, k1 and k2. 我想知道7,k1和k2,再次将数字8取回来。

Do you guys have any ideas ? 你们有什么主意吗?

Some function where you input k1, k2, result (7 for example, for h), and it gives me back 8, but I really don't know how to reverse this. 您在其中输入k1,k2,结果的某些函数(例如,对于h为7),它会给我8,但我真的不知道如何将其取反。

The function for encryption is this: 加密功能如下:

public List<int> get_crypted_char(string[] strr)
        {
            List<int> l = new List<int>();
            int i;
            for (i = 0; i < strr.Length; i++)
            {
                int ch = int.Parse(strr[i]);
                int numberback = k1 * ch + 5;
                numberback = (numberback % 27);
                l.Add(numberback);
            }
            return l;
        }

Where: string[] strr is a string that contains the plaintext. 其中:string [] strr是包含明文的字符串。 Function example: get_crypted_char({"e","c","b"}) 函数示例: get_crypted_char({“ e”,“ c”,“ b”})

The result would be a list like this {"5","3","2"} 结果将是一个像这样的列表{“ 5”,“ 3”,“ 2”}

UPDATE: Here is a link from wikipedia about this encryption, and also decryption, but ... I don't really understand "how to" http://en.wikipedia.org/wiki/Affine_cipher 更新:这是Wikipedia的有关此加密以及解密的链接,但是...我不太了解“如何” http://en.wikipedia.org/wiki/Affine_cipher

It is not possible (in general case, for affine cipher, see update below). 这是不可能的(通常情况下,仿射密码请参阅下面的更新)。 That's why module operation is so frequently used in security algorithms - it is not reversible. 这就是为什么模块操作在安全算法中如此频繁使用的原因-它是不可逆的。 But, why don't we try? 但是,为什么我们不尝试呢?

result = (k1 * input + k2) % 27 (*1)

Let's take the first letter: 让我们来第一个字母:

result = (7 * 8 + 5) % 27 = 7

That's cool. 这很酷。 Now, because we said, that: 现在,因为我们说过:

result = (k1 * input + k2) % 27

the following is also true: 以下内容也适用:

k1 * input + k2 = 27 * div + result (*2)

where 哪里

div = (k1 * input + k2) / 27 (integral division)

It is quite obvious (if a % b = c, then a = b*n + c, where n is the result of integer division a/b). 这是很明显的(如果%b = c,则a = b * n + c,其中n是整数除法a / b的结果)。

You know the values of k1 (which is 7), k2 (5) and result (7). 您知道k1(即7),k2(5)和结果(7)的值。 So, when we put these values to (*2), we get the following: 因此,当我们将这些值设为(* 2)时,我们得到以下信息:

7 * input + 5 = 27 * div + 7 //You need to solve this

As you can see, it is impossible to solve this, because you would need to know also the result of the integral division - translating this to your function's language, you would need the value of 如您所见,这是不可能解决的,因为您还需要知道整数除法的结果-将其转换为函数的语言,您将需要

numberback / 27

which is unknown. 这是未知的。 So answer is: you cannot reverse your function's results, using only output it returns . 因此,答案是: 您不能只使用返回的输出来反转函数的结果


** UPDATE ** **更新**


I focused too much on the question's title, so the answer above is not fully correct. 我过多地关注了问题的标题,因此上述答案并不完全正确。 I decided not to remove it, however, but write an update. 我决定不删除它,而是编写一个更新。

So, the answer for your particular case (affine cipher) is: YES, you can reverse it. 因此, 针对您的特定情况 (仿射密码)的答案是:是的,您可以将其反转。

As you can see on the wiki, decryption function for affine cipher for the following encrytption function: 正如您在Wiki上看到的那样,以下加密功能的仿射密码解密功能:

E(input) = a*input + b mod m

is defined as: 定义为:

D(enc) = a^-1 * (enc - b) mod m

The only possible problem here can be computation of a^-1, which is modular multiplicative inverse. 这里唯一可能的问题是a ^ -1的计算,它是模乘逆。

Read about it on wiki , I will provide only example. wiki上阅读有关内容,我仅提供示例。

In your case a = k1 = 7 and m = 27. So: 在您的情况下,a = k1 = 7,m = 27。

7^-1 = p mod 27
7p = 1 mod 27

In other words, you need to find p, which satisfies the following: 7p % 27 = 1. p can be computed using extended euclidean algorithm and I computed it to be 4 (4 * 7 = 28, 28 % 27 = 1). 换句话说,您需要找到满足以下条件的p:7p%27 =1。可以使用扩展的欧几里得算法来计算p,我将其计算为4(4 * 7 = 28,28%27 = 1)。

Check, if can decipher your output now: 检查,是否可以立即解密您的输出:

E(8) = 7*8 + 5 mod 27 = 7

D(7) = 4 * (7 - 5) mod 27 = 8

Hope that helps :) 希望有帮助:)

Please note that the other answers do not take into account the the algorithm at hand is the Affine Cipher, ie there are some conditions at hand, the most important one the coprime status of k1 and m . 请注意,其他答案未考虑手头的算法是仿射密码,即手头有一些条件,最重要的是k1m的互质状态。

In your case it would be: 您的情况是:

m = 27; // letters in your alphabet
k1 = 7; // coprime with m
k2 = 5; // no reqs here, just that a value above 27 is the same as mod 27 of that value

int Encrypt(int letter) {
  return ((letter * k1) + k2) % m;
}

int Decrypt(int letter) {
  return ((letter - k2) * modInverse(k1, m)) % m;
}

Tuple<int, Tuple<int, int>> extendedEuclid(int a, int b)
{
  int x = 1, y = 0;
  int xLast = 0, yLast = 1;
  int q, r, m, n;
  while (a != 0)
  {
    q = b / a;
    r = b % a;
    m = xLast - q * x;
    n = yLast - q * y;
    xLast = x; yLast = y;
    x = m; y = n;
    b = a; a = r;
  }
  return new Tuple<int, Tuple<int, int>>(b, new Tuple<int, int>(xLast, yLast));
}

int modInverse(int a, int m)
{
  return (extendedEuclid(a, m).Item2.Item1 + m) % m;
}

ModInverse implementation taken from http://comeoncodeon.wordpress.com/2011/10/09/modular-multiplicative-inverse/ . 取自http://comeoncodeon.wordpress.com/2011/10/09/modular-multiplicative-inverse/的 ModInverse实现。

I have created a program that will tell the modular inverse of something. 我创建了一个程序,该程序将告诉模块逆。 I will let you use it. 我会让你使用它。 It is posted below. 它发布在下面。

# Cryptomath Module


def gcf(a, b):
    # Return the GCD of a & b using Euclid's Algorithm
    while a != 0:
        a, b = b % a, a
    return b


def findModInverse(a, m):
    # Return the modular inverse of a % m, which is
    # the number x such that a*x % m = 1

    if gcf(a, m) != 1:
        return None # No mode inverese if a & m aren't relatively prime

    # Calculate using the Extended Euclidean Algorithm:
    u1, u2, u3 = 1, 0, a
    v1, v2, v3 = 0, 1, m
    while v3 != 0:
        q = u3 // v3 # // is the integer division operator
        v1, v2, v3, u1, u2, u3 = (u1 - q * v1), (u2 - q * v2), (u3 - q *
v3), v1, v2, v3
    return u1 % m

Note: The modular inverse is found using the extended euclidean algorithm. 注意:使用扩展的欧几里得算法可以找到模逆。 Here is the Wikipedia entry for it: http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm . 这是它的Wikipedia条目: http : //en.wikipedia.org/wiki/Extended_Euclidean_algorithm

Note: This needs to be imported as a module to be used. 注意:这需要作为要使用的模块导入。 Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM