Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: utf-8
- from Crypto.Cipher import AES
- from Crypto.Hash import SHA
- from Crypto.PublicKey import RSA
- import commands
- import random
- KEYLEN = 16
- def gen_16_chars_key():
- outfirst = commands.getoutput("date +%N")
- outfirst += commands.getoutput("date +%N")
- return "".join(outfirst[:KEYLEN])
- def blockize(plain):
- """
- Pad the plaintext with random number
- return paddedtext and number of added chars
- """
- cntr = 0
- while len(plain) % KEYLEN != 0:
- cntr += 1
- plain += str(random.randrange(10))
- return (plain, cntr)
- def encrypt(plain, enkey):
- obj = AES.new(enkey, AES.MODE_ECB)
- # because data must has len = k * keysize
- padded_plain, padded = blockize(plain)
- obj.encrypt(padded_plain)
- return (obj.encrypt(padded_plain), padded)
- def decrypt(cypher, enkey, padded):
- obj = AES.new(enkey, AES.MODE_ECB)
- padded_plain = obj.decrypt(cypher)
- plain = "".join(padded_plain[:-padded])
- return plain
- def main():
- key = gen_16_chars_key()
- #plain = "hvnsweeting"
- plain = """
- This fixes the problem with randint() which includes the
- endpoint; in Python this is usually not what you want.
- Do not supply the 'int', 'default', and 'maxwidth' arguments."""
- #print len(plain)
- cypher, padded = encrypt(plain, key)
- # base64 the cypher
- # RSA the key
- private = RSA.generate(1024)
- public = private.publickey()
- pkey = private.exportKey()
- Pkey = public.exportKey()
- #print pkey
- #print Pkey
- print decrypt(cypher, key, padded)
- print "Before trans" , key
- encrypted_key = public.encrypt(key, None)
- #print encrypted_key
- print "transfering over internet..."
- print "After trans", private.decrypt(encrypted_key)
- """
- secret_key = os.urandom(16)
- # Padding (see explanations below)
- plaintext_length = (Crypto.Util.number.size(rsa.n) - 2) / 8
- padding = '\xff' + os.urandom(16)
- padding += '\0' * (plaintext_length - len(padding) - len(secret_key))
- """
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment