Android加密:加密文件大小小于原始文件

时间:2016-03-14 09:19:49

标签: android encryption

加密视频文件后,加密文件大小小于原始文件(通常是原始文件大小的一半)。所以我无法解密它。 我的加密代码是..

public static void encryptMe(String  vidName)
{
    try {
        File rawData = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS), "HSDownloads");
        File fiss = new File(rawData, vidName);
        Log.d(TAG,"input file  "+fiss.toString());
        FileInputStream in = new FileInputStream(fiss);

        File encreptedFileDirectory = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS), "HSEncript");

        if(encreptedFileDirectory.exists())
        {

        }
        else
        {
            encreptedFileDirectory.mkdirs();
        }
        File outfile = new File(encreptedFileDirectory,vidName);
        OutputStream out= new FileOutputStream(outfile);

        byte[] skey = AppUtiles.generateKey("qwertyuiopasdfgh");
        SecretKeySpec key = new SecretKeySpec(skey, "AES");


        Cipher c = Cipher.getInstance("AES");
        c.init(Cipher.ENCRYPT_MODE, key);

        out = new CipherOutputStream(out, c);
        int count = 0;
        byte[] buffer = new byte[128*1024];
        while ((count = in.read(buffer)) >= 0)
        {
            out.write(buffer, 0, count);
            Log.d(TAG,"inside while loop encryptMe()"+(count = in.read(buffer)));
        }
       out.close();
    }
    catch(Exception e)
    {
        Log.d(TAG,"exception encryption >"+Log.getStackTraceString(e));
    }
    finally 
    {
        Log.d(TAG,"Inside finally   ");
    }
}

用于解密和流媒体视频,  我正在使用libmedia jar。

public void playENCVideo(String path) {
    try {
        Cipher decipher = null;

        KeyGenerator kgen = KeyGenerator.getInstance("AES");


        byte[] key = AppUtiles.generateKey("qwertyuiopasdfgh");
        SecretKeySpec skey = new SecretKeySpec(key, "AES");

        decipher = Cipher.getInstance("AES");

        decipher.init(Cipher.DECRYPT_MODE, skey);

        mServer = new LocalSingleHttpServer();

        mServer.setCipher(decipher);
        mServer.start();

        path = mServer.getURL(path);

        vvPlayer.setVideoPath(path);
        vvPlayer.start();
    } catch (InvalidKeyException e) {
        Log.d(TAG, "InvalidKeyException  ");
    } catch (NoSuchAlgorithmException e) {
        Log.d(TAG, "NoSuchAlgorithmException  ");

    } catch (NoSuchPaddingException e) {
        Log.d(TAG, "NoSuchPaddingException  ");

    } catch (IOException e) {
        Log.d(TAG, "IOEXCEPTION   ");

    }
    catch (Exception e) {
            Log.d(TAG,"Genral exceptin");
    }
}

1 个答案:

答案 0 :(得分:0)

您在日志语句中从输入文件中读取的数据:

Log.d(TAG,"inside while loop encryptMe()"+(count = in.read(buffer)));

永远不会加密。只有while循环中读取的数据才会写入out

编辑: 不要阅读2:

                   read 1
                   VVVV
while ((count = in.read(buffer)) >= 0) {
    out.write(buffer, 0, count);          
    Log.d(TAG,"inside while loop encryptMe()"+(count = in.read(buffer)));
}                                                         ^^^^
                                                          read 2