1

suppose i wanted to make a bunch of files full of gibberish. if i wanted to one file of gibberish, then encrypt it using ccrypt, i can do this:

$ echo "12 ddsd23" > randomfile.txt, now using ccrypt:

$ ccrypt -e randomfile.txt
Enter encryption key: 
Enter encryption key: (repeat)

as you can see i am prompted for input for the key.

i want to automate this and create a bunch of gibberish files.

script in python to produce random gibberish:

import random as rd
import string as st

alphs = st.ascii_letters
digits = st.digits
word = ""

while len(word) < 1000:
    word += str(rd.choices(alphs))
    word += str(rd.choices(digits))

print(word)

now running this from bash script, saving gibberish to file:

#!/bin/bash

count=1

while [ $count -le 100 ]
do
  python3 /path/r.py > "file$count.txt"
  ccrypt -e "file$count.txt"
  ((count=count+1))
done

problem, as you can see:

$ bash random.sh 
Enter encryption key:

ccrypt does not have an option to provide passphrase as an argument.

Question: is there a way for the bash script to provide the passphrase when shell prompts for it?

i am aware this can be solved just by doing the encryption in python but just curious if something like this can be done with bash.

if it matters: there is an option for ccrypt to ask for just one prompt.

Aiden Choi
  • 142
  • 7

2 Answers2

0

You need to use the yes command in your bash code. Basically this command will provide the inputs for a script (ie. ccrypt) whenever it needs it. Check here for more info.

ARK1375
  • 790
  • 3
  • 19
  • `yes "12312das!32" | ccrypt -e "file$count.txt"` did not work. maybe bcause `ccrypt` is not a bash script? not sure. – Aiden Choi Jun 18 '21 at 07:51
  • Did you use `\n` in your `yes` command to provide pressing the enter key? Also [this](https://stackoverflow.com/questions/9075478/how-to-input-automatically-when-running-a-shell-over-ssh) might help as well – ARK1375 Jun 18 '21 at 08:04
  • yes. `"\n\n"` also does not work. – Aiden Choi Jun 18 '21 at 08:09
  • also as to using `expect`, how would an `expect` script be implemented in my script? i don't mean how i would write the expect script, but how it should be triggered in my script. not one `expect` exmaple i have found explains this. – Aiden Choi Jun 18 '21 at 08:25
0
[Edited]

My original answer suggested to do:

printf "$PASSPHRASE\n$PASSPHRASE\n" | ccrypt -e "file$count.txt"

which is the generic solution that should work with many tools that expect some input passed to their STDIN; but it doesn't seem to work with ccrypt for whatever reason.

However, ccrypt also has options for providing the passphrase in different (non-interactive) ways:

$ ccrypt --help
    ...
    -K, --key key         give keyword on command line (unsafe)
    -k, --keyfile file    read keyword(s) as first line(s) from file
    ...

Here's an example using -K. Note that it is "unsafe" because if you execute this command in your interactive shell, or run your script with -x (to print each executed command), the passphrase may end up in ~/.bash_history or in some logs, respectively, so dump the passphrase to a file and use -k in case that's important.

#!/bin/bash

# read the passphrase, do not display it to screen
read -p "Please provide a passphrase:" -s PASSPHRASE

count=1

while [ $count -le 100 ]
do
  python script.py > "file$count.txt"
  ccrypt -e "file$count.txt" -K "$PASSPHRASE"
  ((count=count+1))
done
Czaporka
  • 2,190
  • 3
  • 10
  • 23
  • did not make difference, `printf` does not get executed. – Aiden Choi Jun 18 '21 at 07:47
  • @raincouver OK, I don't know why that didn't work (it usually does), but I've edited an alternative solution into the answer. In fact, this alternative solution should have been the first choice as these command line options are designed specifically for using the tool within scripts. Many other cryptographic tools (such as `openssl`, `keytool`) also have similar options, so you don't need hacky ways of providing passphrases as user input. – Czaporka Jun 18 '21 at 08:36
  • right, problem is solved – Aiden Choi Jun 18 '21 at 21:36