Search code examples
opensslexpect

Bash Expect decrypt password


  1. I have passphrase in file named phrase which is saved on local system : OXyqRC694pvKQ1FNfIYZQOKS

  2. Encrypted my password : echo "ITSMYPASSWORD" | openssl enc -aes-128-cbc -a -salt -pass pass:OXyqRC694pvKQ1FNfIYZQOKS

  3. In expect script, I am trying to decrypt the password, before doing scp, but getting error "bad decrypt" while decrypting the password

    #!/usr/bin/expect

    set upassword [lindex $argv 0]

    set oF [open "phrase" r]
    set dpassword [read $oF]

    puts $upassword
    puts $dpassword

    puts "echo $upassword | openssl enc -aes-128-cbc -a -d -salt -pass pass:$dpassword"
    #if i execute ouput of above line in shell, i am able to decrypt the password, 
    # but below exec command doesnot not work.         

    exec echo $upassword | openssl enc -aes-128-cbc -a -d -salt -pass pass:$dpassword

How can i fix this?


Solution

  • In Tcl, the read command will include the newline that ends the file. Try this:

    set dpassword [read -nonewline $oF]
    

    Also, the exec command has a << redirection to send a string to the command's stdin:

    exec openssl enc -aes-128-cbc -a -d -salt -pass pass:$dpassword << $upassword