Skip to main content
 首页 » 编程设计

java之python AES加密java解密

2025年05月04日28落叶无声

我有 1000 多张图片和视频需要加密。没有什么比这更简单了,我正在考虑使用 AES,但我不知道如何在我的计算机上加密然后解密设备上的项目。

我可能会使用 python 加密计算机上的所有项目。然后以按需方式使用 java(Android 应用程序)解密该项目

任何简单的解释都可以,伪代码也可以。

我遇到的主要问题是如何使用相同的 key 来加密和解密。我一直在生成 key ,但无法将其移植到其他设备进行解密。

谢谢

Python 代码。 Works 加密和解密。

from Crypto.Cipher import AES 
import os, random, struct 
 
key = '0123456789abcdef' 
mode = AES.MODE_CBC 
chunksize = 64*1024 
 
iv = ''.join(chr(random.randint(0,0xFF)) for i in range(16)) 
encryptor = AES.new(key,mode,iv) 
filesize = os.path.getsize('sample.jpg') 
 
with open('sample.jpg','rb') as infile: 
    with open('sample.enc','wb') as outfile: 
       outfile.write(struct.pack('<Q',filesize)) 
       outfile.write(iv) 
 
    while True: 
        chunk = infile.read(chunksize) 
        if len(chunk) == 0: 
            break 
        elif len(chunk) % 16 != 0: 
            chunk += ' ' * (16 - len(chunk) % 16) 
 
        outfile.write(encryptor.encrypt(chunk)) 
 
## decrypt 
with open('sample.enc', 'rb') as infile: 
       origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0] 
       iv = infile.read(16) 
       decryptor = AES.new(key, AES.MODE_CBC, iv) 
 
       with open('sample2.jpg', 'wb') as outfile: 
          while True: 
            chunk = infile.read(chunksize) 
            if len(chunk) == 0: 
                break 
            outfile.write(decryptor.decrypt(chunk)) 
 
        outfile.truncate(origsize) 

我将如何在 Java 中完成解密部分?这是我快速草率的 java 代码,它不起作用。我认为是填充弄乱了它。

  public void decryptFile(){ 
 
    String inFile = "sample.enc"; 
    String outFile = "sample.jpg"; 
    String dir = Environment.getExternalStorageDirectory() +"/Android/data/HOT/"; 
    InputStream is ; 
    byte[] iv = new byte[16]; 
    try { 
        is = new FileInputStream(dir+inFile); 
 
        is.read(iv); 
 
    } catch (FileNotFoundException e1) { 
        // TODO Auto-generated catch block 
        Log.d("D1","no file found"); 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        Log.d("D-2","no file found"); 
        e.printStackTrace(); 
    } 
 
    byte[] k = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; 
 
    Key key = new SecretKeySpec(k,"AES"); 
 
 
 
 
    try { 
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
        cipher.init(Cipher.DECRYPT_MODE, key,new IvParameterSpec(iv)); 
        OutputStream outs = new FileOutputStream(dir+outFile); 
        is = new FileInputStream(dir+inFile); 
        while(true){ 
            byte[] chunk = new byte[64*1024]; 
            is.read(chunk); 
            if(chunk.length == 0){ 
 
                break; 
 
            } 
            outs.write(cipher.doFinal(chunk));               
        } 
 
 
    } catch (NoSuchAlgorithmException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","1"); 
 
        e.printStackTrace(); 
    } catch (NoSuchPaddingException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","2"); 
        e.printStackTrace(); 
    } catch (InvalidKeyException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","3"); 
        e.printStackTrace(); 
    } catch (InvalidAlgorithmParameterException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","4"); 
        e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","5"); 
        e.printStackTrace(); 
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","6"); 
        e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","7"); 
        e.printStackTrace(); 
    } catch (BadPaddingException e) { 
        // TODO Auto-generated catch block 
        Log.d("D","8"); 
        e.printStackTrace(); 
    } 
 
    ImageView im = (ImageView)findViewById(R.id.imageView2); 
 
    Bitmap mainBitmap = BitmapFactory.decodeFile(dir+outFile); 
 
    im.setImageBitmap(mainBitmap); 
 
} 

请您参考如下方法:

与 Python 版本不同,在 Java 版本中,您似乎在读取 IV 之前没有读取文件大小。

您还打开了第二个 FileInputStream,然后在读取 Cipher block 之前不要跳过文件大小和 IV。

要检查的另一件事是键在 Java 和 Python 中的解释相同,即 Python 中的字符串产生与 Java 中的字节数组相同的字节序列。