这是解密的代码,但它不起作用。你能帮助我吗?我不知道为什么这段代码不起作用。图像解密仍然有效,但我只有更糟糕的图像。我第一次认为我的代码加密和解密错误,但当我学到更多,它似乎很好。没有错误。我先生卡住了,我不知道为什么图片无法解密。请成为我的救世主。
package com.drag0nathz.dekripimage;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
public class MainActivity extends Activity {
private int REQUEST_CAMERA = 0, SELECT_FILE = 1;
private Button btnSelect;
EditText editText13, editText11;
Button btnDekrip, button12;
private ImageView ivImage;
private String userChoosenTask;
private Bitmap operation, bmp;
ByteArrayOutputStream bytearrayoutputstream;
//FileOutputStream fileoutputstream;
String logMerah = "log warna merah program ini : ";
String logHijau = "log warna hijau program ini : ";
String logBiru = "log warna biru program ini : ";
String char_db = "abcdefghijklmnopqrstuvwxyz";
int[][] M_key = new int[3][3];
int[][] M_inverse = new int [3][3];
int i;
String key_input, ciphertext, keyinversechar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText11 = (EditText) findViewById(R.id.editText11);
editText13 = (EditText) findViewById(R.id.editText13);
button12 = (Button) findViewById(R.id.button12);
button12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v == button12){
key_input = editText11.getText().toString();
if (key_input.length() < 9)
Toast.makeText(MainActivity.this, "panjang kunci harus 9 karakter!" , Toast.LENGTH_SHORT).show();
else if (key_input.length() > 9){
Toast.makeText(MainActivity.this, "panjang kunci harus 9 karakter!" , Toast.LENGTH_SHORT).show();
}
else{
check_kunci(key_input);
}
}
}
});
btnSelect = (Button) findViewById(R.id.btnSelectPhoto);
btnSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
btnDekrip = (Button) findViewById(R.id.buttonDekrip);
ivImage = (ImageView) findViewById(R.id.ivImage);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case Utility.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if(userChoosenTask.equals("Take Photo"))
cameraIntent();
else if(userChoosenTask.equals("Choose from Library"))
galleryIntent();
} else {
}
break;
}
}
private void selectImage() {
final CharSequence[] items = { "Take Photo", "Choose from Library",
"Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
boolean result=Utility.checkPermission(MainActivity.this);
if (items[item].equals("Take Photo")) {
userChoosenTask ="Take Photo";
if(result)
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask ="Choose from Library";
if(result)
galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void galleryIntent()
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
}
private void cameraIntent()
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
else if (requestCode == REQUEST_CAMERA)
onCaptureImageResult(data);
}
}
private void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ivImage.setImageBitmap(thumbnail);
}
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
ivImage.setImageBitmap(bm);
BitmapDrawable abmp = (BitmapDrawable) ivImage.getDrawable();
bmp = abmp.getBitmap();
}
public void check_kunci(String kunci){
Toast.makeText(this, kunci , Toast.LENGTH_SHORT).show();
char[] array_kunci = kunci.toCharArray();
int j = 0;
while ( j < array_kunci.length){
for (int x=0; x <3; x++){
for (int y=0; y<3; y++){
M_key[x][y] = char_db.indexOf(array_kunci[j]);
j = j+1;
}
}
}
int d = determinant(M_key);
d = d %256;
if (d == 0){
Toast.makeText(this, "Kunci invalid karena determinant = 0" , Toast.LENGTH_SHORT).show();
} else if (d % 2 == 0 || d % 13 == 0){
Toast.makeText(this, "Kunci invalid karena determinan memiliki persamaan faktor dgn 256" , Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Kunci Valid, multiplication inverse " + mi(d) , Toast.LENGTH_SHORT).show();
M_inverse = getInverse(M_key, mi(d));
int newArray[] = new int[M_inverse.length*M_inverse[0].length];
for (int m = 0; m < M_inverse.length; m++)
{
int [] row = M_inverse[m];
for (int n = 0; n < row.length; n++)
{
int number = M_inverse[m][n];
newArray[m*row.length+n] = number;
}
}
keyinversechar = Arrays.toString(newArray);
editText13.setText(keyinversechar);
}
}
public void dekrip(View view) {
bytearrayoutputstream = new ByteArrayOutputStream();
operation = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
int size = bmp.getRowBytes() * bmp.getHeight();
int[] gambarR = new int[size];
int[] gambarG = new int[size];
int[] gambarB = new int[size];
int k = 0;
int[] matrixGambarMod2R = new int[size];
int[] matrixGambarMod2G = new int[size];
int[] matrixGambarMod2B = new int[size];
int[] matrixGambarMod1R = new int[size];
int[] matrixGambarMod1G = new int[size];
int[] matrixGambarMod1B = new int[size];
for (int i = 0; i < bmp.getWidth(); i++) {
for (int j = 0; j < bmp.getHeight(); j++) {
int p = bmp.getPixel(i, j);
gambarR[k] = Color.red(p);
gambarG[k] = Color.green(p);
gambarB[k] = Color.blue(p);
k++;
}
}
if(size % 3 == 2){
for(int i = 0; i< matrixGambarMod2R.length-2;i=i+3){
//merah
matrixGambarMod2R[i]= (M_inverse[0][0]* matrixGambarMod2R[i]+
M_inverse[0][1]* matrixGambarMod2R[i+1]+
M_inverse[0][2]* matrixGambarMod2R[i+2]) % 256;
matrixGambarMod2R[i+1]= (M_inverse[1][0]* matrixGambarMod2R[i]+
M_inverse[1][1]* matrixGambarMod2R[i+1]+
M_inverse[1][2]* matrixGambarMod2R[i+2]) % 256;
matrixGambarMod2R[i+2]= (M_inverse[2][0]* matrixGambarMod2R[i]+
M_inverse[2][1]* matrixGambarMod2R[i+1]+
M_inverse[2][2]* matrixGambarMod2R[i+2]) % 256;
//hijau
matrixGambarMod2G[i]= (M_inverse[0][0]* matrixGambarMod2G[i]+
M_inverse[0][1]* matrixGambarMod2G[i+1]+
M_inverse[0][2]* matrixGambarMod2G[i+2]) % 256;
matrixGambarMod2G[i+1]= (M_inverse[1][0]* matrixGambarMod2G[i]+
M_inverse[1][1]* matrixGambarMod2G[i+1]+
M_inverse[1][2]* matrixGambarMod2G[i+2]) % 256;
matrixGambarMod2G[i+2]= (M_inverse[2][0]* matrixGambarMod2G[i]+
M_inverse[2][1]* matrixGambarMod2G[i+1]+
M_inverse[2][2]* matrixGambarMod2G[i+2]) % 256;
//biru
matrixGambarMod2B[i]= (M_inverse[0][0]* matrixGambarMod2B[i]+
M_inverse[0][1]* matrixGambarMod2B[i+1]+
M_inverse[0][2]* matrixGambarMod2B[i+2]) % 256;
matrixGambarMod2B[i+1]= (M_inverse[1][0]* matrixGambarMod2B[i]+
M_inverse[1][1]* matrixGambarMod2B[i+1]+
M_inverse[1][2]* matrixGambarMod2B[i+2]) % 256;
matrixGambarMod2B[i+2]= (M_inverse[2][0]* matrixGambarMod2B[i]+
M_inverse[2][1]* matrixGambarMod2B[i+1]+
M_inverse[2][2]* matrixGambarMod2B[i+2]) % 256;
}
for (int i = 0; i< size;i++){
gambarR[i] = matrixGambarMod2R[i];
gambarG[i] = matrixGambarMod2G[i];
gambarB[i] = matrixGambarMod2B[i];
}
}
//else if (size % 3 == 1) -> tambah array diujung
if(size % 3 == 1){
for(int i = 0; i< matrixGambarMod1R.length-1;i=i+3){
//merah
matrixGambarMod1R[i]= (M_inverse[0][0]* matrixGambarMod1R[i]+
M_inverse[0][1]* matrixGambarMod1R[i+1]+
M_inverse[0][2]* matrixGambarMod1R[i+2]) % 256;
matrixGambarMod1R[i+1]= (M_inverse[1][0]* matrixGambarMod1R[i]+
M_inverse[1][1]* matrixGambarMod1R[i+1]+
M_inverse[1][2]* matrixGambarMod1R[i+2]) % 256;
matrixGambarMod1R[i+2]= (M_inverse[2][0]* matrixGambarMod1R[i]+
M_inverse[2][1]* matrixGambarMod1R[i+1]+
M_inverse[2][2]* matrixGambarMod1R[i+2]) % 256;
//hijau
matrixGambarMod1G[i]= (M_inverse[0][0]* matrixGambarMod1G[i]+
M_inverse[0][1]* matrixGambarMod1G[i+1]+
M_inverse[0][2]* matrixGambarMod1G[i+2]) % 256;
matrixGambarMod1G[i+1]= (M_inverse[1][0]* matrixGambarMod1G[i]+
M_inverse[1][1]* matrixGambarMod1G[i+1]+
M_inverse[1][2]* matrixGambarMod1G[i+2]) % 256;
matrixGambarMod1G[i+2]= (M_inverse[2][0]* matrixGambarMod1G[i]+
M_inverse[2][1]* matrixGambarMod1G[i+1]+
M_inverse[2][2]* matrixGambarMod1G[i+2]) % 256;
//biru
matrixGambarMod1B[i]= (M_inverse[0][0]* matrixGambarMod1B[i]+
M_inverse[0][1]* matrixGambarMod1B[i+1]+
M_inverse[0][2]* matrixGambarMod1B[i+2]) % 256;
matrixGambarMod1B[i+1]= (M_inverse[1][0]* matrixGambarMod1B[i]+
M_inverse[1][1]* matrixGambarMod1B[i+1]+
M_inverse[1][2]* matrixGambarMod1B[i+2]) % 256;
matrixGambarMod1B[i+2]= (M_inverse[2][0]* matrixGambarMod1B[i]+
M_inverse[2][1]* matrixGambarMod1B[i+1]+
M_inverse[2][2]* matrixGambarMod1B[i+2]) % 256;
}
for (int i = 0; i< size;i++){
gambarR[i] = matrixGambarMod1R[i];
gambarG[i] = matrixGambarMod1G[i];
gambarB[i] = matrixGambarMod1B[i];
}
}
if(size % 3 == 0){
matrixGambarMod1R = gambarR;
matrixGambarMod1G = gambarG;
matrixGambarMod1B = gambarB;
for(int i = 0; i< matrixGambarMod1R.length;i=i+3){
//merah
matrixGambarMod1R[i]= (M_inverse[0][0]* matrixGambarMod1R[i]+
M_inverse[0][1]* matrixGambarMod1R[i+1]+
M_inverse[0][2]* matrixGambarMod1R[i+2]) % 256;
matrixGambarMod1R[i+1]= (M_inverse[1][0]* matrixGambarMod1R[i]+
M_inverse[1][1]* matrixGambarMod1R[i+1]+
M_inverse[1][2]* matrixGambarMod1R[i+2]) % 256;
matrixGambarMod1R[i+2]= (M_inverse[2][0]* matrixGambarMod1R[i]+
M_inverse[2][1]* matrixGambarMod1R[i+1]+
M_inverse[2][2]* matrixGambarMod1R[i+2]) % 256;
//hijau
matrixGambarMod1G[i]= (M_inverse[0][0]* matrixGambarMod1G[i]+
M_inverse[0][1]* matrixGambarMod1G[i+1]+
M_inverse[0][2]* matrixGambarMod1G[i+2]) % 256;
matrixGambarMod1G[i+1]= (M_inverse[1][0]* matrixGambarMod1G[i]+
M_inverse[1][1]* matrixGambarMod1G[i+1]+
M_inverse[1][2]* matrixGambarMod1G[i+2]) % 256;
matrixGambarMod1G[i+2]= (M_inverse[2][0]* matrixGambarMod1G[i]+
M_inverse[2][1]* matrixGambarMod1G[i+1]+
M_inverse[2][2]* matrixGambarMod1G[i+2]) % 256;
//biru
matrixGambarMod1B[i]= (M_inverse[0][0]* matrixGambarMod1B[i]+
M_inverse[0][1]* matrixGambarMod1B[i+1]+
M_inverse[0][2]* matrixGambarMod1B[i+2]) % 256;
matrixGambarMod1B[i+1]= (M_inverse[1][0]* matrixGambarMod1B[i]+
M_inverse[1][1]* matrixGambarMod1B[i+1]+
M_inverse[1][2]* matrixGambarMod1B[i+2]) % 256;
matrixGambarMod1B[i+2]= (M_inverse[2][0]* matrixGambarMod1B[i]+
M_inverse[2][1]* matrixGambarMod1B[i+1]+
M_inverse[2][2]* matrixGambarMod1B[i+2]) % 256;
}
for (int i = 0; i< size;i++){
gambarR[i] = matrixGambarMod1R[i];
gambarG[i] = matrixGambarMod1G[i];
gambarB[i] = matrixGambarMod1B[i];
}
}
int l = 0;
for(int i = 0; i<bmp.getWidth(); i++){
for(int j = 0; j<bmp.getHeight();j++){
operation.setPixel(i, j, Color.rgb(gambarR[l], gambarG[l], gambarB[l]));
l++;
}
}
ivImage.setImageBitmap(operation);
}
public int determinant(int[][] A){
int res;
res = A[0][0]*A[1][1]*A[2][2] + A[0][1]*A[1][2]*A[2][0] + A[0][2]*A[1][0]*A[2][1] - A[2][0]*A[1][1]*A[0][2] - A[2][1]*A[1][2]*A[0][0] - A[2][2]*A[1][0]*A[0][1];
return res;
}
public int[][] getInverse(int key[][], int mi){
int i,j;
int[][] kofaktor = new int[3][3];
int[][] key_inv = new int[3][3];
int d = determinant(M_key);
mi = mi(d % 256);
if (mi<0)
mi += 256;
kofaktor[0][0] = (key[1][1]*key[2][2]-key[1][2]*key[2][1]);
kofaktor[0][1] = -1 * (key[1][0]*key[2][2]-key[1][2]*key[2][0]);
kofaktor[0][2] = (key[1][0]*key[2][1]-key[1][1]*key[2][0]);
kofaktor[1][0] = -1 * (key[0][1]*key[2][2]-key[0][2]*key[2][1]);
kofaktor[1][1] = (key[0][0]*key[2][2]-key[0][2]*key[2][0]);
kofaktor[1][2] = -1 * (key[0][0]*key[2][1]-key[0][1]*key[2][0]);
kofaktor[2][0] = (key[0][1]*key[1][2]-key[0][2]*key[1][1]);
kofaktor[2][1] = -1 * (key[0][0]*key[1][2]-key[0][2]*key[1][0]);
kofaktor[2][2] = (key[0][0]*key[1][1]-key[0][1]*key[1][0]);
key_inv[0][0] = ((kofaktor[0][0]));
key_inv[0][1] = ((kofaktor[1][0]));
key_inv[0][2] = ((kofaktor[2][0]));
key_inv[1][0] = ((kofaktor[0][1]));
key_inv[1][1] = ((kofaktor[1][1]));
key_inv[1][2] = ((kofaktor[2][1]));
key_inv[2][0] = ((kofaktor[0][2]));
key_inv[2][1] = ((kofaktor[1][2]));
key_inv[2][2] = ((kofaktor[2][2]));
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (key_inv[i][j] < 0)
key_inv[i][j] += 256;
key_inv[i][j] *= mi;
key_inv[i][j] %= 256;
}
}
return key_inv;
}
public int mi(int d)
{
int q,r1,r2,r,t1,t2,t;
r1=256;
r2=d;
t1=0;
t2=1;
while(r1!=1&&r2!=0)
{
q=r1/r2;
r=r1%r2;
t=t1-(t2*q);
r1=r2;
r2=r;
t1=t2;
t2=t;
}
return (t1+t2);
}
}