Added better encryption handling.

Change-Id: Idd8b6e19edb5fd1cf1870a11e20e8def80b3a501