如何在Android中加密和解密图像? [英] How to encrypt and decrypt images in android?

查看:72
本文介绍了如何在Android中加密和解密图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,现在我正在构建一个android应用程序来加密和隐藏数据,这是我的mini项目的一部分.能否请您告诉我使用AES算法在android中加密和解密tha图像的代码..??

I am new to android and now i'm building an android app to encrypt and hide the data,as part of my mini project.Could you please tell me the code to encrypt and decrypt tha images in android using AES algorithm..??

推荐答案

您可以使用以下方法对媒体文件(例如视频或图像)进行加密或解密-

You can use the following methods to Encrypt or Decrypt media files such as video or images --

public class Encrypter {
private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024;
private final static String ALGO_VIDEO_ENCRYPTOR = "AES/CBC/PKCS5Padding";

@SuppressWarnings("resource")
public static void encrypt(SecretKey key, 
        AlgorithmParameterSpec paramSpec, InputStream in, OutputStream out)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IOException {
    try {
        Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR);
        c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        out = new CipherOutputStream(out, c);
        int count = 0;
        byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
        while ((count = in.read(buffer)) >= 0) {
            out.write(buffer, 0, count);
        }
    } finally {
        out.close();
    }
}
@SuppressWarnings("resource")
public static void decrypt(SecretKey key, AlgorithmParameterSpec paramSpec, 
        InputStream in, OutputStream out)
        throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
        InvalidAlgorithmParameterException, IOException {
    try {
        Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR);
        c.init(Cipher.DECRYPT_MODE, key, paramSpec);
        out = new CipherOutputStream(out, c);
        int count = 0;
        byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
        while ((count = in.read(buffer)) >= 0) {
            out.write(buffer, 0, count);
        }
    } finally {
        out.close();
    }
 }
}

这里我正在加密视频.例如,下面是我的MainActivity,它使用这些方法来加密视频文件.

Here i am encrypting the videos.For example following is my MainActivity that uses these methods to encrypt the video file.

public class MainActivity extends AppCompatActivity {
private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG";
private final static String ALGO_SECRET_KEY_GENERATOR = "AES";
private final static int IV_LENGTH = 16;
Cursor mVideoCursor;
ArrayList<HashMap<String, String>> listOfVideo;
@Override    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    listOfVideo = new ArrayList();
    String[] videoColumns = {MediaStore.Video.Media.DATA,MediaStore.Video.Media.DURATION,
    MediaStore.Video.Media.SIZE,MediaStore.Video.Media.DISPLAY_NAME};
    mVideoCursor = getApplicationContext().getContentResolver().query
            (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, null, null);
    mVideoCursor.moveToFirst();
    for (int i = 0; i < mVideoCursor.getCount(); i++) {
        listOfVideo.add(new HashMap<String, String>() {
            {
                put("data", String.valueOf(mVideoCursor.getString( 
                mVideoCursor.getColumnIndex(MediaStore.Video.Media.DATA))));
                put("duration", String.valueOf(mVideoCursor.getString(
                mVideoCursor.getColumnIndex(MediaStore.Video.Media.DURATION))));
                put("displayName", String.valueOf(mVideoCursor.getString(
                mVideoCursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME))));
                put("size", String.valueOf(mVideoCursor.getString(
                mVideoCursor.getColumnIndex(MediaStore.Video.Media.SIZE))));
                mVideoCursor.moveToNext();

            }
        });
    }

    String path = listOfVideo.get(0).get("data");
    File inFile = new File(listOfVideo.get(0).get("data"));
    File outFile = new File(path.substring(0, path.lastIndexOf("/"))+"/enc_video.swf");
    File outFile_dec = new File(path.substring(0, path.lastIndexOf("/"))+"/dec_video.mp4");

    try {
        SecretKey key = KeyGenerator.getInstance(ALGO_SECRET_KEY_GENERATOR).generateKey();
        byte[] keyData = key.getEncoded();
        SecretKey key2 = new SecretKeySpec(keyData, 0, keyData.length, ALGO_SECRET_KEY_GENERATOR);  
        byte[] iv = new byte[IV_LENGTH];
        SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR).nextBytes(iv);
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

        Encrypter.encrypt(key, paramSpec, 
        new FileInputStream(inFile), new FileOutputStream(outFile));
        Encrypter.decrypt(key2, paramSpec, 
        new FileInputStream(outFile), new FileOutputStream(outFile_dec));
    } catch (Exception e) {
        e.printStackTrace();
    }

}

}

这篇关于如何在Android中加密和解密图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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