Вы находитесь на странице: 1из 52

,

( )


244


: -
RSA

:
: -2-09
: ..
: 06.12.11

/
1.

...

2.

, 2011

()

____

___________________________________
...

_________________________________
...

___________________________________

_________________________________

_____ ____________________ 20___.

____ __________________ 20___.


: -
: : -2-09
: RSA
.
1. :
1.1. ,
1.2.
1.3.
1.4.
2. :
2.1. RSA
2.2.
2.3.
2.4.
2.5.
2.6.

3.
3.1. ,

5 . 4 .

3.2.

5 . 5 .

3.3.

5 . 15 .

3.4.

5 . 15 .

3.5.

5 . 15 .

_________________________________
...
_________________________________

____ ___________________ 20___.


1. ....................................................................................................................................................................6
2. ........................................................................................................................7
3. ............................................................................................8
3.1. .............................................................................................................................................................8
3.2. , .....................................................................................................8
4. ........................................................................................................10
4.1. .............................................................................................................................10
4.2. ..................................................................................................................11
5. .....................................................................................................................................12
6. RSA ........................................................................................................................13
6.1. RSA..............................................................................................................................................13
6.2. RSA......................................................................................................................................13
7. ......................................................................................................................................................15
8. RSA..............................................................................................................................................17
8.1. RSA..........................................................................................................................17
8.2. RSA.....................................................................................................................18
8.3. RSA..............................................................................................................................................19
9. .................................................................................................................................................21
9.1. RSA...................................................................................................................................................22
9.1.1. Program.cs...........................................................................................................................................22
9.1.2. MainForm.cs........................................................................................................................................22
9.2. EvklidLib...........................................................................................................................................................23
9.2.1. EvklidLib.cs....................................................................................................................................................23
9.3. EncodeDecode...................................................................................................................................................23
9.3.1. Encoder.cs.......................................................................................................................................................23
9.3.2. EncoderBase.cs...............................................................................................................................................23
9.3.3. IEncoder.cs......................................................................................................................................................23
9.3.4. Key.cs..............................................................................................................................................................24
10. ..................................................................................................................................25
p q ( , )
...............................................................................................................................25
.......................................................................................................................................................................................25
. 4 ............................................................................................................25
, ,
. .
........................................................................................................................................................25
.......................................................................................................................................................................................25
. 5 ..................................................................................................................25
................................................................26
.......................................................................................................................................................................................26
. 6 ...........................................................................................................................................26
11. .....................................................................................................................................................................27
......................................................................................................................................................................................29
.............................................................................................................................................................29
..............................................................................................................................................................38
1. 19.201-78....................................................................................38
1.1.............................................................................................................................................................38
1.1.1. ........................................................................................................................38
1.1.2. ........................................................................................38
RSA
RSA..............................................................................................................................................38
1.2.1. ...................................................................................................38
- ,
.
. , , ,
.., ...........................................................................38
1.2.2. .....................................................................38
1.2. .....................................................................................................................................38
1.3.1. ....................................................................................................................38
4

