Professional Documents
Culture Documents
0.lab 7-1 RSA
0.lab 7-1 RSA
1) Dựa trên file tài liê ̣u Implementation_Tutorial_on_RSA.pdf [1] và Chapter 9 - Public Key
Cryptography and RSA.pdf của sách tham khảo Cryptography and Network Security Principles and
Practice (2014).pdf [2], thực hiê ̣n mã hóa RSA (Encryption) bằng tay (trình bày bằng MS Word) để tìm
Ciphertext, bằng cách thực hiê ̣n các bước sau: (4đ)
- Key Generation: Generate the public and private key
Chú ý: Tham khảo thêm website tài liê ̣u [3], để hiểu rõ thêm công thức toán trong quá trình tạo key.
- RSA Function Evaluation: Process of transforming plaintext into ciphertext
- Plaintext thực hiê ̣n như sau:
Giả sử: Tên SV = Tran Thanh Hung
Mã ASCII: H = 48 tương ứng mã Hexa 8 bits = 0100 1000 và số Decimal:
72 Ta tạo Plaintext (số nguyên 8 bits) từ 1 chữ cái đầu của First Name, ví dụ:
Plaintext = T = 72
Chú ý: SV phải lấy đúng Tên SV của mình thì mới có điểm.
a) Key Generation
- Bước 3: Sử dụng thuật toán GCD để tìm d sao cho e * d = 1 ±120, ta tìm được d= 13(e * d
=481)
PU = 37,143
2) Sau đó, dựa trên kết quả Ciphertext của câu 1), thực hiê ̣n giải mã RSA (Decryption) bằng tay (và trình
bày bằng MS Word) để tìm ngược lại Plaintext (xem có đúng như ban đầu ở câu 1), bằng cách thực hiê ̣n
các bước sau: (4đ)
- RSA Function Evaluation: Process of transforming ciphertext into plaintext
- Ta dùng khóa riêng tư ( Private key –PR) đã tìm ở trên để mã hóa Ciphertext.
- Tương tự câu 1 , ta tạo p, q, tính n và phiN(φ) , e :
- P = 11
- Q =13
PR = 13,143
3) Dựa vào chương trình mẫu Test_RSA.py [4] trong file tài liê ̣u [1], viết lại chương Python thỏa mãn
các điều kiê ̣n sau: (2 điểm)
- Mô ̣t chương trình mã hóa (encryption) và 1 chương trình giải mã (decryption) RSA cipher
- Có thể nhâ ̣p vào (input) số nguyên cần mã hóa (plaintext) và các thông số p, q, e, d phải thích
hợp (ví dụ: p, q: số nguyên tố khác biê ̣t …) và đưa ra (output) ra các dữ liê ̣u tương ứng:
ciphertext (chương trình encryption) và plaintext (chương trình decryption)
Chú ý: Để minh họa 2 chương trình này, SV cần nhâ ̣p vào chữ cái đầu Tên SV của mình làm
plaintext (tham khảo câu 1).
Thí dụ, Plaintext = T
Chú ý: lưu lại hình ảnh minh họa kết quả chương trình chạy làm minh chứng.
Encryption:
def check_prime(n):
if n < 2:
return False
else:
if n == 2:
return True
else:
for i in range(2, n-1, 1):
if n % i == 0:
return False
return True
def input_prime(message):
while True:
try:
pri_num = input(message)
pri_num = int(pri_num)
if (check_prime(pri_num) == True):
return pri_num
else:
print("Invalid prime !")
continue
except:
print("Invalid prime!")
continue
def gcd(a,b):
if (b==0):
return a
return gcd(b,a%b)
def findInverse(a,p):
for i in range(0,p):
if(a * i % p == 1):
return i
return False
def encrypt(m):
c = m ** e % n
print ("Encrypted Message: ", c)
return c
Decryption:
def check_prime(n):
if n < 2:
return False
else:
if n == 2:
return True
else:
for i in range(2, n-1, 1):
if n % i == 0:
return False
return True
def input_prime(message):
while True:
try:
pri_num = input(message)
pri_num = int(pri_num)
if (check_prime(pri_num) == True):
return pri_num
else:
print("Invalid prime !")
continue
except:
print("Invalid prime!")
continue
def gcd(a,b):
if (b==0):
return a
return gcd(b,a%b)
def findInverse(a,p):
for i in range(0,p):
if(a * i % p == 1):
return i
return False
def decrypt(c):
dmsg = c ** d % n
print ("Decrypted Message :",dmsg,"--> character is",chr(dmsg))
return dmsg
while True:
try:
c = input("Enter your ciphertext:")
c = int(c)
except:
print("Invalid number!")
continue
break
p = input_prime("Input your prime p:")
q = input_prime("Input your prime q:")
n=p*q
phiN= (p-1)*(q-1)
while True:
e = input_prime("Input your e:")
e = int(e)
if (gcd(e,phiN) == 1):
break
else:
print("Ước số chung lớn nhất không bằng 1 !")
continue
d = findInverse(e,phiN) #d = e^-1 mod(phi)
print("--> d number is",d)
p = decrypt(c)