(Ancient heritage) cryptography - RSA algorithm full resolution (Chapter II)

RSA algorithm full resolution (Chapter II) - Cryptography
Chapter how to use the RSA algorithm
after taking advantage of free time during the day to write their own chapter of the store, and remember the various theorems assume the first chapter how should we achieve RSA encryption and decryption password do? It does not bother nonsense, direct access to the main topic.
Knowledge first review a few:
1. The nature of modular arithmetic:
associative: (a% p * b) % p = (a * b)% p
understood as when a == b, (a% p * a) % = P (A * A) P%
2. Euler's theorem
A ^ [Phi] (n-). 1 ≡ (n-MOD)
3. multiplicative inverse properties:
E * D. 1 ≡ (n-MOD) => E * D +. 1 ≡ k * n (k is an arbitrary integer)
Note: n and when a and b prime solvable.
Note: How to remove the belt (mod) formula, so that k = 0, that is, ignores other than with style.

    接着我们要证明一种现象,根据欧拉定理与乘法逆元性质外带一点模运算的结合律运算列出如下算式:
            M ^ (e * d) = M ^ (1 + k * φ(n)) = M * M ^ (φ(n) * k) ≡ M * (1) ^ k ≡ M (mod n)
            由此可知  M ^ (e * d)  ≡ M (mod n)
            注:M ^ φ(n) ≡ 1 (mod n) (欧拉定理)

    基于上述请诸位看以下算式:
            C ≡ M ^ e (mod n) ①
            M ≡ C ^ d (mod n) ②
    于是我们这样做,将①代入②可得:M ≡ M ^ (e * d) (mod n),是不是和上述的现象的结果一致呢?
    至此我们也可以看出,其实C就是一个中间密文。
    那么我们来看看RSA运作流程:
    假如A和B两人各自拿着e与d。
    首先A要给B解密用的密钥d时必然得满足乘法逆元性质e和n要互质,否则得不到d。
    其次对于A要加密一个B可以解开的密文必然满足M < n,否则M不满足②式也就不唯一了,同样B也要满足C < n,否则A也解不对B的数据。
    最后利用①与②算式分别生成中间密文交由对方来还原数据。
    综上所述,RSA算法描述就到此结束了。

    以下是算法实现过程:
    一.算法所需参数
            1.密文数据规模 n = φ(n)
            2.生成两把密钥 e 与 d
    二.算法实现代码
    一开始想着分C++和C版本代码,后来一想,既然都写了C代码何必浪费时间去写吃力不讨好的C++呢?= -=
    于是现在直接给出完整的C代码了,因为第三章想基于第二章的代码部分进行讲解,尤其是一些可以优化本质的代码(即不在语法上改进),说不定出了第二章后就不想写第三章了,所以把代码放出来是为了以后好找理由偷懒吧(“都把代码给你了你怎么还要我来讲解(说笑的= -=)”)
    事不宜迟贴代码吧!建议想学习的同学复制后拿到自己的编译器里运行测试一下~

#ifndef RSA

define RSA

