1

根据 Apple 的技术说明,我曾经使用 NoPadding 算法的 AES-128 CBC。我可以使用以下代码加密和解密任何视频。我尝试了 Muxed 加密和 Un-Muxed 加密视频。但我无法正确解密原始测试内容。当我加密原始未加密文件时,它不会在 Safari 上播放。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class Main {
    public static void main(String[] args)
            throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException,
            NoSuchPaddingException, FileNotFoundException, IllegalBlockSizeException, BadPaddingException, IOException {
        // TODO Auto-generated method stub
        byte[] IVBytes = "\u00d5\u00fb\u00d6\u00b8\u002e\u00d9\u003e\u004e\u00f9\u008a\u00e4\u0009\u0031\u00ee\u0033\u00b7".getBytes();
        byte[] KeyBytes = "\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c\u003c".getBytes();
        IvParameterSpec ivspec = new IvParameterSpec(IVBytes);
        SecretKeySpec skey = new SecretKeySpec(KeyBytes, "AES");
        Cipher ci = Cipher.getInstance("AES/CBC/NoPadding");

        ci.init(Cipher.DECRYPT_MODE, skey, ivspec);
        processFile(ci, "C:\\test_java\\encryted.ts", "C:\\test_java\\unencryted.ts");

        /*
        ci.init(Cipher.ENCRYPT_MODE, skey, ivspec);
        processFile(ci, "C:\\test_java\\main.ts", "C:\\test_java\\encrypted_unmu.ts");
*/


    }
    static private void processFile(Cipher ci, String inFile, String outFile)
            throws FileNotFoundException, IOException, IllegalBlockSizeException, BadPaddingException {
        try (FileInputStream in = new FileInputStream(inFile); FileOutputStream out = new FileOutputStream(outFile)) {


                    byte[] ibuf = new byte[1024];
                    int len;
                    while ((len = in.read(ibuf)) != -1) {
                        len = len - (len % 16);

                        byte[] obuf = ci.update(ibuf, 0, len);
                        if ( obuf != null ) out.write(obuf);
                    }
                    byte[] obuf = ci.doFinal();
                    if ( obuf != null ) out.write(obuf);
        }
    }   
}
4

0 回答 0