python AES加密java解密 [英] python AES encryption java decryption
问题描述
我有超过1000张图片和视频需要加密。没有什么超过顶部只是一些简单的,我在想使用AES,但我不知道如何在我的电脑上加密,然后解密设备上的项目。
我将加密我计算机上可能使用python的所有项目。然后在一个按需的时尚将解密该项目与java(Android应用程序)
任何简单的解释将伪代码也是罚款。
主要问题是如何使用相同的密钥进行加密和解密。我一直在生成密钥,无法将其移植到其他设备进行解密。
谢谢
Python代码。作品加密和解密。
从Crypto.Cipher导入AES
import os,random,struct
key ='0123456789abcdef'
mode = AES.MODE_CBC
chunksize = 64 * 1024
iv =''.join(chr(random.randint(0,0xFF ))对于我在范围(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)
如果len )== 0:
break
elif len(chunk)%16!= 0:
chunk + =''*(16 - len(chunk)%16)
outfile.write(encryptor.encrypt(chunk))
##解密
与open('sample.enc','rb')作为infile:
origsize = struct.unpack('< Q',infile.rea d(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是;
byte [] iv = new byte [16];
try {
is = new FileInputStream(dir + inFile);
is.read(iv);
} catch(FileNotFoundException e1){
// TODO自动生成的catch块
Log.d(D1,找不到文件);
} catch(IOException e){
// TODO自动生成的catch块
Log.d(D-2,找不到文件);
e.printStackTrace();
}
byte [] k = {'0','1','2','3','4','5','6' , '8', '9', 'A', 'b', 'C', 'D', 'E', 'F'};
Key key = new SecretKeySpec(k,AES);
try {
密码密码= Cipher.getInstance(AES / CBC / PKCS5Padding);
cipher.init(Cipher.DECRYPT_MODE,key,new IvParameterSpec(iv));
OutputStreamouts = 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自动生成的catch块
Log.d(D,1 );
e.printStackTrace();
} catch(NoSuchPaddingException e){
// TODO自动生成的catch块
Log.d(D,2);
e.printStackTrace();
} catch(InvalidKeyException e){
// TODO自动生成的catch块
Log.d(D,3);
e.printStackTrace();
} catch(InvalidAlgorithmParameterException e){
// TODO自动生成的catch块
Log.d(D,4);
e.printStackTrace();
} catch(FileNotFoundException e){
// TODO自动生成的catch块
Log.d(D,5);
e.printStackTrace();
} catch(IOException e){
// TODO自动生成的catch块
Log.d(D,6);
e.printStackTrace();
} catch(IllegalBlockSizeException e){
// TODO自动生成的catch块
Log.d(D,7);
e.printStackTrace();
} catch(BadPaddingException e){
// TODO自动生成的catch块
Log.d(D,8);
e.printStackTrace();
}
ImageView im =(ImageView)findViewById(R.id.imageView2);
位图mainBitmap = BitmapFactory.decodeFile(dir + outFile);
im.setImageBitmap(mainBitmap);
}
在Java版本中,在阅读IV之前,您似乎没有阅读文件大小,与Python版本不同。
您还可以打开第二个 FileInputStream
,然后在阅读密码
之前不要跳过文件大小和IV。
$ b另一件要检查的是,Java和Python中的键被解释相同,即Python中的字符串产生与Java中的字节数组相同的字节序列。
I have over 1000 images and videos that need to be encrypted. Nothing over the top just something simple, I was thinking of using AES but what I cannot figure out is how to encrypt on my computer then decrypt the item on the device.
I will be encrypting all the items on my computer probably using python. Then in an on demand fashion will decrypt the item with java (Android app)
Any simple explanation will do pseudo code is fine too.
The main issue im having is how to use the same key to encrypt and decrypt. I've been generating the key and can't get it to port to the other device for decryption.
Thanks
Python Code. Works encrypts and decrypts.
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)
How would I do the decrypt part in Java? Here is my quick sloppy java code that doesn't work. I think it is the padding that is messing it up.
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);
}
In the Java version, you don't seem to be reading in the filesize before reading in the IV, unlike the Python version.
You also open a second FileInputStream
and then don't skip the filesize and IV before reading in chunks for the Cipher
.
Another thing to check is that the keys are being interpreted the same in Java and Python, i.e. the string in Python results in the same sequence of bytes as the byte array in Java.
这篇关于python AES加密java解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!