使用AES加密和解密图像的正确方法 [英] Correct way of Encrypting and Decrypting an Image using AES

查看:315
本文介绍了使用AES加密和解密图像的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT :::问题中的代码正常工作,但一旦摄像机拍摄图像,则需要大约10秒才能恢复活动。我放弃了这种方法,并利用Facebook的隐形图书馆加密和解密图像。链接到Facebook的解决方案: Facebook隐藏 - 图像加密和解密






我已经看了很多例子,但仍然无法找出一种获得加密和解密权的方法。我以为我在互联网上使用了一些随机代码时,它是正确的,但在解码时,我得到一个BadPadding异常。



所以,我正在努力工作。我正在按照大多数人对SO提出的以下问题(但是这段代码显示如何加密字符串)。有人可以帮助我加密和解密图像吗?问题中的代码是否适用于图像?



链接到问题: Java 256位AES密码加密



这是我所做的直到现在:



//全局数组列表存储iv和加密

  static ArrayList< byte []> ivandcipher = new ArrayList< byte []>(); 

//生成键

  public static SecretKey generateKey()throws NoSuchAlgorithmException {

char [] password = {'a','b','c','d','e'} ;
byte [] salt = {1,2,3,4,5};

SecretKeyFactory factory = SecretKeyFactory
.getInstance(PBKDF2WithHmacSHA1);
KeySpec spec = new PBEKeySpec(password,salt,65536,256);
SecretKey tmp = null;
try {
tmp = factory.generateSecret(spec);
} catch(InvalidKeySpecException e){
e.printStackTrace();
}

yourKey = new SecretKeySpec(tmp.getEncoded(),AES);

return yourKey;
}

//编码文件



// byte [] fileData,包含转换为byte []的位图(图像)

  public static ArrayList< byte []> encodeFile(SecretKey yourKey,byte [] fileData)
throws异常{

byte [] encrypted = null;

密码密码= Cipher.getInstance(AES / CBC / PKCS5Padding);
cipher.init(Cipher.ENCRYPT_MODE,yourKey);
AlgorithmParameters params = cipher.getParameters();
byte [] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
encrypted = cipher.doFinal(fileData);

ivandcipher.clear();
ivandcipher.add(iv);
ivandcipher.add(encrypted);

return ivandcipher;
}

为什么我将iv和加密字节[] s添加到ivandcipher。因为,正如链接中的答案所示,我应该在解密时使用相同的iv。



//解码文件



//我在这个方法中调用一个重载的decodeFile方法..请注意

 私有位图decodeFile(String filename ){

try {
yourKey = generateKey();
} catch(NoSuchAlgorithmException e1){
e1.printStackTrace();
}

try {
byte [] decodedData = decodeFile(yourKey,readFile(filename));
位图位图= bytesToBitmap(decodedData);

返回位图;
} catch(Exception e){
e.printStackTrace();
}

返回null;
}

//重载decodeFile方法



$

byte [] decryptpted = null;
密码密码= Cipher.getInstance(AES / CBC / PKCS5Padding);
cipher.init(Cipher.DECRYPT_MODE,yourKey,new IvParameterSpec(ivandcipher.get(0)));
decryptpted = cipher.doFinal(fileData);
返回解密;
}

我猜问题是fileData [],我不能正确加密和解​​密。对于Strings,如上述链接的答案所示,即



byte [] ciphertext = cipher.doFinal(Hello,World! .getBytes(UTF-8));



作为cipher.doFinal()的参数应该给予什么?



如果您需要任何其他代码,请让我知道。

解决方案

代码在问题的作品中,但一旦摄像机拍摄图像,则需要大约10秒才能恢复活动。我放弃了这种方法,并利用Facebook的隐形图书馆加密和解密图像。链接到Facebook的解决方案: Facebook隐藏 - 图像加密和解密


EDIT ::: The code in the question works, but it takes around 10 seconds for getting back to the activity once the image is taken in camera. I gave up this approach and used Facebook's Conceal Library to encrypt and decrypt images. Link to Facebook's Solution : Facebook Conceal - Image Encryption and Decryption


I have looked at lot of examples, but still couldn't figure out a way to get Encryption and Decryption right. I thought i got it correct when I used some random code on the internet, but while decoding, i get a BadPadding Exception.

So, i am trying to work it out. I am following the below question, as suggested by most people on SO (but this code shows how to encrypt a string). Can some one help me out in encrypting and decrypting the image? Will the code in the question work for images?

Link to the question : Java 256-bit AES Password-Based Encryption

Here is what i have done till now:

//Global arraylist to store iv and cipher

static ArrayList<byte[]> ivandcipher = new ArrayList<byte[]>();

//Generating Key

public static SecretKey generateKey() throws NoSuchAlgorithmException {

    char[] password = { 'a', 'b', 'c', 'd', 'e' };
    byte[] salt = { 1, 2, 3, 4, 5 };

    SecretKeyFactory factory = SecretKeyFactory
            .getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(password, salt, 65536, 256);
    SecretKey tmp = null;
    try {
        tmp = factory.generateSecret(spec);
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }

    yourKey = new SecretKeySpec(tmp.getEncoded(), "AES");

    return yourKey;
}

//Encoding File

//byte[] fileData, contains the bitmap(image) converted to byte[]

public static ArrayList<byte[]> encodeFile(SecretKey yourKey, byte[] fileData)
        throws Exception {

    byte[] encrypted = null;

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, yourKey);
    AlgorithmParameters params = cipher.getParameters();
    byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
    encrypted = cipher.doFinal(fileData);   

    ivandcipher.clear();
    ivandcipher.add(iv);
    ivandcipher.add(encrypted);

    return ivandcipher;
}

Why am i adding iv and encrypted byte[]s to ivandcipher. Because, as the answer in the link suggests, that i should be using the same iv while decryption.

//Decode file

//I call a overloaded decodeFile method inside this method.. please note

private Bitmap decodeFile(String filename) {

    try {
        yourKey = generateKey();
    } catch (NoSuchAlgorithmException e1) {
        e1.printStackTrace();
    }

    try {
        byte[] decodedData = decodeFile(yourKey, readFile(filename));
        Bitmap bitmap = bytesToBitmap(decodedData);

        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

//overloaded decodeFile method

public static byte[] decodeFile(SecretKey yourKey, byte[] fileData)
        throws Exception {

    byte[] decrypted = null;
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, yourKey, new IvParameterSpec(ivandcipher.get(0)));
    decrypted = cipher.doFinal(fileData);
    return decrypted;
}

I guess the problem is with the fileData[], that i am not able to encrypt and decrypt correctly. For Strings as shown in the answer of the above link, i.e.,

byte[] ciphertext = cipher.doFinal("Hello, World!".getBytes("UTF-8"));

what should be given as parameter for cipher.doFinal()?

Let me know if you need any other piece of code.

解决方案

The code in the question works, but it takes around 10 seconds for getting back to the activity once the image is taken in camera. I gave up this approach and used Facebook's Conceal Library to encrypt and decrypt images. Link to Facebook's Solution : Facebook Conceal - Image Encryption and Decryption

这篇关于使用AES加密和解密图像的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