How to encrypt and decrypt images in android?

后端 未结 3 1002
一生所求
一生所求 2021-02-11 03:33

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 ima

3条回答
  •  太阳男子
    2021-02-11 03:43

    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();
        }
     }
    }
    

    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> 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() {
                {
                    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();
        }
    
    }
    
    }
    

提交回复
热议问题