0% found this document useful (0 votes)
3 views2 pages

Python Code Examples for Security

The document provides Python code examples for implementing RSA encryption and a secure login system using bcrypt. The RSA implementation includes functions for generating keys, encrypting, and decrypting messages, while the login system demonstrates user registration and authentication with hashed passwords. Both examples are educational and not intended for production use.

Uploaded by

kenchannn20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Python Code Examples for Security

The document provides Python code examples for implementing RSA encryption and a secure login system using bcrypt. The RSA implementation includes functions for generating keys, encrypting, and decrypting messages, while the login system demonstrates user registration and authentication with hashed passwords. Both examples are educational and not intended for production use.

Uploaded by

kenchannn20
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

University Level Python Code Examples

(Educational & Safe)

1) RSA Implementation (Simple, Not for Production)


import random
from math import gcd

def is_prime(n, k=8):


if n < 2:
return False
small_primes = [2,3,5,7,11,13,17,19,23,29]
for p in small_primes:
if n == p:
return True
if n % p == 0:
return False
d = n - 1
s = 0
while d % 2 == 0:
d //= 2
s += 1
def trial(a):
x = pow(a, d, n)
if x == 1 or x == n-1:
return True
for _ in range(s-1):
x = pow(x, 2, n)
if x == n-1:
return True
return False
for _ in range(k):
a = [Link](2, n-2)
if not trial(a):
return False
return True

def egcd(a, b):


if b == 0:
return 1, 0, a
x, y, g = egcd(b, a % b)
return y, x - (a // b) * y, g

def modinv(a, m):


x, y, g = egcd(a, m)
if g != 1:
raise Exception("modular inverse does not exist")
return x % m

def generate_keys(bits=512):
p = generate_prime(bits//2)
q = generate_prime(bits//2)
while q == p:
q = generate_prime(bits//2)
n = p * q
phi = (p-1)*(q-1)
e = 65537
if gcd(e, phi) != 1:
while True:
e = [Link](3, phi, 2)
if gcd(e, phi) == 1:
break
d = modinv(e, phi)
return (e, n), (d, n)

def encrypt(plaintext: bytes, pubkey):


e, n = pubkey
m = int.from_bytes(plaintext, 'big')
if m >= n:
raise ValueError("Message too long")
return pow(m, e, n)
def decrypt(cipher_int: int, privkey):
d, n = privkey
m = pow(cipher_int, d, n)
length = (m.bit_length() + 7)//8
return m.to_bytes(length, 'big')

2) Secure Login System Using bcrypt


import bcrypt

def register_user(username, password):


pw = [Link]('utf-8')
hashed = [Link](pw, [Link](rounds=12))
return {"username": username, "password_hash": hashed}

def authenticate(user_record, password_attempt):


pw = password_attempt.encode('utf-8')
return [Link](pw, user_record["password_hash"])

You might also like