Automatically typing a password into terminal
You did not mention what tool you are using to effect the change of passphrase, but from a shell command line, hdiutil probably makes the most sense.

Its chpass subcommand will let you change the passphrase of an encrypted disk image. It can be told to read the old and new passphrases from stdin. However, it requires that the passphrases be presented in a “NUL terminated” format, which is difficult to accomplish when typing them directly. You can use the printf command to create the required format though:

printf '%s\0' '01d Pa55w0rD' 'n3w p4ssWoRd' | 
  hdiutil chpass /path/to/encrypted.dmg -oldstdinpass -newstdinpass


The passphrases should be given in the same order as the -oldstdinpass and -newstdinpass parameters (in the example above, the -oldstdinpass parameter and the old passphrase itself come first in their respective contexts).



Note: If you are in a multi-user environment there is a chance that another user could spot your passphrases by watching the list of processes and their associated command lines (e.g. the printf command and its argument (thus your passphrases) might show up in the output of ps -ef). This is less likely if you are using a shell where printf is a built into the shell itself (e.g. ksh, bash, zsh do not require using a separate process for the external printf command).