1.3.2. .................................................................................................................38
1.3. .................................................................................39
1.4.1. ................................................................................39
1.4.2. .........................................................................................................................39
1.4.3. ...............................................................................................................................41
1.4.4. ....................................................................42
1.4.5. ..........................................................42
1.4.7. ......................................................................................43
......................................................43
1.4.8. ..........................................................................................................................43
......................................................43
1.5. ..............................................................................................43
1.5.1. .........................................................................43
1.6. - ..............................................................................................................43
1.7. .............................................................................................................................43
1.8. .........................................................................................................................44
1.8.1. ........................................................................................................................................44
- .....................44
1.8.2. ......................................................................................................44
- , , ,
- .........................................................................................................................44
.............................................................................................................................................................45
1.1. ...................................................................................................................................45
..........................................................................45
1.2. ....................................................................................................................45
..........................................................................45
1.3. ()........................................................................................................45
..........................................................................45
1.4. ..................................................................................................................................45
..........................................................................45
1.5. .....................................................................................................................................45
..............................................................................................................................................................46
1. 19.301-79......................................................................................46
1.1.1. ........................................................................................................................................46
1.1.2. ............................................................................................................................................46
1.1.3. .................................................................................................................................46
1.1.4. ..........................................................................................................................46
1.2. ...............................................................................................................................................46
1.3. ............................................................................................................................................46
1.3.1. ...................................................................................................46
1.3.2. ................................................................................................46
1.3.3. , .............................................................................................47
1.3.4. , ..........................................................................47
1.4. .........................................................................................................................................47
1.4.1. ....................................................................................................................47
1.4.2. , .............................................48
1.4.3. ........................................................................48
1.4.4. , ................................................................48
1.5. ..................................................................................................................................49
1.6. ....................................................................................................49
1.7. ....................................................................................................49
1.7.1. ...............................................................................................................49
1.7.2. ...............................................................49
1.7.3. ...................................................................................49
1.7.4. ...........................................................................................49
1.7.5., ..............................50
1.7.6. , ..................................................50
1.7.7. , ...............................................................................50
1.8. ...........................................................................................................................................50
1.8.3. ....................................................................51
...........................................................................................................52
5

1.
1976

.
--

, .
(
). ,
, ,
.
, (Ronald Linn Rivest),
(Adi Shamir) (Leonard Adleman)
(MIT)


.
40 , ,
,

, RSA.
.

2.

,
:
x, f(x) .
y=f(x), x .

,
, ,
. , ,
.
RSA

, .

(. public key),
(. secret key). - .
RSA .
.
,
.

, .
:
RSA ,
.
7

3.
3.1.

RSA- :
1) p q
(, 1024 ).
2) n = pq, .
3) n:

4) e,

. e

,
, , 17, 257, 65537. e
. e ( 3)
RSA (. RSA.).
5)

d,

, k ( , d
). .
.
6) P = (e,n) RSA (.
RSA public key).
7) S = (d,n) RSA (. RSA private
key) .
3.2. ,

, p q
, , .
8

( n

x y,

x2 y2 = n,

.) , ,

(.
RSA.)
(.
.)

. ,
a b, ,
,
:
:
a = bq0 + r1
b = r1q1 + r2
r1 = r2q2 + r3
rk 2 = rk 1qk 1 + rk
rn 1 = rnqn

:
r1 = a + b( - q0)
r2 = b r1q1 = a( q1) + b(1 + q1q0)
gcd(a,b) = rn = as + bt
s t .
, s t .

.
9

:
RSA .
4.
4.1.


:
,
(e,n) .
:
.
(d,n),
:
(1) (2) RSA
, ,
( D , E , , P S
):

, :

. 1 RSA

10

4.2.

.
,

, .
:

, .

,
, :

.
, :

. 2

, ,

11

:
.
5.

,
- ,
.
,
, e.

12

6. RSA
6.1. RSA

2009 RSA ,
1024 .
6.2. RSA

, , ,
, ,
RSA
768 . ,
RSA-
1024 . 1024
- , 2013.
( 6 1)
80 , 3 %
, (),
.
AMD Opteron
2.2 2 , 1500 .
( )
.
12 .

4 .
192 796 550192 795 550 27 795 115 920
( 144 ).
105 .
5
.
13

232- ,
.
, ,
1024- RSA-
, 2020 .
: ,
RSA , ( 2011 )
,
.

14

7.

, n ,

, n .
, .
n
n=p0 * p1 * * pk

,

n - .
:
0+
10+
20+
30+
40+
50+
60+
70+
80+
90+

+0 +1
1
4 10
8 12
8 30
16 40
20 32
16 60
24 70
32 54
24 72

+2
1
4
10
16
12
24
30
24
40
44

+3
2
12
22
20
42
52
36
72
82
60

+4
2
6
8
16
20
18
32
36
24
46

+5
4
8
20
24
24
40
48
40
64
72

+6
2
8
12
12
22
24
20
36
42
32

