mirror of
https://github.com/billbuchanan/appliedcrypto.git
synced 2026-02-21 11:18:02 +00:00
Update
This commit is contained in:
30
unit09_future/README.md
Normal file
30
unit09_future/README.md
Normal file
@@ -0,0 +1,30 @@
|
||||

|
||||
|
||||
# Unit 9: Future Crypto
|
||||
|
||||
The key concepts are:
|
||||
|
||||
* Zero-knowledge proof (ZKP).
|
||||
* Homomophic encryption.
|
||||
* Tokenization.
|
||||
* Quantum-robust encryption.
|
||||
|
||||
## What you should know at the end of unit?
|
||||
|
||||
* Understand the usage of Light-weight cryptography.
|
||||
* Understand the usage of Zero-knowledge proofs.
|
||||
|
||||
## Material
|
||||
|
||||
|
||||
* Week 9 Lecture (Video): [here](https://youtu.be/CKZjrCnUrAM).
|
||||
* Week 9 Lecture (Video Live): [here](https://www.youtube.com/watch?v=AWMGHAVh_nE).
|
||||
* Week 9 Lecture (PDF): [here](https://asecuritysite.com/public/unit09_next_gen.pdf).
|
||||
* Week 9 Lab (PDF): [here](https://asecuritysite.com/public/lab09.pdf).
|
||||
|
||||
## A few demos and articles
|
||||
|
||||
* Format Preserving Encryption (FPE): [here](https://asecuritysite.com/encryption/fpe).
|
||||
* Light-weight crypto: [here](https://asecuritysite.com/encryption/#light).
|
||||
* ZKP: [here](https://asecuritysite.com/subjects/chapter100).
|
||||
|
||||
292
unit09_future/lab/README.md
Normal file
292
unit09_future/lab/README.md
Normal file
@@ -0,0 +1,292 @@
|
||||

|
||||
|
||||
# Unit 9: Future Crypto Lab
|
||||
|
||||
Aim: To provide a foundation in some of the up-and-coming methods in cryptography.
|
||||
|
||||
**New feature:** Repl.it code additions.
|
||||
|
||||
## Light-weight crypto
|
||||
### L1
|
||||
In many operations within public key methods we use the exponential operation:
|
||||
|
||||
g<sup>x</sup> (mod p)
|
||||
|
||||
If we compute the value of gx and then perform a (mod p) it is a very costly operation in terms of CPU as the value of gx will be large. A more efficient method it use Montgomery reduction and use pow(g,x,p).
|
||||
|
||||
```Python
|
||||
import random
|
||||
g=3
|
||||
x= random.randint(2, 100)
|
||||
n=997
|
||||
res1 = g**x % n
|
||||
res2= pow(g,x, n)
|
||||
print res1
|
||||
print res2
|
||||
```
|
||||
|
||||
Repl.it: https://repl.it/@billbuchanan/powex
|
||||
|
||||
Now add some code to determine the time taken to perform each of the two operations, and compare them:
|
||||
|
||||
Can you now put each of the methods into a loop, and perform each calculation 1,000 times?
|
||||
|
||||
Now measure the times taken. What do you observe?
|
||||
|
||||
|
||||
Now increase the range for x (so that it is relatively large) and make n a large prime number. What do you observe from the performance:
|
||||
|
||||
|
||||
### L2
|
||||
Normally light-weight crypto has to be fast and efficient. The XTEA method is one of the fastest around. Some standard open source code in Node.js is (use npm install xtea):
|
||||
|
||||
```Node.js
|
||||
var xtea = require('xtea');
|
||||
|
||||
var plaintext = new Buffer('ABCDEFGH', 'utf8');
|
||||
var key = new Buffer('0123456789ABCDEF0123456789ABCDEF', 'hex');
|
||||
var ciphertext = xtea.encrypt( plaintext, key );
|
||||
|
||||
console.log('Cipher:\t'+ ciphertext.toString('hex') );
|
||||
console.log('Decipher:\t'+ xtea.decrypt( ciphertext, key ).toString() );
|
||||
```
|
||||
|
||||
Repl.it: https://repl.it/@billbuchanan/xteajs
|
||||
|
||||
A sample run is:
|
||||
<pre>
|
||||
Cipher: 52deb267335dd52a49837931c233cea8
|
||||
Decipher: ABCDEFGH
|
||||
</pre>
|
||||
|
||||
|
||||
|
||||
What is the block and key size of XTEA?
|
||||
|
||||
|
||||
|
||||
Can you add some code to measure the time taken for 1,000 encryptions?
|
||||
|
||||
|
||||
Can you estimate the number for encryption keys that could be tried per second on your system?
|
||||
|
||||
|
||||
If possible, run the code on another machine, and estimate the rate of encryption keys that can be used per second:
|
||||
|
||||
|
||||
|
||||
### L3
|
||||
RC4 is a stream cipher created by Ron Rivest and has a variable key length. Run the following Python code and test it:
|
||||
|
||||
```Python
|
||||
def KSA(key):
|
||||
keylength = len(key)
|
||||
|
||||
S = range(256)
|
||||
|
||||
j = 0
|
||||
for i in range(256):
|
||||
j = (j + S[i] + key[i % keylength]) % 256
|
||||
S[i], S[j] = S[j], S[i] # swap
|
||||
|
||||
return S
|
||||
|
||||
|
||||
def PRGA(S):
|
||||
i = 0
|
||||
j = 0
|
||||
while True:
|
||||
i = (i + 1) % 256
|
||||
j = (j + S[i]) % 256
|
||||
S[i], S[j] = S[j], S[i] # swap
|
||||
|
||||
K = S[(S[i] + S[j]) % 256]
|
||||
yield K
|
||||
|
||||
def RC4(key):
|
||||
S = KSA(key)
|
||||
return PRGA(S)
|
||||
|
||||
def asctohex(string_in):
|
||||
a=""
|
||||
for x in string_in:
|
||||
a = a + ("0"+((hex(ord(x)))[2:]))[-2:]
|
||||
return(a)
|
||||
|
||||
def convert_key(s):
|
||||
return [ord(c) for c in s]
|
||||
|
||||
key="0102030405"
|
||||
|
||||
plaintext = 'Hello'
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
plaintext=str(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
key=str(sys.argv[2])
|
||||
|
||||
key = key.decode('hex')
|
||||
key = convert_key(key)
|
||||
|
||||
keystream = RC4(key)
|
||||
print "Keystream: ",
|
||||
for i in range (0,15):
|
||||
print hex(keystream.next()),
|
||||
print
|
||||
print "Cipher: ",
|
||||
keystream = RC4(key)
|
||||
|
||||
for c in plaintext:
|
||||
sys.stdout.write("%02X" % (ord(c) ^ keystream.next()))
|
||||
```
|
||||
Repl.it: https://repl.it/@billbuchanan/rc4tut
|
||||
|
||||
Now go to https://tools.ietf.org/html/rfc6229 and test a few key generation values and see if you get the same key stream.
|
||||
|
||||
Tests:
|
||||
|
||||
Key: 0102030405 Key stream (first six bytes):
|
||||
|
||||
Key: Key stream (first six bytes):
|
||||
|
||||
Key: Key stream (first six bytes):
|
||||
|
||||
Key: Key stream (first six bytes):
|
||||
|
||||
How does the Python code produce a key stream length which matches the input data stream:
|
||||
|
||||
|
||||
Can you test the code by decrypting the cipher stream (note: you just use the same code, and do the same operation again)?
|
||||
|
||||
|
||||
RC4 uses an s-Box. Can you find a way to print out the S-box values for a key of “0102030405”?
|
||||
|
||||
|
||||
What are the main advantages of having a variable key size and having a stream cipher in light-weight cryptography?
|
||||
|
||||
|
||||
|
||||
### L4
|
||||
The ELLI method can be used to identify an RFID tag.
|
||||
|
||||
|
||||
|
||||
Can you run the following code and determine that it works (C and D should be the same)? Can you also explain how it works?
|
||||
|
||||
|
||||
|
||||
```Python
|
||||
from os import urandom
|
||||
from eccsnacks.curve25519 import scalarmult, scalarmult_base
|
||||
import binascii
|
||||
|
||||
lamb = urandom(32)
|
||||
a = scalarmult_base(lamb)
|
||||
|
||||
eps = urandom(32)
|
||||
b = scalarmult_base(eps)
|
||||
|
||||
c = scalarmult(eps, a)
|
||||
|
||||
d = scalarmult(lamb, b)
|
||||
|
||||
print "RFID private key: ",binascii.hexlify(eps)
|
||||
|
||||
print "Reader private key: ",binascii.hexlify(lamb)
|
||||
|
||||
print
|
||||
print "A value: ",binascii.hexlify(a)
|
||||
print "B value: ",binascii.hexlify(b)
|
||||
|
||||
print "C value: ",binascii.hexlify(c)
|
||||
print "D value: ",binascii.hexlify(d)
|
||||
```
|
||||
Repl.it: https://repl.it/@billbuchanan/elli
|
||||
|
||||
## 3 Zero-knowledge proof (ZKP)
|
||||
|
||||
### L5
|
||||
With ZKP, Alice can prove that he still knows something to Bob, without revealing her secret. At the basis of many methods is the Fiat-Shamir method:
|
||||
|
||||
|
||||
Ref: https://asecuritysite.com/encryption/fiat
|
||||
|
||||
Repl.it: https://repl.it/@billbuchanan/zktut2
|
||||
|
||||
The following code implements some basic code for Fiat-Shamir, can you prove that for a number of values of x, that Alice will always be able to prove that she knows x.
|
||||
|
||||
x: Proved: Y/N
|
||||
x: Proved: Y/N
|
||||
x: Proved: Y/N
|
||||
x: Proved: Y/N
|
||||
|
||||
The value of n is a prime number. Now increase the value of n, and determine the effect that this has on the time taken to compute the proof:
|
||||
|
||||
|
||||
|
||||
```Python
|
||||
import sys
|
||||
import random
|
||||
|
||||
n=97
|
||||
|
||||
g= 5
|
||||
|
||||
x = random.randint(1,5)
|
||||
v = random.randint(n//2,n)
|
||||
c = random.randint(1,5)
|
||||
|
||||
y= pow(g,x, n)
|
||||
|
||||
t = pow(g,v,n)
|
||||
|
||||
r = (v - c * x)
|
||||
|
||||
print r
|
||||
if (r<0): r=-r
|
||||
|
||||
Result = ( pow(g,r,n)) * (pow(y,c,n)) % n
|
||||
|
||||
|
||||
print 'x=',x
|
||||
print 'c=',c
|
||||
print 'v=',v
|
||||
print 'P=',n
|
||||
print 'G=',g
|
||||
print '======'
|
||||
print 't=',t
|
||||
print 'r=',Result
|
||||
if (t==Result):
|
||||
print 'Alice has proven she knows x'
|
||||
else:
|
||||
print 'Alice has not proven she knows x'
|
||||
```
|
||||
|
||||
Repl.it: https://repl.it/@billbuchanan/zktut
|
||||
|
||||
### L6
|
||||
We can now expand this method by creating a password, and then making this the secret. Copy and run the code here:
|
||||
|
||||
https://asecuritysite.com/encryption/fiat2
|
||||
|
||||
Repl.it: https://repl.it/@billbuchanan/zktut2
|
||||
|
||||
Now test the code with different passwords?
|
||||
|
||||
|
||||
How does the password get converting into a form which can be used in the Fiat-Shamir method?
|
||||
|
||||
|
||||
|
||||
### L1.7
|
||||
The Diffie-Hellman method can be used to perform a zero-knowledge proof implementation. Copy the code from the following link and verify that it works:
|
||||
|
||||
https://asecuritysite.com/encryption/diffiez
|
||||
|
||||
Repl.it: https://repl.it/@billbuchanan/diffiez
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
102
unit09_future/lab/diffiez.py
Normal file
102
unit09_future/lab/diffiez.py
Normal file
@@ -0,0 +1,102 @@
|
||||
# zkp-dh: Zero-knowledge proof generator and verifier for one party to show
|
||||
# to another that their Diffie-Hellman shared secret is correct.
|
||||
# See the Camenisch and Stadler paper for procedural specifics on ZKP
|
||||
# proof generation, such as knowledge of discrete logarithm.
|
||||
|
||||
# Lining Wang, June 2014
|
||||
|
||||
import random
|
||||
import hashlib
|
||||
import binascii
|
||||
import sys
|
||||
|
||||
# DiffieHellman class enables construction of keys capable of performing
|
||||
# D-H exchanges, and interactive proof of knowledge
|
||||
class DiffieHellman:
|
||||
P = 101
|
||||
G = 51
|
||||
|
||||
def __init__(self,secret=0):
|
||||
if (secret==0):
|
||||
self.secret = random.randrange(1 << (self.G.bit_length() - 1), self.G - 1)
|
||||
else:
|
||||
self.secret = secret
|
||||
self.public = pow(self.G, self.secret, self.P)
|
||||
|
||||
# get shared secret: (g^b)^a mod p
|
||||
def get_shared_secret(self, remote_pub):
|
||||
return pow(remote_pub, self.secret, self.P)
|
||||
|
||||
# Given the public key of B (remote_pub), shows that the shared secret
|
||||
# between A and B was generated by A.
|
||||
# Returns zero-knowledge proof of shared Diffie-Hellman secret between A & B.
|
||||
def prove_shared_secret(self, remote_pub):
|
||||
G = self.G; prover_pub = self.public; phi = self. P - 1;
|
||||
secret = self.get_shared_secret(remote_pub)
|
||||
|
||||
# Random key in the group Z_q
|
||||
randKey = DiffieHellman() # random secret
|
||||
commit1 = randKey.public
|
||||
commit2 = randKey.get_shared_secret(remote_pub)
|
||||
|
||||
# shift and hash
|
||||
concat = str(G) + str(prover_pub) + str(remote_pub) + str(secret) + str(commit1) + str(commit2)
|
||||
h = hashlib.md5()
|
||||
h.update(concat.encode("utf-8"))
|
||||
challenge = int(h.hexdigest(), 16)
|
||||
product = (self.secret * challenge) % phi
|
||||
response = (randKey.secret - product) % phi
|
||||
|
||||
return (secret, challenge, response)
|
||||
|
||||
# Verifies proof generated above. Verifier c is showing that
|
||||
# shared secret between A and B was generated by A.
|
||||
# returns 0 if if verification fails; returns shared secret otherwise
|
||||
def verify_shared_secret(self, prover_pub, remote_pub, secret, challenge,
|
||||
response):
|
||||
P = self.P; G = self.G ; public = self.public
|
||||
|
||||
# g^r * (a's public key)^challenge
|
||||
commit1 = (pow(G, response, P) * pow(public, challenge, P)) % P
|
||||
|
||||
# (b's public key)^response * (secret)^challenge
|
||||
commit2 = (pow(remote_pub, response, P) * pow(secret, challenge, P)) % P
|
||||
|
||||
# Shift and hash
|
||||
hasher = hashlib.md5()
|
||||
concat = str(G) + str(prover_pub) + str(remote_pub) + str(secret) + str(commit1) + str(commit2)
|
||||
hasher.update(concat.encode("utf-8"))
|
||||
check = int(hasher.hexdigest(), 16)
|
||||
|
||||
if challenge == check:
|
||||
return secret
|
||||
else:
|
||||
return 0
|
||||
|
||||
x=3
|
||||
y=4
|
||||
|
||||
|
||||
a = DiffieHellman(x)
|
||||
b = DiffieHellman(y)
|
||||
|
||||
print("G=",a.G)
|
||||
print("p=",a.P)
|
||||
print("x=",x)
|
||||
print("y=",y)
|
||||
print("\n============")
|
||||
|
||||
print("a (pub,sec)=",a.public,a.secret)
|
||||
print("b (pub,sec)=",b.public,b.secret)
|
||||
shared=a.get_shared_secret(b.public)
|
||||
print("Shared=",shared)
|
||||
print("\nNow Bob will generate the secret, a challenge and a response")
|
||||
results = a.prove_shared_secret(b.public)
|
||||
print("(secret, challenge, response):",results)
|
||||
|
||||
val=a.verify_shared_secret(a.public, b.public, results[0], results[1], results[2])
|
||||
print("\nAlice now checks")
|
||||
if (val==shared):
|
||||
print("Bob has proven he knows x")
|
||||
else:
|
||||
print("Bob has not proven that he knows x")
|
||||
102
unit09_future/lab/diffiez.py.bak
Normal file
102
unit09_future/lab/diffiez.py.bak
Normal file
@@ -0,0 +1,102 @@
|
||||
# zkp-dh: Zero-knowledge proof generator and verifier for one party to show
|
||||
# to another that their Diffie-Hellman shared secret is correct.
|
||||
# See the Camenisch and Stadler paper for procedural specifics on ZKP
|
||||
# proof generation, such as knowledge of discrete logarithm.
|
||||
|
||||
# Lining Wang, June 2014
|
||||
|
||||
import random
|
||||
import hashlib
|
||||
import binascii
|
||||
import sys
|
||||
|
||||
# DiffieHellman class enables construction of keys capable of performing
|
||||
# D-H exchanges, and interactive proof of knowledge
|
||||
class DiffieHellman:
|
||||
P = 101
|
||||
G = 51
|
||||
|
||||
def __init__(self,secret=0):
|
||||
if (secret==0):
|
||||
self.secret = random.randrange(1 << (self.G.bit_length() - 1), self.G - 1)
|
||||
else:
|
||||
self.secret = secret
|
||||
self.public = pow(self.G, self.secret, self.P)
|
||||
|
||||
# get shared secret: (g^b)^a mod p
|
||||
def get_shared_secret(self, remote_pub):
|
||||
return pow(remote_pub, self.secret, self.P)
|
||||
|
||||
# Given the public key of B (remote_pub), shows that the shared secret
|
||||
# between A and B was generated by A.
|
||||
# Returns zero-knowledge proof of shared Diffie-Hellman secret between A & B.
|
||||
def prove_shared_secret(self, remote_pub):
|
||||
G = self.G; prover_pub = self.public; phi = self. P - 1;
|
||||
secret = self.get_shared_secret(remote_pub)
|
||||
|
||||
# Random key in the group Z_q
|
||||
randKey = DiffieHellman() # random secret
|
||||
commit1 = randKey.public
|
||||
commit2 = randKey.get_shared_secret(remote_pub)
|
||||
|
||||
# shift and hash
|
||||
concat = str(G) + str(prover_pub) + str(remote_pub) + str(secret) + str(commit1) + str(commit2)
|
||||
h = hashlib.md5()
|
||||
h.update(concat.encode("utf-8"))
|
||||
challenge = int(h.hexdigest(), 16)
|
||||
product = (self.secret * challenge) % phi
|
||||
response = (randKey.secret - product) % phi
|
||||
|
||||
return (secret, challenge, response)
|
||||
|
||||
# Verifies proof generated above. Verifier c is showing that
|
||||
# shared secret between A and B was generated by A.
|
||||
# returns 0 if if verification fails; returns shared secret otherwise
|
||||
def verify_shared_secret(self, prover_pub, remote_pub, secret, challenge,
|
||||
response):
|
||||
P = self.P; G = self.G ; public = self.public
|
||||
|
||||
# g^r * (a's public key)^challenge
|
||||
commit1 = (pow(G, response, P) * pow(public, challenge, P)) % P
|
||||
|
||||
# (b's public key)^response * (secret)^challenge
|
||||
commit2 = (pow(remote_pub, response, P) * pow(secret, challenge, P)) % P
|
||||
|
||||
# Shift and hash
|
||||
hasher = hashlib.md5()
|
||||
concat = str(G) + str(prover_pub) + str(remote_pub) + str(secret) + str(commit1) + str(commit2)
|
||||
hasher.update(concat.encode("utf-8"))
|
||||
check = int(hasher.hexdigest(), 16)
|
||||
|
||||
if challenge == check:
|
||||
return secret
|
||||
else:
|
||||
return 0
|
||||
|
||||
x=3
|
||||
y=4
|
||||
|
||||
|
||||
a = DiffieHellman(x)
|
||||
b = DiffieHellman(y)
|
||||
|
||||
print "G=",a.G
|
||||
print "p=",a.P
|
||||
print "x=",x
|
||||
print "y=",y
|
||||
print "\n============"
|
||||
|
||||
print "a (pub,sec)=",a.public,a.secret
|
||||
print "b (pub,sec)=",b.public,b.secret
|
||||
shared=a.get_shared_secret(b.public)
|
||||
print "Shared=",shared
|
||||
print "\nNow Bob will generate the secret, a challenge and a response"
|
||||
results = a.prove_shared_secret(b.public)
|
||||
print "(secret, challenge, response):",results
|
||||
|
||||
val=a.verify_shared_secret(a.public, b.public, results[0], results[1], results[2])
|
||||
print "\nAlice now checks"
|
||||
if (val==shared):
|
||||
print "Bob has proven he knows x"
|
||||
else:
|
||||
print "Bob has not proven that he knows x"
|
||||
BIN
unit09_future/lab/lab09.docx
Normal file
BIN
unit09_future/lab/lab09.docx
Normal file
Binary file not shown.
BIN
unit09_future/lab/lab09.pdf
Normal file
BIN
unit09_future/lab/lab09.pdf
Normal file
Binary file not shown.
BIN
unit09_future/lab/~$lab09.docx
Normal file
BIN
unit09_future/lab/~$lab09.docx
Normal file
Binary file not shown.
24
unit09_future/lecture/README.md
Normal file
24
unit09_future/lecture/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||

|
||||
|
||||
# Unit 9: Future Crypto
|
||||
|
||||
The key concepts are:
|
||||
|
||||
* Zero-knowledge proof (ZKP).
|
||||
* Homomophic encryption.
|
||||
* Tokenization.
|
||||
* Quantum-robust encryption.
|
||||
|
||||
## What you should know at the end of unit?
|
||||
|
||||
* Understand the usage of Light-weight cryptography.
|
||||
* Understand the usage of Zero-knowledge proofs.
|
||||
|
||||
## Material
|
||||
|
||||
* Week 9 Lecture (Video): [here](https://youtu.be/CKZjrCnUrAM).
|
||||
* Week 9 Lecture (Video Live): [here](https://www.youtube.com/watch?v=AWMGHAVh_nE).
|
||||
* Week 9 Lecture (PDF): [here](https://asecuritysite.com/public/unit09_next_gen.pdf).
|
||||
|
||||
|
||||
|
||||
BIN
unit09_future/lecture/unit09_next_gen.pdf
Normal file
BIN
unit09_future/lecture/unit09_next_gen.pdf
Normal file
Binary file not shown.
BIN
unit09_future/lecture/unit09_next_gen.pptx
Normal file
BIN
unit09_future/lecture/unit09_next_gen.pptx
Normal file
Binary file not shown.
42
unit09_future/src/a_01.py
Normal file
42
unit09_future/src/a_01.py
Normal file
@@ -0,0 +1,42 @@
|
||||
# Zero-knowledge Proof: Proving age with hash chains.
|
||||
# https://asecuritysite.com/encryption/age
|
||||
|
||||
import hashlib;
|
||||
import passlib.hash;
|
||||
import sys;
|
||||
|
||||
age_actual=19
|
||||
age_to_prove=18
|
||||
seed=b"12345667"
|
||||
|
||||
proof = hashlib.md5(seed)
|
||||
encrypted_age = hashlib.md5(seed)
|
||||
|
||||
for i in range(1,1+age_actual-age_to_prove):
|
||||
proof = hashlib.md5(proof.digest())
|
||||
|
||||
for i in range(1,age_actual+1):
|
||||
encrypted_age = hashlib.md5(encrypted_age.digest())
|
||||
|
||||
verfied_age=proof
|
||||
|
||||
for i in range(0,age_to_prove):
|
||||
verfied_age = hashlib.md5(verfied_age.digest())
|
||||
|
||||
|
||||
|
||||
print "Peggy's Age:\t\t",age_actual
|
||||
print "Age to prove:\t\t",age_to_prove
|
||||
|
||||
print "...."
|
||||
|
||||
|
||||
print "Proof:\t\t",proof.hexdigest()
|
||||
print "Encr Age:\t",encrypted_age.hexdigest()
|
||||
print "Verified Age:\t",verfied_age.hexdigest()
|
||||
|
||||
if (encrypted_age.hexdigest()==verfied_age.hexdigest()):
|
||||
print "You have proven your age ... please come in"
|
||||
else:
|
||||
print "You have not proven you age!"
|
||||
|
||||
34
unit09_future/src/a_02.py
Normal file
34
unit09_future/src/a_02.py
Normal file
@@ -0,0 +1,34 @@
|
||||
# Zero-knowledge proof (discrete logs).
|
||||
# https://asecuritysite.com/encryption/z
|
||||
|
||||
import sys
|
||||
|
||||
p=71
|
||||
g=13
|
||||
x=7
|
||||
r=8
|
||||
|
||||
print 'p=',p
|
||||
print 'g=',g
|
||||
print 'x=',x
|
||||
print 'r=',r
|
||||
print '========'
|
||||
|
||||
y= g**x % p
|
||||
|
||||
print 'Y=',y
|
||||
|
||||
C = g**r % p
|
||||
print 'C=',C
|
||||
|
||||
print '========'
|
||||
val1=g**((x+r)%(p-1)) % p
|
||||
print 'g^(x+r)%(p-1) mod p=',val1
|
||||
|
||||
val2=C*y %p
|
||||
print 'C.y mod P=',val2
|
||||
|
||||
if (val1==val2):
|
||||
print 'Well done ... have you proven that you know x'
|
||||
else:
|
||||
print 'Not proven'
|
||||
37
unit09_future/src/a_03.py
Normal file
37
unit09_future/src/a_03.py
Normal file
@@ -0,0 +1,37 @@
|
||||
import sys
|
||||
import random
|
||||
|
||||
n=101
|
||||
g= 3
|
||||
|
||||
ans=7
|
||||
|
||||
x = 3
|
||||
y = 4
|
||||
|
||||
E1= g**( (x+y) % (n-1)) % n
|
||||
|
||||
E2= (g**x * g**y) % n
|
||||
|
||||
E3 = g**(ans) % n
|
||||
|
||||
print '======Agreed parameters============'
|
||||
print 'P=',n,'\t(Prime number)'
|
||||
print 'G=',g,'\t(Generator)'
|
||||
print 'x=',x,'\t(Value 1 - Alice first value)'
|
||||
print 'y=',y,'\t(value 2 - Alice second value)'
|
||||
print 'ans=',ans,'\t(Answer = x+y?)'
|
||||
|
||||
print '======Encrypted values============'
|
||||
print 'g^x=',(g**x) % n
|
||||
print 'g^y=',(g**y) % n
|
||||
|
||||
print '======zkSnark===================='
|
||||
print 'E1=',E1
|
||||
print 'E2=',E2
|
||||
print 'E3=',E3
|
||||
if (E2==E3):
|
||||
print 'Alice has proven she knows the sum is ',ans
|
||||
else:
|
||||
print 'Alice has proven she does not know the sum is ',ans
|
||||
|
||||
40
unit09_future/src/a_04.py
Normal file
40
unit09_future/src/a_04.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import sys
|
||||
import random
|
||||
|
||||
n=101
|
||||
g= 3
|
||||
|
||||
x=5
|
||||
|
||||
a = 3
|
||||
b = 4
|
||||
|
||||
# eqn = ax + b x^2
|
||||
|
||||
|
||||
E1= g**( a *x ) % n
|
||||
|
||||
E2= g**(b*x*x) % n
|
||||
|
||||
E3 = (E1 * E2) % n
|
||||
E4 = g**(a*x + b*x*x) % n
|
||||
|
||||
|
||||
|
||||
print '======Agreed parameters============'
|
||||
print 'P=',n,'\t(Prime number)'
|
||||
print 'G=',g,'\t(Generator)'
|
||||
print 'a=',a
|
||||
print 'b=',b
|
||||
print 'x=',x,'\t(Eqn= ax + bx^2)'
|
||||
|
||||
|
||||
print '======zkSnark===================='
|
||||
|
||||
print 'E3=',E3
|
||||
print 'E4=',E4
|
||||
|
||||
if (E3==E4):
|
||||
print 'Alice has computed the result'
|
||||
else:
|
||||
print 'Alice has proven she does not know result'
|
||||
32
unit09_future/src/a_05.py
Normal file
32
unit09_future/src/a_05.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import sys
|
||||
import random
|
||||
|
||||
n=101
|
||||
|
||||
g= 3
|
||||
|
||||
x = random.randint(5,10)
|
||||
v = random.randint(100,150)
|
||||
c = random.randint(5,10)
|
||||
|
||||
y= g**x % n
|
||||
|
||||
t = g**v % n
|
||||
|
||||
r = v - c * x
|
||||
|
||||
Result = ( (g**r) * (y**c) ) % n
|
||||
|
||||
|
||||
print 'x=',x
|
||||
print 'c=',c
|
||||
print 'v=',v
|
||||
print 'P=',n
|
||||
print 'G=',g
|
||||
print '======'
|
||||
print 't=',t
|
||||
print 'r=',Result
|
||||
if (t==Result):
|
||||
print 'Alice has proven she knows x'
|
||||
else:
|
||||
print 'Alice has not proven she knows x'
|
||||
26
unit09_future/src/a_06.py
Normal file
26
unit09_future/src/a_06.py
Normal file
@@ -0,0 +1,26 @@
|
||||
n=101*23
|
||||
r=13
|
||||
|
||||
s1=5
|
||||
s2=7
|
||||
s3=3
|
||||
|
||||
a1=1
|
||||
a2=0
|
||||
a3=1
|
||||
|
||||
print 'N=',n
|
||||
x = (r**2) % n
|
||||
print 'x=',x
|
||||
print 's1=',s1,'s2=',s2,'s3=',s3
|
||||
print 'a1=',a1,'a2=',a2,'a3=',a3
|
||||
|
||||
y = (r * ((s1**a1) * (s2**a2) * (s3**a3)) ) % n
|
||||
print 'Y=',y, ' y^2 mod n = ',(y**2 % n)
|
||||
|
||||
v1=(s1**2) %n
|
||||
v2=(s2**2) %n
|
||||
v3=(s3**2) %n
|
||||
|
||||
y2 = (x * ( (v1**a1) * (v2**a2) * (v3**a3)) ) % n
|
||||
print 'Y=',(y**2) %n
|
||||
55
unit09_future/src/a_07.py
Normal file
55
unit09_future/src/a_07.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import random
|
||||
|
||||
p=59
|
||||
g=13
|
||||
x=11
|
||||
v=9
|
||||
|
||||
|
||||
def string2numeric_hash(text):
|
||||
import hashlib
|
||||
return int(hashlib.md5(text).hexdigest()[:8], 16)
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
g=int(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
x=int(sys.argv[2])
|
||||
|
||||
v= random.randint(3, 8)
|
||||
|
||||
print 'g=',g
|
||||
print 'x=',x, ' (the secret)'
|
||||
print 'v=',v, ' (random)'
|
||||
print '=====Alice computes========='
|
||||
|
||||
import hashlib
|
||||
|
||||
y= g**x
|
||||
t= g**v
|
||||
|
||||
print 't=',t
|
||||
|
||||
print 'y=',y
|
||||
|
||||
c = string2numeric_hash(str(g)+str(y)+str(t))
|
||||
c =c % p
|
||||
|
||||
print 'c=',c
|
||||
|
||||
r= v -c*x
|
||||
|
||||
print '=============='
|
||||
|
||||
print 'Alice sends (t,r)=(',str(t),',',(r),')'
|
||||
|
||||
t1 = (g**r)
|
||||
t2= (y**c)
|
||||
|
||||
val=int(t1*t2)
|
||||
print 'My calc for g^r x y^c=',val
|
||||
|
||||
if (val==t):
|
||||
print "Alice has proven her ID"
|
||||
else:
|
||||
print "You are a fraud"
|
||||
55
unit09_future/src/a_08.py
Normal file
55
unit09_future/src/a_08.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import random
|
||||
|
||||
p=59
|
||||
g=13
|
||||
x=11
|
||||
v=9
|
||||
|
||||
|
||||
def string2numeric_hash(text):
|
||||
import hashlib
|
||||
return int(hashlib.md5(text).hexdigest()[:8], 16)
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
g=int(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
x=int(sys.argv[2])
|
||||
|
||||
v= random.randint(3, 8)
|
||||
|
||||
print 'g=',g
|
||||
print 'x=',x, ' (the secret)'
|
||||
print 'v=',v, ' (random)'
|
||||
print '=====Alice computes========='
|
||||
|
||||
import hashlib
|
||||
|
||||
y= g**x
|
||||
t= g**v
|
||||
|
||||
print 't=',t
|
||||
|
||||
print 'y=',y
|
||||
|
||||
c = string2numeric_hash(str(g)+str(y)+str(t))
|
||||
c =c % p
|
||||
|
||||
print 'c=',c
|
||||
|
||||
r= v -c*x
|
||||
|
||||
print '=============='
|
||||
|
||||
print 'Alice sends (t,r)=(',str(t),',',(r),')'
|
||||
|
||||
t1 = (g**r)
|
||||
t2= (y**c)
|
||||
|
||||
val=int(t1*t2)
|
||||
print 'My calc for g^r x y^c=',val
|
||||
|
||||
if (val==t):
|
||||
print "Alice has proven her ID"
|
||||
else:
|
||||
print "You are a fraud"
|
||||
40
unit09_future/src/a_09.py
Normal file
40
unit09_future/src/a_09.py
Normal file
@@ -0,0 +1,40 @@
|
||||
import sys
|
||||
import uuid
|
||||
import hashlib
|
||||
import random
|
||||
|
||||
def hash_password(password):
|
||||
salt = uuid.uuid4().hex
|
||||
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
|
||||
def check_password(hashed_password, user_password):
|
||||
password, salt = hashed_password.split(':')
|
||||
return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
|
||||
bob=random.randint(1, 1000)
|
||||
hash_bob = hash_password(str(bob))
|
||||
alice=random.randint(1, 1000)
|
||||
hash_alice = hash_password(str(alice))
|
||||
print '\n===Bob random and hash=====\n'
|
||||
print 'Bob random=',bob
|
||||
print 'Bob hash=',hash_bob
|
||||
print '\n===Alice random and hash=====\n'
|
||||
print 'Alice random=',alice
|
||||
print 'Alice hash=',hash_alice
|
||||
coin=(bob & 0x1) ^ (alice & 0x1)
|
||||
if (coin==0):
|
||||
print 'Heads ',
|
||||
else:
|
||||
print 'Tails ',
|
||||
print '\n====Checking the flips ====\n'
|
||||
print 'Alice checks value with salt: ',check_password(hash_bob,str(bob))
|
||||
print 'Bob checks value with salt: ',check_password(hash_alice,str(alice))
|
||||
print '\n====10 random flips====\n'
|
||||
for i in range(1,10):
|
||||
bob=random.randint(1, 1000)
|
||||
hash_bob = hash_password(str(bob))
|
||||
alice=random.randint(1, 1000)
|
||||
hash_alice = hash_password(str(alice))
|
||||
coin=(bob & 0x1) ^ (alice & 0x1)
|
||||
if (coin==0):
|
||||
print 'Heads ',
|
||||
else:
|
||||
print 'Tails ',
|
||||
37
unit09_future/src/a_10.py
Normal file
37
unit09_future/src/a_10.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from phe import paillier
|
||||
import sys
|
||||
vote1=100
|
||||
vote2=200
|
||||
|
||||
def num(s):
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
return float(s)
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
vote1=num(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
vote2=num(sys.argv[2])
|
||||
|
||||
public_key, private_key = paillier.generate_paillier_keypair()
|
||||
|
||||
keyring = paillier.PaillierPrivateKeyring()
|
||||
|
||||
keyring.add(private_key)
|
||||
|
||||
public_key1, private_key1 = paillier.generate_paillier_keypair(keyring)
|
||||
|
||||
|
||||
print 'Votes 1=',vote1
|
||||
print 'Votes 2=',vote2
|
||||
|
||||
encrypted1= public_key.encrypt(vote1)
|
||||
print 'Encrypted1=',encrypted1
|
||||
|
||||
encrypted2= public_key.encrypt(vote2)
|
||||
|
||||
print 'Encrypted2=',encrypted2
|
||||
|
||||
print 'Result =',private_key.decrypt(encrypted1+encrypted2)
|
||||
37
unit09_future/src/a_11.py
Normal file
37
unit09_future/src/a_11.py
Normal file
@@ -0,0 +1,37 @@
|
||||
from phe import paillier
|
||||
import sys
|
||||
vote1=100
|
||||
vote2=200
|
||||
|
||||
def num(s):
|
||||
try:
|
||||
return int(s)
|
||||
except ValueError:
|
||||
return float(s)
|
||||
|
||||
if (len(sys.argv)>1):
|
||||
vote1=num(sys.argv[1])
|
||||
|
||||
if (len(sys.argv)>2):
|
||||
vote2=num(sys.argv[2])
|
||||
|
||||
public_key, private_key = paillier.generate_paillier_keypair()
|
||||
|
||||
keyring = paillier.PaillierPrivateKeyring()
|
||||
|
||||
keyring.add(private_key)
|
||||
|
||||
public_key1, private_key1 = paillier.generate_paillier_keypair(keyring)
|
||||
|
||||
|
||||
print 'Votes 1=',vote1
|
||||
print 'Votes 2=',vote2
|
||||
|
||||
encrypted1= public_key.encrypt(vote1)
|
||||
print 'Encrypted1=',encrypted1
|
||||
|
||||
encrypted2= public_key.encrypt(vote2)
|
||||
|
||||
print 'Encrypted2=',encrypted2
|
||||
|
||||
print 'Result =',private_key.decrypt(encrypted1+encrypted2)
|
||||
77
unit09_future/src/a_12.py
Normal file
77
unit09_future/src/a_12.py
Normal file
@@ -0,0 +1,77 @@
|
||||
from cryptography.fernet import Fernet
|
||||
import sys
|
||||
import binascii
|
||||
|
||||
operator = "a & b"
|
||||
x=0
|
||||
y=0
|
||||
|
||||
operator=operator.replace('or','|')
|
||||
operator=operator.replace('and','&')
|
||||
operator=operator.replace('xor','^')
|
||||
operator=operator.replace('not','~')
|
||||
|
||||
print "---Input parameters---"
|
||||
print "Operation:",operator
|
||||
print "Input:",x,y
|
||||
|
||||
keyX_0 = Fernet.generate_key()
|
||||
keyX_1 = Fernet.generate_key()
|
||||
keyY_0 = Fernet.generate_key()
|
||||
keyY_1 = Fernet.generate_key()
|
||||
|
||||
data =[]
|
||||
for a in range(0,2):
|
||||
for b in range(0,2):
|
||||
data.append(str(eval(operator) & 0x01))
|
||||
print "Outputs of function:",data
|
||||
|
||||
print "\n---Keys generated---"
|
||||
|
||||
print "KeyX_0 (first 20 characters):"+binascii.hexlify(bytearray(keyX_0))[:20]
|
||||
print "KeyX_1 (first 20 characters):"+binascii.hexlify(bytearray(keyX_1))[:20]
|
||||
print "KeyY_0 (first 20 characters):"+binascii.hexlify(bytearray(keyY_0))[:20]
|
||||
print "KeyY_1 (first 20 characters):"+binascii.hexlify(bytearray(keyY_1))[:20]
|
||||
|
||||
print "\n---Cipers send from Bob to Alice---"
|
||||
|
||||
|
||||
cipher_text00 = Fernet(keyY_0).encrypt(Fernet(keyX_0).encrypt(data[0]))
|
||||
cipher_text01 = Fernet(keyY_0).encrypt(Fernet(keyX_1).encrypt(data[1]))
|
||||
cipher_text10 = Fernet(keyY_1).encrypt(Fernet(keyX_0).encrypt(data[2]))
|
||||
cipher_text11 = Fernet(keyY_1).encrypt(Fernet(keyX_1).encrypt(data[3]))
|
||||
|
||||
print "Cipher (first 20 chars): "+binascii.hexlify(bytearray(cipher_text00))[:40]
|
||||
print "Cipher (first 20 chars): "+binascii.hexlify(bytearray(cipher_text01))[:40]
|
||||
print "Cipher (first 20 chars): "+binascii.hexlify(bytearray(cipher_text10))[:40]
|
||||
print "Cipher (first 20 chars): "+binascii.hexlify(bytearray(cipher_text11))[:40]
|
||||
|
||||
|
||||
if (x==0): keyB = keyX_0
|
||||
if (x==1): keyB = keyX_1
|
||||
|
||||
if (y==0): keyA = keyY_0
|
||||
if (y==1): keyA = keyY_1
|
||||
|
||||
print "\n---Bob and Alice's key---"
|
||||
print "Bob's key: "+binascii.hexlify(bytearray(keyB))[:20]
|
||||
print "Alice's key: "+binascii.hexlify(bytearray(keyA))[:20]
|
||||
|
||||
print "\n---Decrypt with keys (where '.' is an exception):"
|
||||
|
||||
try:
|
||||
print Fernet(keyB).decrypt(Fernet(keyA).decrypt(cipher_text00)),
|
||||
except:
|
||||
print ".",
|
||||
try:
|
||||
print Fernet(keyB).decrypt(Fernet(keyA).decrypt(cipher_text01)),
|
||||
except:
|
||||
print ".",
|
||||
try:
|
||||
print Fernet(keyB).decrypt(Fernet(keyA).decrypt(cipher_text10)),
|
||||
except:
|
||||
print ".",
|
||||
try:
|
||||
print Fernet(keyB).decrypt(Fernet(keyA).decrypt(cipher_text11)),
|
||||
except:
|
||||
print ".",
|
||||
48
unit09_future/src/a_13.py
Normal file
48
unit09_future/src/a_13.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import sys
|
||||
from random import randint
|
||||
|
||||
J = 4
|
||||
I = 5
|
||||
|
||||
e=79
|
||||
d=1019
|
||||
N=3337
|
||||
|
||||
primes = [601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997]
|
||||
val=randint(0,len(primes))
|
||||
p=primes[val]
|
||||
|
||||
U=randint(0,2000)
|
||||
|
||||
C=(U**e) % N
|
||||
|
||||
print 'Bob has',I,'millions'
|
||||
print 'Alice has',J,'millions'
|
||||
|
||||
print '\ne=',e,'d=',d,'N=',N,'p=',p
|
||||
print '\nRandom Value U is:\t',U
|
||||
print 'C value is (U^e %N):\t',C
|
||||
|
||||
val_for_alice = C - J + 1
|
||||
print "Alice shares this value (C-J-1):",val_for_alice
|
||||
|
||||
Z=[]
|
||||
|
||||
for x in range(0,10):
|
||||
val = (((val_for_alice+x)**d) % N) % p
|
||||
if (x>(I-1)):
|
||||
Z.append(val+1)
|
||||
else:
|
||||
Z.append(val)
|
||||
|
||||
G = U % p
|
||||
|
||||
|
||||
print "\nG value is",G
|
||||
print "Z values are:",
|
||||
for x in range(0,10):
|
||||
print Z[x],
|
||||
|
||||
print '\n\nAlice checks U(',U,') against the ',J,'th value (',Z[J-1],')'
|
||||
if (G==Z[J-1]): print "\nSame. Bob has more money or the same"
|
||||
else: print "\nDiffer. Alice has more money"
|
||||
Reference in New Issue
Block a user