@ Euler function: φ (x) = x Π (1-1 / p) (P N is the number of prime factors)
int Eular (n-int)
{
int RET =. 1, I;
for (I = 2; I I <= n-; I ++)
{
IF (n-% 0 == I)
{
n-/ = I, RET
=-I. 1;
the while (n-== 0% I)
n-/ = I, RET = I;
}
}
IF (n->. 1)
RET
= n--. 1;
return RET;
}
// Euclidean function Ho: Gcd (A, B) = Gcd (B, A% B)
int Gcd (A int, int B)
{
return 0 ? A == B: Gcd (B, A% B);
}
// expand Euclidean function Ho: A X + B Y = GCD (A, B)
// GCD (A, B) = GCD (B, B% A)
// A% B = A- (A / B) B
// A
X + BGCD = Y (A, B) = GCD (B, A% B) B = X1 + (A- (A / B) B) Y1
// B = X1 + A Y1- (A / B) B Y1
// A = Y1 + B (X1-A / B Y1)
int Egcd (A int, int B, int
X, int Y)
{
int Result, T;
// recursive termination condition
IF (B == 0)
{
X =. 1 , Y = 0;
return A;
}
Result = Egcd (B, A% B, X, Y);
T =
X, X = Y, Y = TA / B ( Y);
return Result;
}
// Solution multiplicative inverse membership function B
(return value). 1 ≡ (MOD A)
int Inverse (A int, int B)
{
int X = 0, Y = 0;
IF (Egcd (A, B, & X, & Y) =. 1!)
return -1;
// ensure remainder and dividend sign consistent
IF (B <0)
A = -a;
IF ( (A = Y%) <= 0)
Y = A +;
return Y;
}
// modulus arithmetic% B ^ K A
// generally thought solving violent version
unsigned PowMod (unsigned A, B unsigned, unsigned C)
{
unsigned A = ANS;
the while (B -)
ANS = A;
return ANS% C;
}
// modulus arithmetic% B ^ K A
// recursive version B ^ A (C MOD)
// modulo operation is associative: (a
A) MOD C = ((A MOD C) * A) MOD C
// C language expression: ((A * B)% P = (P * A% B)% P)
unsigned RecursionPowMod (unsigned A, B unsigned, C unsigned)
{
return b ? a * RecursionPowMod(a, b - 1, c) % c : 1;
}
// 幂模运算a^b%k
// Montgomery 版本
unsigned FastPowMod(unsigned a, unsigned b, unsigned c)
{
unsigned ans;
for (ans = 1; b; a = aa%c, b >>= 1)
if (b & 1)
ans = ans
a%c;// LSB位为 1
return ans;
}

#endif // RSA

include "stdio.h"

include "stdlib.h"

main int ()
{
// the data size N, En = φ (N) (Euler function result), Encrypt and Decrypt are two key
int N = 0x1F, En = Eular (N), Encrypt = 2, Decrypt ;
// generate RSA parameters
do
{
// select and Encrypt prime number En
the while (Gcd. 1 = (En, Encrypt)!)
Encrypt ++;
// solve its multiplicative inverse, if Decrypt == -1 indicates En and Encrypt not coprime
The Decrypt = Inverse (En, Encrypt);
} the while (-1 == The Decrypt);
// preview RSA parameters
printf ( "En:% 4d \ tE:% 4d \ tD:% 4d \ n", En , Encrypt, Decrypt);
// function test, if caught in an infinite loop must be not being able to revert back to the original data can not lead into the next index (++), self-test data is greater than N scale
puts ( "test Encrypt Key" );
for (int the src = 0, Goal; the src <N; the src ++)
{
the printf ( "the src:% 4D \ T", the src);
Goal = RecursionPowMod (the src, Encrypt, N);
the printf ( "Goal:% 4D \ T", Goal);
the src = RecursionPowMod (Goal, The Decrypt, N);
the printf ( "the src:% 4D \ n-", the src);
}
the puts ( "the Test The Decrypt Key");
for (int the src = 0, Goal; the src <N; the src ++)
{
the printf ( "the src:% 4D \ T", the src);
Goal = RecursionPowMod (the src, The Decrypt, N);
the printf ( "Goal:% 4D \ T" , Goal);
the src = RecursionPowMod (Goal, Encrypt, N);
the printf ( "the src:% 4D \ n-", the src);
}
return 0;
}
way also stickers operation results = - = - title is my IDE path , 32-bit compiler mingw

Finally, the last, this chapter also nothing to talk about, the code says it all, look at the code more effective than a crap ten I think it ~
If you have questions or want to learn more students can whisper I = - = Anyway, I night has been very free (occasionally go play World of Warcraft it ~)

Guess you like

Origin www.cnblogs.com/juwan/p/11449042.html