+7
6
16
18
36
46
36
66
60
56
96

+8
4
6
12
18
16
28
32
24
40
42

+9
6
18
28
24
42
58
44
78
88
60

, p q , n , n=p*k.
,
:
,
.

15

,
n, 100
.

, ,
,
.
:
,
.

16

8. RSA
8.1. RSA

RSA :
1) ,
, ,
.
2)
.
.
3) , , ,
.

17

8.2. RSA

1) - ,

, ,
.
2) - ( 30 / 512
2 ),

( ), RSA .



.

18

8.3. RSA.

1) ,

:
p q ,
, . ,
,
.
,
p-1 (
N .
p, p 1 ,
.).
, ,
. :
79,
97, 101.
97 > (79+101)/2 = 90, 97 - .
2) ,

, n=p*q
. A
M. ,
,

n , e, d.
3) 1990 (Michael J. Wiener) ,
d RSA, ,
(
19

),
, d.

4)
, .
e=3. , RSA,

, ,
.
.
, ,

(Johan Hastad),
, ,
.

, RSA
, , ,
.
RSA ,
, ,
.
5) ,
, RSA,

20

. n ,
d.
6)
, d.
(Paul Kocher).
,
.
7)
,
, ,
,
RSA.
:
RSA,
, .

9.
21

C#.
.
9.1. RSA
9.1.1. Program.cs

, ,
Program.cs. , .
9.1.2. MainForm.cs

( )
:

. 3
MainForm ,
, :
OpenDocument
gcd
MutuallySimple d/e,
.
Checkpq p q.
, .

22

ApplyPQButton_Click p q,

learButton_Click
CryptButton_Click - ()
DecryptButton_Click ()
9.2. EvklidLib


9.2.1. EvklidLib.cs

:
ExtGCD .
9.3. EncodeDecode

.
9.3.1. Encoder.cs

()
:
Encode , (
)
Decode - , (
)
9.3.2. EncoderBase.cs

, .
:
Process
. .
ModularExponantiation - ,
RSA
9.3.3. IEncoder.cs

Encode Decode.
23

9.3.4. Key.cs

. Key,
E/D N.

24

10.
p q ( ,
) .

. 4
, ,
.
. .

. 5
25

. 6

26

11.
, , ,
,
, .

27


1.

, =

Practical Cryptography: Designing and Implementing Secure Cryptographic


Systems .: , 2004 .
2.

. . , ,

= Applied Cryptography. Protocols, Algorithms


and Source Code in C .: , 2002 .
3.

