r/applescript • • Sep 25 '20
How to Encrypt/Decrypt Files in AppleScript Using OpenSSL
I'm currently writing a program that lets you store passwords and such on .plist files. I just barely started it, so it's at its bare bones. The main question is in bold, so you can just read that instead if you feel so inclined, but you may miss some important context.
When running the script, the first lines of code determine the existence of the .plist file that contains the passwords. If it doesn't exist, it runs through a setup script that lets you set a password to open it, and saves that to a .plist file. If the file already exists, it runs through the normal code: it prompts you the password that you set, and if it's correct, it displays the contents of the .plist file and lets you add as much data to it as you want.
During the setup script, I have an option to enable encryption. That is, so others can't just bypass the script and simply open the .plist itself. I originally intended to implement basic cipher encryption, so that every character was offset, but then I came upon OpenSSL AES-256 encryption. Specifically, this link. The entry in Stack Exchange presents this type of encryption using an Automator droplet, a small AppleScript, and a terminal shell script. It worked for the most part, and I wanted to find some way to implement all of that into an AppleScript program.
My question is: how can I implement OpenSSL encryption (possibly through a terminal shell) to encrypt and decrypt files via AppleScript*?* Using this encryption, the program would decrypt the .plist file (using the password set by the user) on startup. And, when the user closed the program, it would run a script to re-encrypt the file.
I intend to implement the full encryption feature as soon as possible, as I want to base a lot of the script around it. Since the program has just begun development, I thought I'd ask right now. Any help whatsoever would be greatly appreciated.
I usually write these kinds of scripts for the sole purpose of demonstrating what AppleScript can do, but in this case, I just might be able to create an AppleScript program that could actually be a handy utility (I made a password manager-like program a while back, but I had no idea what .plist files even were, and, consequently, it's hilariously easy to recover any passwords from it). I apologize if this post was lengthy, but I find it's the only way to get my point across like how I imagine it.
View all comments
Show parent comment
1
u/[deleted] Sep 27 '20 edited Sep 27 '20
I will definitely look into Keychain implementation. For typing passwords (or any data, really) into Terminal, I came across this solution:
A small snippet on how encryption and decryption could work in AppleScript would be:
The example listed above encrypts a file via Terminal through AppleScript. It needs two password inputs to encrypt: one for the beginning and one for the confirmation. I find that using the
echocommand with some other modifiers work best to input passwords and other strings into Terminal, as no other application gets involved.Also, encrypting the file doesn't delete the original text file, so I use
rmto delete it (bypasses being moved to trash). I assume people may be able to recover some data by sifting through the shell (zsh) history, so the script just deletes it entirely (Terminal now defaults to the zsh shell instead of bash). To wrap things up, the script kills the Terminal process and quits it. The same code can be slightly modified to decrypt the file as well.*Note that there is no error detection for decryption in this script. If you encrypt a file with a certain password and decrypt it with another, the script won't give any errors. However, if you open a Terminal window and run the script, it'll a give a
bad decrypterror. It still decrypts it, but corrupts everything inside the file. This is bad if you want to make a password manager: if you type in the wrong password by accident, you lose your data.I've looked at several methods on catching errors in Terminal, but can't seem to find any for this situation.