The Prophet taught this dua for entering the toilet because impure places are gathering points for evil spirits. By seeking Allah's protection before entering you place yourself under His shield in a vulnerable moment.