<!---
Test settings / Change these
--->
<!---
ENCRYPT
--->
<!---
DECRYPT (must use same settings used to ENCRYPT)
--->
Test settings / Change these
--->
<cfset inputImagePath = ExpandPath("./logo.gif")>
<!--- should be GIF, JPG, etc... --->
<cfset imageType = listLast(inputImagePath, ".")>
<cfset encryptedImagePath = ExpandPath("./encryptedLogo.gif")>
<cfset decryptedImagePath = ExpandPath("./decryptedLogo.gif")>
<!--- note, "salt" should be varied for better security --->
<cfset salt = BinaryDecode("28e9ac7b748194b0", "hex")>
<cfset iterations = 20>
<cfset password = "my password">
<!---
ENCRYPT
--->
<
<!--- read in the unencrypted image --->
<cfset unencryptedImage = ImageGetBufferedImage( ImageRead( inputImagePath ) )>
<!--- get SecretKeyFactory for key generation --->
<cfset passwordAlgorithm = "PBEWithMD5AndDES">
<cfset keyFactory = createObject("java", "javax.crypto.SecretKeyFactory").getInstance(passwordAlgorithm)>
<!--- use SecretKeyFactory to create secret key using given password --->
<cfset keySpec = createObject("java", "javax.crypto.spec.PBEKeySpec").init( password.toCharArray() )>
<cfset secretKey = keyFactory.generateSecret( keySpec )>
<!--- set up other parameters for password-based encryption ---->
<cfset keyParams = createObject("java", "javax.crypto.spec.PBEParameterSpec").init(salt, iterations)>
<!--- Create cipher and initialize it to ENCRYPT using the given password --->
<cfset encyrptAlgorithm = "PBEWithMD5AndDES" >
<cfset cipher = createObject("java", "javax.crypto.Cipher").getInstance(encyrptAlgorithm)>
<cfset cipher.init(Cipher.ENCRYPT_MODE, secretKey, keyParams)>
<!--- Create a regular output stream to store the encrypted image on disk --->
<cfset outStream = createObject("java", "java.io.FileOutputStream").init( encryptedImagePath )>
<!--- Create a cipher output stream to encrypt the output --->
<cfset cipherOutStream = createObject("java", "javax.crypto.CipherOutputStream").init( outStream, cipher )>
<!--- Write the encrypted image to disk --->
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<cfset ImageIO.write( unencryptedImage, imageType, cipherOutStream )>
<!--- Finish. Close both streams --->
<cfset cipherOutStream.close()>
<cfset outStream.close()>
<!---
DECRYPT (must use same settings used to ENCRYPT)
--->
<!--- use SecretKeyFactory to get secret key --->
<cfset passwordAlgorithm = "PBEWithMD5AndDES">
<cfset keyFactory = createObject("java", "javax.crypto.SecretKeyFactory").getInstance(passwordAlgorithm)>
<cfset keySpec = createObject("java", "javax.crypto.spec.PBEKeySpec").init( password.toCharArray() )>
<cfset secretKey = keyFactory.generateSecret( keySpec )>
<!--- set up other parameters for password-based encryption ---->
<!--- "salt" should be varied for better security --->
<cfset PBEParameterSpec = createObject("java", "javax.crypto.spec.PBEParameterSpec")>
<cfset keyParams = createObject("java", "javax.crypto.spec.PBEParameterSpec").init(salt, iterations)>
<!--- Initialize cipher for DECRYPT operation --->
<cfset encyrptAlgorithm = "PBEWithMD5AndDES" >
<cfset cipher = createObject("java", "javax.crypto.Cipher").getInstance(encyrptAlgorithm)>
<cfset cipher.init( cipher.DECRYPT_MODE, secretKey, keyParams)>
<!--- read the encrypted image from disk into a cipher stream for decrypting --->
<cfset inStream = createObject("java", "java.io.FileInputStream").init( encryptedImagePath )>
<cfset CipherInputStream = createObject("java", "javax.crypto.CipherInputStream")>
<cfset cipherInStream = CipherInputStream.init(inStream, cipher)>
<!--- extract the decrypted image --->
<cfset ImageIO = createObject("java", "javax.imageio.ImageIO")>
<cfset imageBuffered = ImageIO.read( cipherInStream )>
<cfset cipherInStream.close()>
<!--- Convert the decrypted image to a CF compatible image --->
<cfset CFCompatibleImage = ImageNew(imageBuffered)>
<!--- **AND/OR** save the unecrypted image back to disk --->
<cfset outputStream = createObject("java", "java.io.FileOutputStream").init( decryptedImagePath )>
<cfset ImageIO.write(imageBuffered, imageType, outputStream)>
<cfset outputStream.close()>
<b>Original Image</b><br>
<cfimage action="writeToBrowser" source="#inputImagePath#">
<br><b>Encrypted Image</b><br>
<cftry>
<cfimage action="writeToBrowser" source="#encryptedImagePath#">
You should NOT see an image above
<cfcatch>
Success! Cannot render encrypted images
</cfcatch>
</cftry>
<br><b>Decrypted Image:</b><br>
<cfimage action="writeToBrowser" source="#decryptedImagePath#">