RSA (http://ru.wikipedia.org/wiki/RSA)

28


MainForm.cs
using
using
using
using
using
using
using
using
using
using
using
using

System;
System.Collections.Generic;
System.ComponentModel;
System.Data;
System.Drawing;
System.Linq;
System.Text;
System.Windows.Forms;
System.IO;
System.Numerics;
EncodeDecode;
EvklidLib;

namespace RSA
{
public partial class MainForm : Form
{
public long P, Q, D, E, M, N = 0;// .
string Source, fn = "";
public long[] cipherText;

public MainForm()
{
InitializeComponent();
label1.Visible = false;
#region FileDialog Settings

openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = " |*.txt";
openFileDialog1.Title = " ";
openFileDialog1.Multiselect = false;
#endregion

/// <summary>
///
/// </summary>
private void OpenDocument()
{
openFileDialog1.FileName = string.Empty;

//

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fn = openFileDialog1.FileName;
Encoding enc = Encoding.GetEncoding(1251);
try
{
//
StreamReader sr = new StreamReader(fn, enc); //

SourceTextBox.Text = sr.ReadToEnd(); //
SourceTextBox.SelectionStart = SourceTextBox.TextLength;
sr.Close(); //

29

}
catch (Exception exc) //
{
MessageBox.Show(" .\n"
+ exc.ToString(), "Error!",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}

}
/// <summary>
/// " "
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
OpenDocument();
TextBox1Length.Text = Convert.ToString(SourceTextBox.TextLength);
}
// " "

/// <summary>
///
/// </summary>
/// <param name="x"> </param>
/// <param name="y"> </param>
/// <returns> (
)</returns>
private static long gcd(long x, long y)
{
long g;
g = y;// g
while (x > 0) // ( , )
{
g = x; // g y
x = y % x;// "" ,
y = g;// ,
}
return g;// , g
}
/// <summary>
/// d (e),
/// </summary>
/// <param name="m"> ,
d</param>
/// <returns> d, </returns>
private static long MutuallySimple(long m)
{
Random rd = new Random(DateTime.Now.Millisecond);//

int tmp_d;
long d = 0, check;
bool quit = false;
while (quit == false)// ,
{
tmp_d = rd.Next(2, 100);// d, 2 100
d = Convert.ToInt64(tmp_d);//

30

check = gcd(m, d);// m,



if (check == 1) // == 1 d -
{
quit = true;// "" ,
}
}
return d;//
}
/// <summary>
/// , p q
/// </summary>
/// <param name="p"> </param>
/// <param name="q"> </param>
/// <returns> , , </returns>
public static bool Checkpq(long p, long q)
{
long t = p;//
if (q < p)
t = q;
for (int i = 2; i < t-1; i++)
{
if (((p % i) == 0) && (q % i == 0))// i
{
return true;//
}
}
return false;// , 1
}
/// <summary>
/// , .
.
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void ApplyPQButton_Click(object sender, EventArgs e)
{
if (PTextBox.Text.Length == 0 && QTextBox.Text.Length == 0 )// ,

{
MessageBox.Show(" P Q!");
return;
} else if (PTextBox.Text.Length == 0)//
{
MessageBox.Show(" P!");
return;
}
else if (QTextBox.Text.Length == 0)// Q
{
MessageBox.Show(" Q!");
return;
}
P = Convert.ToUInt32(PTextBox.Text);//
Q = Convert.ToUInt32(QTextBox.Text);// Q
if (Checkpq(P, Q))//, P Q
{
MessageBox.Show(" P Q !");
return;
}
N = P * Q;// . N
M = (P - 1) * (Q - 1);// . M

31

D = MutuallySimple(M);// d (e), m
long tmp_E;
Evklid evk = new Evklid();// ,
tmp_E = evk.ExtGCD(D, M);// D
if (tmp_E <= 0)//
{
while (tmp_E < 0)// ,
{
D = MutuallySimple(M);// D, , tmp_E
tmp_E = evk.ExtGCD(D, M);
}
}
E = Convert.ToInt64(tmp_E);// .
p_label.Text = Convert.ToString(P);// .
q_label.Text = Convert.ToString(Q);// .
e_label.Text = Convert.ToString(E);// .
d_label.Text = Convert.ToString(D);// .
n_label.Text = Convert.ToString(N);// .
m_label.Text = Convert.ToString(M);// .
CryptButton.Enabled = true;//
DecryptButton.Enabled = true;//

/// <summary>
///
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void ClearButton_Click(object sender, EventArgs e)
{
p_label.Text = "0";
q_label.Text = "0";
n_label.Text = "0";
m_label.Text = "0";
d_label.Text = "0";
e_label.Text = "0";
CryptButton.Enabled = false;//
DecryptButton.Enabled = false;//

}
/// <summary>
/// ""
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void CryptButton_Click(object sender, EventArgs e)
{
if (SourceTextBox.Text.Length == 0)//
{
MessageBox.Show(" ");
return;
}
ResultTextBox.ResetText();//
textBox1.ResetText();// 2
Key publicKey = new Key(E, N);// Key,
E N

32

EncodeDecode.Encoder enc = new EncodeDecode.Encoder();//

Source = SourceTextBox.Text;
cipherText = enc.Encode(Source, publicKey);// ,

for (int i = 0; i < cipherText.Length; i++)
{
textBox1.Text += cipherText[i];//
ResultTextBox.Text += cipherText[i];// ,
ResultTextBox.Text += " ";
}
TextBox2Length.Text = Convert.ToString(ResultTextBox.TextLength);//

}
/// <summary>
/// , ""
/// </summary>
/// <param name="sender"> </param>
/// <param name="e"> </param>
private void DecryptButton_Click(object sender, EventArgs e)
{
if (ResultTextBox.Text.Length == 0)//
{
MessageBox.Show(" ");
return;
}
ResultTextBox.ResetText();//
Key privateKey = new Key(D, N);// D N
EncodeDecode.Encoder enc = new EncodeDecode.Encoder();//
.
ResultTextBox.Text = enc.Decode(cipherText, privateKey);//

TextBox2Length.Text = Convert.ToString(ResultTextBox.TextLength);
if (SourceTextBox.Text == ResultTextBox.Text)// ,

{
label1.Visible = true;
label1.Text = " !";
label1.ForeColor = Color.Green;
}
else//
{
label1.Visible = true;
label1.Text = " !";
label1.ForeColor = Color.Red;
}
}
private void SourceTextBox_TextChanged(object sender, EventArgs e)
{
TextBox1Length.Text = Convert.ToString(SourceTextBox.TextLength);
}
}

33

}
Evklid.cs
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace EvklidLib
{
/// <summary>
///
/// </summary>
public class Evklid
{
/// <summary>
///
/// </summary>
/// <param name="x"> </param>
/// <param name="y"> </param>
/// <returns> , x
y</returns>
public long ExtGCD(long x, long y)
{
long a, b, q, r, u1, u2;
long t;
a = Convert.ToInt64(x);//
b = Convert.ToInt64(y);// b
u1 = 1;
u2 = 0;
while (b != 0)//, , b 0
{
q = a / b;
r = a % b;
a = b;
b = r;
t = u2;
u2 = u1 - q * u2;
u1 = t;
}
return u1;// ,
}

Encoder.cs
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace EncodeDecode
{
/// <summary>
///
/// </summary>
public class Encoder : EncoderBase, IEncoder<long>

34

{
/// <summary>
/// ( )
/// </summary>
/// <param name="sMessage"> </param>
/// <param name="oKey"> </param>
/// <returns> </returns>
public virtual long[] Encode(string sMessage, Key oKey)
{
long[] numericMessage = new long[sMessage.Length];//
int length = sMessage.Length;
for (int i = 0; i < length; i++)
{
numericMessage[i] = (long)sMessage[i];// ,

}
return Process(numericMessage, oKey);//
}

/// <summary>
///
/// </summary>
/// <param name="numericMessage"> </param>
/// <param name="oKey"> </param>
/// <returns> </returns>
public virtual string Decode(long[] numericMessage, Key oKey)
{
long[] processedMessage = Process(numericMessage, oKey);//

StringBuilder message = new StringBuilder();//
int length = processedMessage.Length;
for (int i = 0; i < length; i++)
{
message.Append((char)processedMessage[i]);//
}
return message.ToString();//
}
/// <summary>
///
/// </summary>
/// <returns> </returns>
public override int GetHashCode()
{
return base.GetHashCode();
}

/// <summary>
///
/// </summary>
/// <returns> string</returns>
public override string ToString()
{
return "Encoder";
}

35

EncoderBase.cs
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace EncodeDecode
{
/// <summary>
///
/// </summary>
public class EncoderBase
{
/// <summary>
/// / ( )
/// </summary>
/// <param name="src_text"> </param>
/// <param name="oKey"> </param>
/// <returns></returns>
protected long[] Process(long[] src_text, Key oKey)
{
long[] ciphertext = new long[src_text.Length];//

int length = src_text.Length;


for (int i = 0; i < length; i++)
{
ciphertext[i] = ModularExponantiation(src_text[i], oKey);//
}

return ciphertext;//

/// <summary>
/// -
/// </summary>
/// <param name="num"> </param>
/// <param name="oKey"></param>
/// <returns> </returns>
protected long ModularExponantiation(long num, Key oKey)
{
long C = 1;
long E = 0;
while (E++ < oKey.index)
{
C = (C * num) % oKey.N;// N -
RSA
}
}
}

return C;

IEncoder.cs

using System;
using System.Collections.Generic;

36

using System.Linq;
using System.Text;
namespace EncodeDecode
{
/// <summary>
/// Encoder ()
/// </summary>
/// <typeparam name="T"> ( )</typeparam>
public interface IEncoder<T> where T :struct
{
T[] Encode(string sMessage, Key oKey);
string Decode(T[] numericMessage, Key oKey);
}
}
Key.cs

using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;

namespace EncodeDecode
{
/// <summary>
///
/// </summary>
public struct Key
{
public long index;
public long N;
/// <summary>
///
/// </summary>
/// <param name="lIndex"> E ( ) D (
)</param>
/// <param name="lN"> N</param>
///
public Key(long lIndex, long lN)
{
index = lIndex;
N = lN;
}
}
}

37


1. 19.201-78
1.1.
1.1.1.
- RSA.
1.1.2.
RSA
RSA.
1.2.1.

, .
. ,
, ,
.., .
1.2.2.

RSA .
1.2.
1.3.1.

RSA,
.
1.3.2.
.

.
38

1.3.
1.4.1.
1.4.1.1.

:
) RSA
)
)
)
1.4.1.2.

.
, UTF.
1.4.1.3.
,
.
1.4.1.4.
.
1.4.2.
1.4.2.1.

()
()

, :
)

;
39

, 23 1998 .

;
)

51188-98.

.
.
1.4.2.2.
,
( ), (
) , -

.
,
, () ,
,
.
1.4.2.3. -

() .


.

40

1.4.3.
1.4.3.1.
.
1.4.3.2.
1.4.3.3.
.
1.4.3.4.

, 2 -
- .

. , ,
:
)

