Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to encrypt a n image using java

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

public class Encryption {
	public static void main(String[] args)
		throws FileNotFoundException, IOException
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("Note : Encryption Key act as Password to
		Decrypt the same Image,otherwise it will corrupt the Image.");
	
		// Here key is act as password to Encrypt and
		// Decrypt the Image
		System.out.print("Enter key for Encryption : ");
		int key = sc.nextInt();
							
		// Selecting a Image for operation
		FileInputStream fis = new FileInputStream(
			"C:UserslenovoPictureslogo4.png");
							
		// Converting Image into byte array, create a
		// array of same size as Image size
							
		byte data[] = new byte[fis.available()];
							
		// Read the array
		fis.read(data);
		int i = 0;
							
		// Performing an XOR operation on each value of
		// byte array due to which every value of Image
		// will change.
		for (byte b : data) {
			data[i] = (byte)(b ^ key);
			i++;
		}
							
		// Opening a file for writing purpose
		FileOutputStream fos = new FileOutputStream(
			"C:UserslenovoPictureslogo4.png");
							
		// Writing new byte array value to image which
		// will Encrypt it.
							
		fos.write(data);
							
		// Closing file
		fos.close();
		fis.close();
		System.out.println("Encryption Done...");
	}
}
 
PREVIOUS NEXT
Tagged: #encrypt #image #java
ADD COMMENT
Topic
Name
2+9 =