我是 Android 新手,现在我正在构建一个 Android 应用程序来加密和隐藏数据,作为我的迷你项目的一部分。您能告诉我使用 AES 算法在 Android 中加密和解密图像的代码吗? .??
最佳答案
您可以使用以下方法来加密或解密视频或图像等媒体文件 --
public class Encrypter {
private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024;
private final static String ALGO_VIDEO_ENCRYPTOR = "AES/CBC/PKCS5Padding";
@SuppressWarnings("resource")
public static void encrypt(SecretKey key,
AlgorithmParameterSpec paramSpec, InputStream in, OutputStream out)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException, IOException {
try {
Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR);
c.init(Cipher.ENCRYPT_MODE, key, paramSpec);
out = new CipherOutputStream(out, c);
int count = 0;
byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
while ((count = in.read(buffer)) >= 0) {
out.write(buffer, 0, count);
}
} finally {
out.close();
}
}
@SuppressWarnings("resource")
public static void decrypt(SecretKey key, AlgorithmParameterSpec paramSpec,
InputStream in, OutputStream out)
throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException,
InvalidAlgorithmParameterException, IOException {
try {
Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR);
c.init(Cipher.DECRYPT_MODE, key, paramSpec);
out = new CipherOutputStream(out, c);
int count = 0;
byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE];
while ((count = in.read(buffer)) >= 0) {
out.write(buffer, 0, count);
}
} finally {
out.close();
}
}
}
这里我正在加密视频。例如,以下是我的 MainActivity,它使用这些方法来加密视频文件。
public class MainActivity extends AppCompatActivity {
private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG";
private final static String ALGO_SECRET_KEY_GENERATOR = "AES";
private final static int IV_LENGTH = 16;
Cursor mVideoCursor;
ArrayList<HashMap<String, String>> listOfVideo;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listOfVideo = new ArrayList();
String[] videoColumns = {MediaStore.Video.Media.DATA,MediaStore.Video.Media.DURATION,
MediaStore.Video.Media.SIZE,MediaStore.Video.Media.DISPLAY_NAME};
mVideoCursor = getApplicationContext().getContentResolver().query
(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, null, null);
mVideoCursor.moveToFirst();
for (int i = 0; i < mVideoCursor.getCount(); i++) {
listOfVideo.add(new HashMap<String, String>() {
{
put("data", String.valueOf(mVideoCursor.getString(
mVideoCursor.getColumnIndex(MediaStore.Video.Media.DATA))));
put("duration", String.valueOf(mVideoCursor.getString(
mVideoCursor.getColumnIndex(MediaStore.Video.Media.DURATION))));
put("displayName", String.valueOf(mVideoCursor.getString(
mVideoCursor.getColumnIndex(MediaStore.Video.Media.DISPLAY_NAME))));
put("size", String.valueOf(mVideoCursor.getString(
mVideoCursor.getColumnIndex(MediaStore.Video.Media.SIZE))));
mVideoCursor.moveToNext();
}
});
}
String path = listOfVideo.get(0).get("data");
File inFile = new File(listOfVideo.get(0).get("data"));
File outFile = new File(path.substring(0, path.lastIndexOf("/"))+"/enc_video.swf");
File outFile_dec = new File(path.substring(0, path.lastIndexOf("/"))+"/dec_video.mp4");
try {
SecretKey key = KeyGenerator.getInstance(ALGO_SECRET_KEY_GENERATOR).generateKey();
byte[] keyData = key.getEncoded();
SecretKey key2 = new SecretKeySpec(keyData, 0, keyData.length, ALGO_SECRET_KEY_GENERATOR);
byte[] iv = new byte[IV_LENGTH];
SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR).nextBytes(iv);
AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
Encrypter.encrypt(key, paramSpec,
new FileInputStream(inFile), new FileOutputStream(outFile));
Encrypter.decrypt(key2, paramSpec,
new FileInputStream(outFile), new FileOutputStream(outFile_dec));
} catch (Exception e) {
e.printStackTrace();
}
}
}
关于android - 如何在android中对图像进行加密和解密?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26697112/