본문 바로가기
728x90

Sejong University/Public-key cryptography7

ECC from __future__ import print_function from random import randint from sys import argv, stdout import collections import random def mulInv(n, q): return extEuclid(n, q)[0] % q def extEuclid(a, b): s0, s1, t0, t1 = 1, 0, 0, 1 while b > 0: q, r = divmod(a, b) a, b = b, r s0, s1, t0, t1 = s1, s0 - q * s1, t1, t0 - q * t1 pass return s0, t0, a def sqrRoot(n, q): r = pow(n,(q+1)/4,q) return r, q - r P.. 2022. 4. 24.
DH Key Agreement import prime #miller-rabin test import random as r #randint 사용 ''' 현재 보고 있는 키 생성 함수에서 키 길이의 의미를 다음과 같이 이해하였음을 밝힘 1. 키는 p을 기준으로 보고 |p|는 n(prime_length)이 됨 2. 키 길이가 예를 들어 n bit의 경우 MSB의 값을 1로 고정하였을 때 표현할 수 있는 정수의 범위는 2^(n-1) 2022. 4. 24.
Hashed-RSA import hashlib #sha1사용 import prime #miller-rabin test import random as r #randint사용 import modcrt #extended_gcd, square & multiply ''' 현재 보고 있는 키 생성 함수에서 키 길이의 의미를 다음과 같이 이해하였음을 밝힘 1. 키는 n을 기준으로 보고 n=p*q이므로 |p|,|q|는 |n|의 절반이 됨 2. 키 길이가 예를 들어 n bit의 경우 MSB의 값을 1로 고정하였을 때 표현할 수 있는 정수의 범위는 2^(n-1) 2022. 4. 24.
RSA crypto with miller_rabin and non-miller_rabin #include #include #define p_value "C485F491D12EA7E6FEB95794E9FE0A819168AAC9D545C9E2AE0C561622F265FEB965754C875E049B19F3F945F2574D57FA6A2FC0A0B99A2328F107DD16ADA2A7" #define q_value "F9A91C5F20FBBCCC4114FEBABFE9D6806A52AECDF5C9BAC9E72A07B0AE162B4540C62C52DF8A8181ABCC1A9E982DEB84DE500B27E902CD8FDED6B545C067CE4F" typedef struct _b10rsa_st { BIGNUM *e; BIGNUM *d; BIGNUM *n; }BOB10_RSA; BIGNUM *XEucl.. 2022. 4. 23.
Prime with Miller Rabin test import random as r #base 난수 생성을 위한 패키지 def is_prime(n): #소수 판별 함수 a = make_base(n) # 최소 20개의 베이스 생성(단, 예외 존재) exp, odd_value = divide_two(n) #n-1 = 2^s * t에서 s, t값 for i in range(0, len(a)): #miller rabin test 수행 #만약 miller_rabin test를 통과하지 못했다면 if miller_rabin_test(n, a[i], exp, odd_value) != 1: return 0 #0 반환 return 1 #miller_rabin test를 통과한 값이라면 1 반환 ''' 단, "Let n > 1 be an odd integer and de.. 2022. 4. 23.
Modular exponentiation and CRT #유클리드 알고리즘 def gcd(a, b): if b == 0: #만약 b가 0이라면 a가 gcd return a else: #b가 0이 아니라면 정수배 선형결합을 한 값을 주고 재귀 return gcd(b, a%b) #확장 유클리드 알고리즘 def extended_gcd(a, b): r0, r1 = a, b #초기 r0, r1값 초기화 x0, y0 = 1, 0 #초기 x0, y0값 초기화 x1, y1 = 0, 1 #초기 x1, y1값 초기화 gcd_value = gcd(a, b) #gcd값을 추출 while gcd_value != r1: #r1이 gcd에 도달할 때까지 x0, y0, x1, y1 = x1, y1, x0-(r0//r1)*x1, y0-(r0//r1)*y1 #확장 유클리드 알고리즘에 의한 .. 2022. 4. 23.
Extended GCP in public key crypto #유클리드 알고리즘 def gcd(a, b): if b == 0: #만약 b가 0이라면 a가 gcd return a else: #b가 0이 아니라면 정수배 선형결합을 한 값을 주고 재귀 return gcd(b, a%b) #확장 유클리드 알고리즘 def extended_gcd(a, b): r0, r1 = a, b #초기 r0, r1값 초기화 x0, y0 = 1, 0 #초기 x0, y0값 초기화 x1, y1 = 0, 1 #초기 x1, y1값 초기화 gcd_value = gcd(a, b) #gcd값을 추출 while gcd_value != r1: #r1이 gcd에 도달할 때까지 x0, y0, x1, y1 = x1, y1, x0-(r0//r1)*x1, y0-(r0//r1)*y1 #확장 유클리드 알고리즘에 의한 .. 2022. 4. 23.
728x90