0

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());
   }
Chada
  • 3
  • 4
  • Of course it can't be read - that is the purpose of encryption. In a correctly encrypted files not a single byte is usable without preceding decryption. Therefore an encrypted BMP file has no BMP header and no BMP data. – Robert May 11 '19 at 15:25
  • This is the problem i went it to be readable.i tried to encrypt every pixel but it didn't work – Chada May 11 '19 at 16:21
  • 3
    This does not make any sense. A file is encrypted or readable but never both at the same time. Edit your question describe exactly what you want to achieve and why you want to to it this way. – Robert May 11 '19 at 16:59
  • Tnx for your note .I edit my question – Chada May 12 '19 at 11:17
  • Seems you are doing a lot of assumptions which may be wrong leading you to a wrong approach. What are you trying to achieve? Calculate the noise ration? – gusto2 May 13 '19 at 06:53
  • I'm trying to encrypt each pixel of the original image to get a new encrypted image. Then calculate the(PSNR)peak signal-to-noise ratio between theme – Chada May 13 '19 at 09:56

1 Answers1

0

Seems you are making some wrong assumptions and you may want to reconsider / rethink your approach.

to get a new encrypted image. Then calculate the(PSNR)peak signal-to-noise ratio between theme

BMP image format consists of more than just pixels. As already commented, there are header metadata (size, depth, ,...) which, if you encrypt the whole file, won't make sense anymore (it won't be any bmp) . I'd say you can create a new image and process a pixel by pixel on the image level, not just encrypting an input file. Example: https://www.dyclassroom.com/image-processing-project/how-to-get-and-set-pixel-value-in-java

int p = img.getRGB(x,y);

So I researched other options and found ECIES.

ECIES is a hybrid scheme, you may have a look at following answer for ECIES encryption https://gist.github.com/amrishodiq/9821413 . The output will need to contain more/other information than just pixels and it is definitely not any BMP you can just open and compare.

Hybrid encryption scheme assumes that data are encrypted by a symmetric cipher (I am not sure which is used by BC in this case, if someone knows, please comment), where the encryption key is derived using ECC.

So at the end you'd be comparing pixels encrypted by an underlying symmetric algorithm (e. g. aes256-cbc), where it is designed to provide output indistinguishable from random output.

have to encrypt a bmp image with ECC

If you would encrypt each pixel (lets assume you will represent every pixel as 16 bit rgb), the ECC output is much longer than the input data, so it's not easy to direcly map input values into output of the same dimension (size)

gusto2
  • 11,210
  • 2
  • 17
  • 36
  • I encrypted image with AES and RC4 without any problem, i used img.getRGB( x,y)to encrypt each pixel and it work and create the encryption image. But for ECC if i encrypt each pixel i will get a longer output (as you said) and can't create the encryption image.so is there anything that i can do?any function in BC that can help? – Chada May 13 '19 at 17:18
  • @Chada you are principally asking for wrong thing (no library may help you). ECC is intended to encrypt/exchange an encryption key for encrypting data, not data itself (try to learn more about hybrid encryption, the same would be valid for RSA). In theory you can make up some arbitrary mapping, but that may not be the purpose. – gusto2 May 13 '19 at 18:36
  • i'm lost here.i've been screeching for ecc and found some document about encrypting image with ecc,for example (IMAGE ENCRYPTION AND DECRYPTION USING ELLIPTIC CURVE CRYPTOGRAPHY in International Journal of Advance Research In Science And Engineering)so can u give me some advice or some document that can help me.i really need to encrypt the image with ecc. – Chada May 13 '19 at 19:31
  • 1
    @Chada I found the [paper](https://pdfs.semanticscholar.org/7718/4d77cca9d5e32d9be1c45d50e4170f89ddf8.pdf) and if you want to repeat the process, I suggest to implement the algorithm by yourself (mapping 32x32 blocks). This option may not be implemented out of box in BC (or elsewhere), as it would be slow and reading the paper I see the authors are neglecting *a lot* of things leading to a cryptographically not secure result. – gusto2 May 14 '19 at 11:11
  • the problem is i didn't understand the mapping processe at all.how to mapp the pixels in EC??if u have some papers about it? or if u can explain it i will thankful – Chada May 14 '19 at 15:44