Skip to content

Instantly share code, notes, and snippets.

@hikenshi
hikenshi / RSA.py
Last active March 5, 2019 05:10
RSA public and private key encrypt decrypt
def kiem_tra_so_nguyen_to_miller_rabin(n):
"""Phương pháp miller - rabin là một phương pháp kiểm tra tính nguyên tố của một số, thực chất là kiểm tra một số không phải là số nguyên tố \
để loại trừ. Cơ sở lý thuyết của phương pháp này như sau: \
1. Định lý nhỏ fermat cho chúng ta biết rằng, nếu n là một số nguyên tố, thì với mọi số a sao cho \
1 <= a < n thì a ^ (n-1) % n = 1.
2. n chắc chắn là số lẻ, vì n lẻ nên n-1 là một số chẵn và có thể biểu đạt dưới dạng: n-1 = d*2^s \
trong đó d là số lẻ và s>0 . \
3. Nhờ hai điều trên, với một số bất kỳ trong khoảng [2, n-2] thì a^(d*2^s) % n phải bằng 1.
4. Một tính chất toán học khác nữa là nếu x^2 % n = 1 thì x^2 -1 % n = 0"""
s = 0
@hikenshi
hikenshi / check_fermat.py
Created February 11, 2019 10:52
Think Python, Excersie 5.2
def check_fermat(a,b,c,n):
if n < 3:
print("please enter n > 2!")
return
if a**n + b**n == c**n:
print("Holy smokes, Fermat was wrong!")
else:
print("No, that doesn't work.")
try:
a = int(input("enter a: \n"))
@hikenshi
hikenshi / draws_a_grid.py
Created February 10, 2019 16:34
Exercise 3.3: Draws a grid. Sách Think Python
def print_arg(s):
print(s, end=' ')
def print_four(f,s):
for i in range(0,4):
f(s)
def so_o_doc(h,f,s):
for i in range(0,int(h)):
print_arg(f)
print_four(print_arg,s)
print_arg(f)
@hikenshi
hikenshi / encrypt_file.py
Last active February 9, 2019 17:50
Script mã hoá và giải mã file bằng AES256
"""
Ma Hoa AES256. Dung cho python 3.5+
Can cai dat pycryptodome. https://pypi.org/project/pycryptodome/
Script nay co su dung code tu: https://eli.thegreenplace.net/2010/06/25/aes-encryption-of-files-in-python-with-pycrypto/
"""
import os, random, struct
import hashlib
from Crypto.Cipher import AES