首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android离线播放视频加密

Android离线播放视频加密
EN

Stack Overflow用户
提问于 2017-01-26 20:24:16
回答 1查看 1K关注 0票数 3

我想从服务器下载mp4视频到我的安卓设备上。我希望这个视频被存储在块(加密),并应在视频播放时实时组合。如何开始这方面的工作。

EN

回答 1

Stack Overflow用户

发布于 2019-01-04 18:14:38

对于这样的样本

代码语言:javascript
复制
public class AESdemo extends Activity {
boolean encryptionIsOn = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aesdemo);
    // needs <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    String homeDirName = Environment.getExternalStorageDirectory().getAbsolutePath() +
            "/" + getPackageName();
    File file = new File(homeDirName, "test.txt");
    byte[] keyBytes = getKey("password");

    try {
        File dir = new File(homeDirName);
        if (!dir.exists())
            dir.mkdirs();
        if (!file.exists())
            file.createNewFile();

        OutputStreamWriter osw;

        if (encryptionIsOn) {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

            FileOutputStream fos = new FileOutputStream(file);
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);
            osw = new OutputStreamWriter(cos, "UTF-8");
        }
        else    // not encryptionIsOn
            osw = new FileWriter(file);

        BufferedWriter out = new BufferedWriter(osw);
        out.write("This is a test\n");
        out.close();
    }
    catch (Exception e) {
        System.out.println("Encryption Exception "+e);
    }

    ///////////////////////////////////
    try {
        InputStreamReader isr;

        if (encryptionIsOn) {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES");
            IvParameterSpec ivParameterSpec = new IvParameterSpec(keyBytes);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);

            FileInputStream fis = new FileInputStream(file);
            CipherInputStream cis = new CipherInputStream(fis, cipher);
            isr = new InputStreamReader(cis, "UTF-8");
        }
        else
            isr = new FileReader(file);

        BufferedReader in = new BufferedReader(isr);
        String line = in.readLine();
        System.out.println("Text read: <"+line+">");
        in.close();
    }
    catch (Exception e) {
        System.out.println("Decryption Exception "+e);
    }
}

private byte[] getKey(String password) throws UnsupportedEncodingException {
    String key = "";
    while (key.length() < 16)
        key += password;
    return key.substring(0, 16).getBytes("UTF-8");
}

}

使用.mp4代替文本文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41873402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档