在此代码文件中保存原始图像,加密图像和解密图像。
我有两个问题:
只有加密图像应存储在文件中(savedimages)
加密图像应直接解密为bytearray或可在imageView中设置为位图的东西。目前它直接取自文件。
有没有办法实现这一点,或者我应该尝试其他一些方法
public Main3Activity() {
try {
// SecureRandom sr = SecureRandom.getInstance("SHA1PRNG","Crypto");
keyGenerator = KeyGenerator.getInstance("Blowfish");
secretKey = keyGenerator.generateKey();
cipher = Cipher.getInstance("Blowfish");
} catch (NoSuchPaddingException ex) {
System.out.println(ex);
} catch (NoSuchAlgorithmException ex) {
System.out.println(ex);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
ActivityCompat.requestPermissions(this, new String[]{ACCESS_FINE_LOCATION, CAMERA, WRITE_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
v1=(ImageView)findViewById(R.id.imageViewnew);
b1=(Button)findViewById(R.id.cameranew);
final String imagePath = "image" + ".png";
File root = new File(Environment.getExternalStorageDirectory() + File.separator + "savedimages");
root.mkdirs();
if (!root.exists()) {
root.mkdir();
}
file = new File(Environment.getExternalStorageDirectory() + File.separator + "savedimages",imagePath);
file1 = new File(Environment.getExternalStorageDirectory() + File.separator + "savedimages","Encryp"+imagePath);
file2 = new File(Environment.getExternalStorageDirectory() + File.separator + "savedimages","decryp"+imagePath);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
OutputStream outStream = null;
InputStream inStream = null;
try {
System.out.println("Encryption Over");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
inStream = new FileInputStream(file);
outStream = new FileOutputStream(file1);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
outStream.close();
} catch (IllegalBlockSizeException ex) {
System.out.println(ex);
} catch (BadPaddingException ex) {
System.out.println(ex);
} catch (InvalidKeyException ex) {
System.out.println(ex);
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
try {
System.out.println("Decryption Over");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
inStream = new FileInputStream(file1);
outStream = new FileOutputStream(file2);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0)
{
//------------>>>>>>>>> Here Image is Storing instead of that it should read as byte array
// ------------>>>>>>>>>> and read as bitmap and set in Imageview for preview
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
System.out.print("Decryption ovverrr");
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (IllegalBlockSizeException ex) {
System.out.println(ex);
} catch (BadPaddingException ex) {
System.out.println(ex);
} catch (InvalidKeyException ex) {
System.out.println(ex);
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
} catch (Exception ex) {
System.out.println(ex);
}
BitmapFactory.Options options=new BitmapFactory.Options();
final Bitmap b=BitmapFactory.decodeFile(file2.toString(),options);
System.out.println("Image getting from File");
//----------->>>>>>>>>> Decrypted Image should view Here. Currently I am getting directly from file.
v1.setImageBitmap(b);
}
}
}
答案 0 :(得分:1)
此library在某些时候提供加密,但我没有检查哪种加密可能对您来说足够了。它比Java File类更容易使用。
答案 1 :(得分:0)
使用this图书馆我找到了问题的解决方案。这可能对某人有所帮助。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
v1=(ImageView)findViewById(R.id.imageViewnew);
v2=(ImageView)findViewById(R.id.imageViewupdate);
b1=(Button)findViewById(R.id.cameranew);
b2=(Button)findViewById(R.id.cameraupp);
String IVX = "abcdefghijklmnop"; // 16 lenght - not secret
String SECRET_KEY = "secret1234567890"; // 16 lenght - secret
SimpleStorageConfiguration configuration = new SimpleStorageConfiguration.Builder()
.setEncryptContent(IVX, SECRET_KEY)
.build();
if (SimpleStorage.isExternalStorageWritable()) {
storage = SimpleStorage.getExternalStorage();
}
else {
storage = SimpleStorage.getInternalStorage(getApplicationContext());
}
SimpleStorage.updateConfiguration(configuration);
imagePath = "image" + ".png";
boolean dirExists = storage.isDirectoryExists("savedimages");
if(dirExists==true)
{
System.out.println("Directoy Already Created");
}
else {
storage.createDirectory("savedimages", true);
}
boolean fileExists = storage.isFileExist("savedimages",imagePath);
if(fileExists==true) {
System.out.println("File Already Created");
}
else {
storage.createFile("savedimages", imagePath, " ");
}
ff=storage.getFile("savedimages",imagePath);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
outputFileUri = Uri.fromFile(ff);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
BitmapFactory.Options options=new BitmapFactory.Options();
final Bitmap b=BitmapFactory.decodeFile(ff.toString(),options);
v2.setImageBitmap(b);
storage.createFile("savedimages",imagePath,b);
}
}}