Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Encrypt and decrypt a file

Started by jj2007, July 11, 2020, 09:16:09 AM

Previous topic - Next topic


To encrypt a file, drag it over the attached executable; filename.enc will be generated
To decrypt a file, drag filename.enc over the executable.
The password should be around 12 valid characters long. Spaces, tabs etc get ignored. A-Z, a-z, 0-9, /#*$&:+-?\ are valid password elements.

Is it safe? Use at your own risk, and never forget the password - I won't be able to help you :cool:

The password generates two seeds for use in two independent and pretty good PRNGs. The password also generates two offsets that are used to insert several hundred random bytes at the start and the end of the file. An attacker would have to...
- guess a dword value from the original (which is not impossible, e.g. PKxx for zip files)
- find two independent PRNG series with a period of almost 4GB that produce this dword
- do that several hundred times at the start of the *.enc file, because the position of the PKxx is not known.

IMHO it is pretty safe, but again: use at your own risk. Building requires MasmBasic 11 July 2020 :cool:

Note that the *.enc file cannot be compressed. Below a screenshot from an attempt to zip the *.enc file. While the original file compresses quite well (almost a factor 5), the encrypted version is virtually uncompressible. It is therefore recommended to zip large files first, then encrypt them.

include \masm32\MasmBasic\
  Let esi=Qtrim$(CL$())                 ; get the file from the commandline
  .if Exist(esi)
        Let edi=Input$("Password: ", "Assembly is fun!")        ; 12 chars will be used (blanks and ! ignored)
        .if dword ptr [esi+Len(esi)-4]==Mirror$(".enc")         ; encrypted file
                Decrypt esi, edi        ; decrypt...
                ShEx eax                ; ... and show
                Encrypt esi, edi
                PrintLine "Encoded as ", eax
        PrintLine "No commandline found: ", esi

P.S.: It's Assembly, i.e. fast - a 20MB file encrypts and decrypts in about 150 milliseconds on my trusty old Core i5 :tongue:


Update: With MasmBasic of 11 July 20 V2, Encrypt and Decrypt do not change the file's time stamp, unless you specify MbCryptTS=0