Bitcoin adopts public key cryptography for bitcoin exchange.
By knowing about algorism of public key cryptography,
easy to get understanding about private key and public key.
Now I'd like to talk about cryptography used some public key cryptography.
Public key cryptography
Public key cryptography is encryption method that encrypts and decrypts data using two keys paired.
A:public key A
B:public key B
Diffie-Hellman key exchange
One of the common key cryptosystem,It's a method proposed to safely deliver keys.
Below code show about how to get common key by Alice and Bob.
def pab_key(a, b): alice_priv_key = 13 bob_priv_key = 7 send_to_bob = a ** alice_priv_key % b send_to_alice = b ** bob_priv_key % a conf_alice = send_to_bob ** alice_priv_key % a conf_bob = send_to_alice ** bob_priv_key % b return(conf_alice == conf_bob) alice_pub_key = 3 bob_pub_key = 4 print(pab_key(a, b))
①Create public key(a, b)and private key(13, 7)
②Encrypting public key by private key(sed_to_bob, send_to_alice)
③Receive the encrypted data to each other , and decryption.(conf_alice, conf_bob)
④Decrypted data of each other is same, Alice and Bob could get common key.
Common key cryptosystem use same private key to cryption and decryption,so not practcal.
It is a cryptography to implement Diffie-Hellman Key exchange concept.
Use public key to cryption, private key to decryption.
Easy to adminstration of keys and interoperable excellent system.
①Alice prepare key pair（Alice's private key, Alice's public key）,publishing public key(Alice's public key) to Bob.
②Alice have Bob encrypt sentence(Hello Alice!) with public key.
③Bob sends encrypted data(6EB6957008E03CE4) ,Alice decrypt the data with private key.
encryption:c = m^e mod R
decryption:m = c^d mod R
private key:d = (n*(P-1)*(Q-1)+1)/e
e:Appropriate integer(public key)
Consider to R (modulo:finite field(0-32)) multiplied by prime number P and Q as example.
①To encrypt plain text(m=7,13,17,24) to the power of public key(e=3)
②C (m to the power of e)is an encrypted ciphertext(13,19,29,30)
③For decryption, calculate c(cryption with public key e) to the power of d(private key "3")
④Then it can get plain text(self numbers "m" and blue lie).
Now,Let's see how to calculate by RSA, while looking at the code.
Greatest common divisor
def gcd(a, b): while b != 0: a, b = b, a%b return a
Using the Euclidean Algorithm.
Greatest common divisor of "a" and "b" equal greatest common divisor factor of "a" and "a%b",
"a" is greatest common divisor of a and b when b=0.
Least common multiple
def lcm(x, y): return x * y // gcd(x, y)
using the value of greatest common divisor obtained by gcd.
Least common multiple of "a" and "b" is number obtained by dividing ”Common prime factors for "a" and "b" ” from the product of "a" and "b".
Extended Euclidean Algorithm
def gcd2(x, y): c0, c1 = x, y a0, a1 = 1, 0 b0, b1 = 0, 1 while c1 != 0: m = c0 % c1 q = c0 // c1 c0, c1 = c1, m a0, a1 = a1, (a0 - q * a1) b0, b1 = b1, (b0 - q * b1) return c0, a0, b0
Euclidean algorithm calculate greatest common divisor of to "a" and "b".
Furthermore expand to euclidean algorithm,the algorithm can find (x, y) that satisfies 「ax + by = gcd(a, b)」.
a1x + b1y = c1...(2)
(1) - q(2) ...(3)
Repeat difference calculation until X=0(namely q=1),and then find x and y .
p,q = 50231, 89959 n = p * q l = lcm(p -1, q - 1) e = False while not gcd(e, l) == 1: e = random.randint(1,l) c, a, b = gcd2(e, l) d = a % l m = 20090103
Public key"e" is smaller than "l" and greatest common divisor of e and l is '1'.
Private key "d" finds from 「ed = 1(mod l) d」.
Above formula converte to「ex + ly = 1」.
Now,use the Extended Euclidean Algorithm to find x and find the private key d.
Since gcd 2 may take a negative value,so "a% l" calculate to take a positive number.
「a% l 」return "a" when "a" is positive,Add "l" when negative.
In the case ,subtract e from b, "ex + ly = 1" holds.
c = pow(m, e, n)
→Pow(built-in function) can describe 「c = m^e mod n」.
decryption = pow(c, d, n) print(decryption == m)
decryption = pow(c, d, n)
→Pow(built-in function) can describe「m = c^d mod n」too.
print(decryption == m)
→Return True when decryption data equal message"m".That means decryption success.
The code used in this article is published in Github.