I have to encrypt a bmp image with ECC, but I found it very difficult especially when the pixels will be mapped on the elliptic curve, I didn't understand it at all. So I researched other options and found ECIES.
I tried to implement it in java using bouncycastle, it works but the problem is that my code encrypt the image as a file ,but i went to encrypt the pixels of the image and get a new encrypted image so I can calculate the PSNR between the encrypted and the unencrypted images. I tried to encrypt every pixel alone than create the encrypting image but didn't work. Is there some function can help me or anything in BC?or do i have to implement ECIES without it?
This is what i tried:
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
ECNamedCurveParameterSpec curveParameterSpec = ECNamedCurveTable.getParameterSpec("secp384r1");
keyPairGenerator.initialize(curveParameterSpec, new SecureRandom());
KeyPair KeyPair = keyPairGenerator.generateKeyPair();
ECPublicKey publicKey = (ECPublicKey) KeyPair.getPublic();
ECPrivateKey privateKey = (ECPrivateKey) KeyPair.getPrivate();
javax.crypto.Cipher c1 = javax.crypto.Cipher.getInstance("ECIES", "BC");
javax.crypto.Cipher c2 = javax.crypto.Cipher.getInstance("ECIES", "BC");
c1.init(ENCRYPT_MODE, publicKey, new SecureRandom());
c2.init(DECRYPT_MODE, privateKey, new SecureRandom());
try{
File bmpFile = new File("C:\\Users\\acer\\Desktop\\py\\6.bmp");
BufferedImage image = ImageIO.read(bmpFile);
// to byte
ByteArrayOutputStream baos=new ByteArrayOutputStream();//length=32 bytes, though its size increases if necessary.
ImageIO.write(image, "bmp", baos );
byte[] b = baos.toByteArray();
byte[] cipherimage = c1.doFinal(b, 0, b.length);
byte[] plainimage = c2.doFinal(cipherimage, 0, cipherimage.length);
bmpFile=new File("C:\\Users\\acer\\Desktop\\py\\encryptedimage.bmp");
FileOutputStream fos = new FileOutputStream(bmpFile);
fos.write(cipherimage);
fos.flush();
fos.close();
bmpFile=new File("C:\\Users\\acer\\Desktop\\py\\decryptedimage.bmp");
FileOutputStream fos1 = new FileOutputStream(bmpFile);
fos1.write(plainimage);
fos1.flush();
fos1.close();
} catch (IOException e){
System.out.println(e.getMessage());
}