Skip to content

Instantly share code, notes, and snippets.

@n0nuser
Created August 27, 2021 08:13
Show Gist options
  • Save n0nuser/f7061d9cfb8545e2d5c0835d49f416d7 to your computer and use it in GitHub Desktop.
Save n0nuser/f7061d9cfb8545e2d5c0835d49f416d7 to your computer and use it in GitHub Desktop.
Encryption with Python
from Crypto.Cipher import AES # Use cryptodome!!
import base64
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS).encode()
unpad = lambda s: s[:-ord(s[len(s)-1:])]
def iv():
return chr(0) * 16
class AESCipher(object):
def __init__(self, key):
if type(key) is not bytes:
self.key = key.encode()
else:
self.key = key
def encrypt(self, message):
if type(message) is not bytes:
raw = pad(message.encode())
else:
raw = pad(message)
cipher = AES.new(self.key, AES.MODE_CBC, iv().encode())
enc = cipher.encrypt(raw)
return base64.b64encode(enc).decode()
def decrypt(self, enc):
if type(enc) is not int and enc != "":
enc = base64.b64decode(enc)
cipher = AES.new(self.key, AES.MODE_CBC, iv().encode())
dec = cipher.decrypt(enc)
nopad = unpad(dec)
try:
return nopad.decode()
except UnicodeDecodeError:
return nopad
else:
return enc
# message = "Hi!!"
# encrypted_data = AESCipher(password).encrypt(message)
# message = AESCipher(password).decrypt(encrypted_data)
# Use cryptodome!!
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
# Encrypt
public_key = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2b25L4Znkz1mJrg5uLvn
0CogBkBfzGIBPvs2hXmhqoLfrl2x+e33/IGHVg4epn6GMBKG8heXVWnR+IU/iY9q
hwk0C5gn1z6bH9hAJM0GchiACK7tSgmrdsdVIyfKfluRn05M8mLZD4NgzOwMvV49
IlK1Oz9CRjQMDdELlLC8Qg7bouh/ILW3PhV87XdlOhxkX5oX6084Cik917QnCyvs
WQleZY1oR7UaO9EJkAMLFDQYKmPMGipPiJKs3FzeV5rOwy0KnTn0D46lZTZIoZXW
9CWTzMbQdPI1FvYDqwGys2HvinqN4GIK/XCmfDzWAjFonfz0Ak/s7wz2RLhx0oBE
+wIDAQAB
-----END PUBLIC KEY-----"""
## You can read it from a file
pub = RSA.importKey(public_key)
pub_cipher = PKCS1_OAEP.new(pub)
data = ("Hi!!")
enc_data = pub_cipher.encrypt(data.encode())
# s.send(enc_data)
# Decrypt
private_key = """-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA2b25L4Znkz1mJrg5uLvn0CogBkBfzGIBPvs2hXmhqoLfrl2x
+e33/IGHVg4epn6GMBKG8heXVWnR+IU/iY9qhwk0C5gn1z6bH9hAJM0GchiACK7t
SgmrdsdVIyfKfluRn05M8mLZD4NgzOwMvV49IlK1Oz9CRjQMDdELlLC8Qg7bouh/
ILW3PhV87XdlOhxkX5oX6084Cik917QnCyvsWQleZY1oR7UaO9EJkAMLFDQYKmPM
GipPiJKs3FzeV5rOwy0KnTn0D46lZTZIoZXW9CWTzMbQdPI1FvYDqwGys2HvinqN
4GIK/XCmfDzWAjFonfz0Ak/s7wz2RLhx0oBE+wIDAQABAoIBADxpViLocH46j8f4
XxNxFvz7Yw04+RtUqbHWsybPO1p6myLeng2Qx7gIHAt1OW3KNuLJkSVEBFnPVVFV
bu50mwLCO+1nls4ycYmhTxKmy74GLCe8mNts2WTII0fwt07diwafC9xILp8OPWI9
r78ukVmfhubcQ0AFGr8Om97yNhlBUK7pQuN0NGi2C2WBIm681VPINRpqHkzns8lh
DJ4oQzzZlyObosBilQUCiXW5E7+Frl5bgE1F4Z2LYNSWHUXtpukEXxIf9aFD/8ez
eJUZc9luLFRABL2Y6aSiEiSsIxKdGIdeMJbBHFKGXo7sK2lN9AurkxrQLAlEPVMH
pXVRz4ECgYEA2vUDSie7gUg4yDkbP3QbiR2QZcXMdsucn6qsJcN1lbKmNC6xhOWa
ZKmMGAYPPSZPVTPDT8ld1V3VANpu/OaYR5p7IUhGFsC6gHc1w3mVu+xO2nfYhPcA
uASf3gG5pviqldcie3yxos2Ko0Yi1dPdRPEEhjUQdvJgnvQXlRcgUTsCgYEA/pQM
Av7ueINvXb6PT9RYn+RR6LyIs0Ny4rnPcwlF8IhjIwuVP5Y8B1zNndz7tdFDyGlU
vrOhnz7KJASlsvrYXQZhWYVl9v/ECYIDDOYU5q+s2ywDGpnIVP6vpqhOV77wBEa2
ueOaD/kCK2crxyPSDsmgcBO86/ar8EQu0JUIn0ECgYBeMSLOJtOJ5SjQMYjgPjGO
RdKVhYbU3/ir0z8lK7WMxYKsVh313tQRU8K5nvm1Q6LDM3Zk/2pHXItCR7WRRB3k
R7eKdM36AcT5eM7/HXHlmt29K+tQH/YGM68WUUWtZ7Hyrc7esqC0OZnXbVeVBwRc
IRbiKTfBA7m/B6CReZZkewKBgQCUS2kz1YzUZrvYv8h7mBaIDVgpWqnGwuOJbvOI
7z3uf4RARd8Ky1jscQLVkoSNxPjXJYRYEgtz1/+oO4zKVZTzm2PZ4fsjhpmLcFCW
kvBZ8LugcGLee/ZWjIWFb0Uzl+X4l7iZUBvOch7xaYf22MbkFk9++gBy8530t8MN
fcMoAQKBgBvmCQOFKCkAPPHDVvJgtD+tuzNYvs5WZSEQDnrv/Puklfd1KrwKa7ql
p6WXkJ7MArtRf9soisfW+xUatjk8poVI4odHGjezANvEKzhuG1j8edYtDrP+rDXt
7xZHjzg9IAHANevc1/uTJ731kK+kdOH+8iTx4gfwjG6bJ78KfwHe
-----END RSA PRIVATE KEY-----"""
## You can read it from a file
pem = RSA.importKey(private_key)
pem_cipher = PKCS1_OAEP.new(pem)
# enc_data = s.recv(4096)
dec_data = pem_cipher.decrypt(enc_data).decode()
print(dec_data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment