The trick is that the encrypted JPEG data is not PNG data. The file is usually crafted in such a way that the container formats are tolerant of the junk data that becomes the second file after encryption. The second file's format is also tolerant of the junk data that used to be a JPEG. Some parts may actually be used on both sides of the transformation, but not most of the bits.