Java - Encrypt part of the file(audio & image) instead of the whole file?
I read something about this here Android - Increas开发者_如何学Ce speed of encryption
I need to only encrypt part of the file(audio & image). Is this possible? Can somebody provide snippet of both encryption and decryption if it's possible?
Thanks in advance =)
You could define an encrypted member and a non-encrypted members of your class. Then, just serialize them out appropriately. For instance, your class may be something like this:
public Class partiallyEncrypted {
private transient byte[] imagePart1Decrypted; // transient member will not be serialized by default
private byte[] imagePart1Encrypted;
private byte[] imagePart2;
private static final int BYTES_TO_ENCRYPT = 2048;
// other non-encrypted fields
...
public setImage(byte[] imageBytes) {
// calc bytes to encrypt and those left over, initialize arrays
int bytesToEncrypt = Math.min(imageBytes.length, BYTES_TO_ENCRYPT);
int bytesLeftOver = Math.max(imageBytes.length - bytesToEncrypt, 0);
imagePart1Decrypted = new byte[bytesToEncrypt];
imagePart2 = new byte[bytesLeftOver];
// copy data into local arrays
System.arraycopy(imageBytes, 0, imagePart1Decrypted, 0, bytesToEncrypt);
if (bytesLeftOver > 0)
System.arraycopy(imageBytes, bytesToEncrypt, imagePart2, 0, bytesLeftOver);
imagePart1Encrypted = encrypt(bytesToEncrypt);
}
public byte[] getImage() {
if (imagePart1Decrypted == null)
imagePart1Decrypted = decrypt(imagePart1Encrypted);
byte[] fullImage = new byte[imagePart1Decrypted.length + imagePart2.length];
System.arraycopy(imagePart1Decrypted, 0, fullImage, 0, imagePart1Decrypted.length);
if (imagePart2 != null && imagePart2.length > 0)
System.arraycopy(imagePart2, 0, fullImage, imagePart1Decrypted.length, imagePart2.length);
return fullImage;
}
}
精彩评论