package
com.gtappdevelopers.encryptionanddecryption;
import
android.content.ContextWrapper;
import
android.content.Intent;
import
android.database.Cursor;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.net.Uri;
import
android.os.Bundle;
import
android.os.Environment;
import
android.provider.MediaStore;
import
android.view.View;
import
android.widget.Button;
import
android.widget.ImageView;
import
android.widget.Toast;
import
androidx.annotation.Nullable;
import
androidx.appcompat.app.AppCompatActivity;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.security.InvalidKeyException;
import
java.security.NoSuchAlgorithmException;
import
javax.crypto.Cipher;
import
javax.crypto.CipherInputStream;
import
javax.crypto.CipherOutputStream;
import
javax.crypto.NoSuchPaddingException;
import
javax.crypto.spec.SecretKeySpec;
public
class
MainActivity
extends
AppCompatActivity {
private
ImageView imageView;
private
Button encBtn, decBtn;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.idIVimage);
encBtn = findViewById(R.id.idBtnEncrypt);
decBtn = findViewById(R.id.idBtnDecrypt);
encBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Intent i =
new
Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,
1
);
}
});
decBtn.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
try
{
decrypt();
}
catch
(Exception e) {
Toast.makeText(MainActivity.
this
,
"Fail to decrypt image"
, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
});
}
public
void
decrypt()
throws
IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
ContextWrapper contextWrapper =
new
ContextWrapper(getApplication());
File photoDir = contextWrapper.getExternalFilesDir(Environment.DIRECTORY_DCIM);
File file =
new
File(photoDir,
"encfile"
+
".png"
);
FileInputStream fis =
new
FileInputStream(file.getPath());
File decFile =
new
File(photoDir,
"decfile.png"
);
FileOutputStream fos =
new
FileOutputStream(decFile.getPath());
SecretKeySpec sks =
new
SecretKeySpec(
"KERQIRUDYTH"
.getBytes(),
"AES"
);
Cipher cipher = Cipher.getInstance(
"AES"
);
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis =
new
CipherInputStream(fis, cipher);
int
b;
byte
[] d =
new
byte
[
8
];
while
((b = cis.read(d)) != -
1
) {
fos.write(d,
0
, b);
}
fos.flush();
fos.close();
cis.close();
Toast.makeText(
this
,
"File decrypted successfully.."
, Toast.LENGTH_SHORT).show();
File imgFile =
new
File(decFile.getPath());
if
(imgFile.exists()) {
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getPath());
imageView.setImageBitmap(bitmap);
}
}
private
void
encrypt(String path)
throws
IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis =
new
FileInputStream(path);
ContextWrapper contextWrapper =
new
ContextWrapper(getApplication());
File photoDir = contextWrapper.getExternalFilesDir(Environment.DIRECTORY_DCIM);
File file =
new
File(photoDir,
"encfile"
+
".png"
);
FileOutputStream fos =
new
FileOutputStream(file.getPath());
SecretKeySpec sks =
new
SecretKeySpec(
"KERQIRUDYTH"
.getBytes(),
"AES"
);
Cipher cipher = Cipher.getInstance(
"AES"
);
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos =
new
CipherOutputStream(fos, cipher);
int
b;
byte
[] d =
new
byte
[
8
];
while
((b = fis.read(d)) != -
1
) {
cos.write(d,
0
, b);
}
cos.flush();
cos.close();
fis.close();
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
@Nullable
Intent data) {
super
.onActivityResult(requestCode, resultCode, data);
if
(requestCode ==
1
&& resultCode == RESULT_OK &&
null
!= data) {
Uri imgUri = data.getData();
String[] filePath = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(imgUri, filePath,
null
,
null
,
null
);
cursor.moveToFirst();
int
columnIndex = cursor.getColumnIndex(filePath[
0
]);
String picPath = cursor.getString(columnIndex);
cursor.close();
try
{
encrypt(picPath);
Toast.makeText(
this
,
"Image encrypted.."
, Toast.LENGTH_SHORT).show();
}
catch
(Exception e) {
e.printStackTrace();
Toast.makeText(
this
,
"Fail to encrypt image : "
+ e, Toast.LENGTH_SHORT).show();
}
}
}
}