I need to implement file decryption in php. As a result of my attempts, I get a broken archive
I use the code as a basis https://github.com/samloader/samloader/tree/master
I'm interested in the part
elif args.command == "decrypt":
getkey = crypt.getv4key if args.enc_ver == 4 else crypt.getv2key
key = getkey(args.fw_ver, args.dev_model, args.dev_region)
length = os.stat(args.in_file).st_size
with open(args.in_file, "rb") as inf:
with open(args.out_file, "wb") as outf:
crypt.decrypt_progress(inf, outf, key, length)
and
def decrypt_progress(inf, outf, key, length):
""" Decrypt a stream of data while showing a progress bar. """
cipher = AES.new(key, AES.MODE_ECB)
if length % 16 != 0:
raise Exception("invalid input block size")
chunks = length//4096+1
pbar = tqdm(total=length, unit="B", unit_scale=True)
for i in range(chunks):
block = inf.read(4096)
if not block:
break
decblock = cipher.decrypt(block)
if i == chunks - 1:
outf.write(unpad(decblock))
else:
outf.write(decblock)
pbar.update(4096)
def getv4key(version, model, region):
""" Retrieve the AES key for V4 encryption. """
client = fusclient.FUSClient()
version = versionfetch.normalizevercode(version)
req = request.binaryinform(version, model, region, client.nonce)
resp = client.makereq("NF_DownloadBinaryInform.do", req)
root = ET.fromstring(resp)
fwver = root.find("./FUSBody/Results/LATEST_FW_VERSION/Data").text
logicval = root.find("./FUSBody/Put/LOGIC_VALUE_FACTORY/Data").text
deckey = request.getlogiccheck(fwver, logicval)
return hashlib.md5(deckey.encode()).digest()
I rewrote it, there are doubts about this line
hashlib.md5(deckey.encode()).digest()
I got it like this `$deckey = 'AU77D7K3SAU/D3UU';
$key = hash('md5', $deckey, true);`
This is right?
And the part of the code that is responsible for my decryption
<?php
$source = 'file.zip.enc4';
$dest = 'file.zip';
$source = fopen($source, 'r');
$dest = fopen($dest, 'w');
$chunkSize = 4096;
while (!feof($source)) {
$chunk = fread($source, $chunkSize + 1);
if (!$chunk) {
break;
}
$encryptedChunk = openssl_decrypt($chunk, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
fwrite($dest, $encryptedChunk);
}
fclose($source);
fclose($dest);
Help me figure out what I'm doing wrong
Edit: ...in my code I used openssl_decrypt, there was an error when I wrote here. The problem seems to be with php
$source = 'file.zip.enc4';
$dest = 'file.zip';
$source = fopen($source, 'r');
$dest = fopen($dest, 'w');
$chunkSize = 4096;
while (!feof($source)) {
$chunk = fread($source, $chunkSize + 1);
$encryptedChunk = openssl_decrypt($chunk, 'aes-128-ecb', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
var_dump(openssl_error_string());
fwrite($dest, $encryptedChunk);}
fclose($source);
fclose($dest);
i have errors
string(58) "error:1C80006B:Provider routines::wrong final block length"
openssl works through the console. I'll do it via exec('openssl...', $res);
AU77D7K3SAU/D3UU,deckeyingetv4key()?