Commit e95d6da5 by Matteo Di Fraia

aggiunte risorse kerlink

1 parent 52afd595
The file could not be displayed because it is too large.
The file could not be displayed because it is too large.
No preview for this file type
No preview for this file type
#!/usr/bin/python3
import argparse
import os
import hashlib
from subprocess import call
from Crypto.Cipher import AES
from Crypto import Random
from binascii import hexlify
DEFAULT_SALT = "__UPLOAD__"
DEFAULT_ITERATION = 8192
DEFAULT_KEY_LEN = 32
def cipherfile(file,key):
size = os.path.getsize(file)
hash = hashlib.sha256(open(file, 'rb').read()).hexdigest()
data = size.to_bytes(4, byteorder='big') + open(file, 'rb').read() + bytes.fromhex(hash)
iv_raw = Random.new().read(16)
iv = int.from_bytes(iv_raw, byteorder='big')
iv_str = format(iv, 'x')
with open(file + ".tmp", 'wb') as f:
f.write(data)
f.close()
call(["openssl", "aes-256-cbc", "-md", "sha256", "-e", "-iv", iv_str, "-K", hexlify(key), "-in", file + ".tmp", "-out", file + ".enc"])
data = iv_raw + open(file + ".enc", 'rb').read()
with open(file + ".enc", 'wb') as f:
f.write(data)
f.close()
try:
os.remove(file + ".tmp")
except Exception:
pass
def pnrcipher(file,pnrapilevel,password, salt=DEFAULT_SALT, iterations=DEFAULT_ITERATION, keylen=DEFAULT_KEY_LEN):
if pnrapilevel == 0 :
call(["openssl", "aes-256-cbc", "-md", "sha256" , "-e", "-in", file, "-out", file + ".enc", "-k", password])
elif pnrapilevel == 1:
pbkdf2(file, password, salt, iterations, keylen)
else :
print("Error : Unknown Proven Run Api Level : " + args.pnrapilevel)
return 1
return 0
def pbkdf2(file, password, salt, iterations, keylen):
dk = hashlib.pbkdf2_hmac('sha256', str.encode(password), str.encode(salt), iterations, dklen=keylen )
cipherfile(file,dk)
return 0
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--file", dest="file", type=str, required=True, help="File to be ciphered")
parser.add_argument("-p", "--password", dest="password", type=str, required=True, help="Password used")
parser.add_argument("-s", "--salt", dest="salt", type=str, default=DEFAULT_SALT)
parser.add_argument("-i", "--iterations", dest="iterations", type=int, default=DEFAULT_ITERATION)
parser.add_argument("-l", "--len", dest="keylen", type=int, default=DEFAULT_KEY_LEN)
args = parser.parse_args()
pbkdf2(args.file, args.password, args.salt, args.iterations, args.keylen)
return 0
if __name__ == "__main__":
main()
exit(0)
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!