()

-
;
)

() .

()

.
II
( ).

41

1.4.4.
IBM-
(), :
) Pentium-4 , - 2, ;
) , - 100, ;
) , - 1, ;
) .
1.4.5.
1.4.5.1.
.
1.4.5.2.
C#.
1.4.5.3.

, ,

.
1.4.5.4.
.
1.4.6.
.

42

1.4.7.
.
1.4.8.
.

1.5.
1.5.1.
:
)

1.6. -
.
1.7.
,

5 . 4 .

5 . 5 .

5 . 15 .

5 . 15 .

5 . 15 .

43

1.8.
1.8.1.
.
1.8.2.
- , ,
, - .

44


1.1.
.
1.2.
.
1.3. ()
.
1.4.
.
1.5.
) p q
p q
)

45


1. 19.301-79
1.1.1.
1.1.2.
- RSA.
1.1.3.

.
1.1.4.

RSA .
1.2.
-
( )
,
.
1.3.
1.3.1.
.
1.3.2.
- 21.12.11.
-

46

1.3.3. ,
.
1.3.4. ,
:
)

1.4.
1.4.1.
:
1.

2.

1.4.1.1. , 1
, 1 ,
:
)

.
, 1
, .
1.4.1.2. , 2
, 2 ,
:
)

.
, 2
, .

47

1.4.2. ,
1.4.2.1. ,
-
, :
)

1.4.2.2. ,
-
() .

:
) RSA
)
)
)

1.4.3.
, .
.
1.4.4. ,

- .
,
() ..
48

,
.
1.5.

() ,
.
.
1.6.
:
)

1.7.
1.7.1.
.
1.7.2.
1
2 ,
1 (. . , 1
).

, 2 .
1.7.3.
.
1.7.4.
.
49

1.7.5. ,


,

12.2.007.0-75,

12.2.007.3 - 75,
,
.
1.7.6. ,


, 30
.

,
.
1.7.7. ,
, , II

).
1.8.
1.8.1.

, ,
50

. ,
.

, ,
,
.
1.8.2.


, .
, .

,
,

. ,
,
.

-
( ) .
, .
1.8.3.

, .
-
.

51

52

...