Is there a library available in java to encrypt a string? Is there any library to encrypt and decrypt an image using AES in Java?
            Asked
            
        
        
            Active
            
        
            Viewed 1,552 times
        
    0
            
            
        - 
                    1I haven't seen any libraries that let you encrypt strings - only byte arrays. – user253751 Jan 06 '15 at 06:40
- 
                    Okay... Any library which encrypts an image? – Prashanth Jan 06 '15 at 06:42
- 
                    Any library that can encrypt byte arrays can encrypt images. Or songs. Or strings. Or anything you want. – user253751 Jan 06 '15 at 06:43
- 
                    Have you read this [question](http://stackoverflow.com/q/992019/2970947)? – Elliott Frisch Jan 06 '15 at 06:43
1 Answers
1
            
            
        You can encrypt an image by piping it out to ByteArrayOutputStream, using Cipher.doFinal on the result bytes and writing that to an output source.
Example:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
SecretKeySpec keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance( ... );
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(baos.toByteArray());
 
    
    
        David Xu
        
- 5,555
- 3
- 28
- 50
 
    