관리 메뉴

Heizelnut의 IT이야기

이미지 암호화처리 본문

IT_Engineer/JAVA

이미지 암호화처리

Heizelnut 2013.02.22 21:39

암호화 처리할때 

    private static final String algorithm = "AES";

private static final String transformation = algorithm + "/ECB/PKCS5Padding";

private Key key;


public FileCoder(Key key) {

this.key = key;

}


public void encrypt(File source, File dest) throws Exception {

crypt(Cipher.ENCRYPT_MODE, source, dest);

}


private void crypt(int mode, File source, File dest) throws Exception {

Cipher cipher = Cipher.getInstance(transformation);

cipher.init(mode, key);

InputStream input = null;

OutputStream output = null;

try {

input = new BufferedInputStream(new FileInputStream(source));

output = new BufferedOutputStream(new FileOutputStream(dest));

byte[] buffer = new byte[1024];

int read = -1;

while ((read = input.read(buffer)) != -1) {

output.write(cipher.update(buffer, 0, read));

}

output.write(cipher.doFinal());

} finally {

if (output != null) {

try { output.close(); } catch(IOException ie) {}

}

if (input != null) {

try { input.close(); } catch(IOException ie) {}

}

}

}

public static void main(String[] args) throws Exception {

// 128비트의 키



SecretKeySpec key = new SecretKeySpec(toBytes("key값을 넣어요~~", 16), algorithm);

FileCoder coder = new FileCoder(key);

coder.encrypt(new File("D:/a.jpg"), new File("D:/aa.jpg"));

}

 


안드로이드에서 암호해독할때 





public class AndroidTestSecurityActivity extends Activity {

    /** Called when the activity is first created. */

private static final String algorithm = "AES";

private static final String transformation = algorithm + "/ECB/PKCS5Padding";

Button button;

ImageView imageview;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        button = (Button)findViewById(R.id.button);

        imageview= (ImageView)findViewById(R.id.image);

      

button.setOnTouchListener(new OnTouchListener(){

public boolean onTouch(View view, MotionEvent event) {

// TODO Auto-generated method stub

int eventaction = event.getAction();

switch(eventaction){

case MotionEvent.ACTION_DOWN : 

return true;

case MotionEvent.ACTION_MOVE :

return true;

case MotionEvent.ACTION_UP : 


   SecretKeySpec key = new SecretKeySpec(toBytes("696d697373796f7568616e6765656e61", 16),algorithm);

FileCoder coder = new FileCoder(key);

try {

//coder.encrypt(new File("D:/a.jpg"), new File("D:/aa.jpg"));

coder.decrypt(new File("/sdcard/aa.jpg"));

} catch (Exception e) {

// TODO: handle exception

}

return true;

}

return false;

}

});

   }



public static byte[] toBytes(String digits, int radix) throws IllegalArgumentException, NumberFormatException {

if (digits == null) {

return null;

}

if (radix != 16 && radix != 10 && radix != 8) {

throw new IllegalArgumentException("For input radix: \"" + radix + "\"");

}

int divLen = (radix == 16) ? 2 : 3;

    int length = digits.length();

    if (length % divLen == 1) {

    throw new IllegalArgumentException("For input string: \"" + digits + "\"");

    }

    length = length / divLen;

    byte[] bytes = new byte[length];

    for (int i = 0; i < length; i++) {

    int index = i * divLen;

    bytes[i] = (byte)(Short.parseShort(digits.substring(index, index+divLen), radix));

    }

    return bytes;

}

    

    

    

    public class FileCoder {

private Key key;


    public FileCoder(Key key) {

    this.key = key;

    }

   

    public void decrypt(File source) throws Exception {

    crypt(Cipher.DECRYPT_MODE, source);

    }

    private void crypt(int mode, File source) throws Exception {

    Cipher cipher = Cipher.getInstance(transformation);

    cipher.init(mode, key);

    InputStream input = null;

    byte[] aa = new byte[1024*1024];

   

    int i=0;

    try {

    input = new BufferedInputStream(new FileInputStream(source));

    byte[] buffer = new byte[1024];

    int read = -1;

    while ((read = input.read(buffer)) != -1) {

    byte[] vbuffer = cipher.update(buffer, 0, read);

    for(int j=0 ; j<vbuffer.length; j++){

    aa[i] = vbuffer[j];

    i++;

    }

   

    }

    byte[] vbuffer =  cipher.doFinal();

    for(int j=0 ; j<vbuffer.length; j++){

    aa[i] = vbuffer[j];

    i++;

    }  

    i=0;

    while(buffer[i] != -1){

    System.out.println(aa[i]);

    i++;

    }    

    System.out.println(aa);

    InputStream is = new ByteArrayInputStream(aa);

    Bitmap bitmap = BitmapFactory.decodeStream(is);

    imageview.setImageBitmap(bitmap);

    } finally {


    if (input != null) {

    try { input.close(); } catch(IOException ie) {}

    }

    }

    }


    }

}

신고
0 Comments
댓글쓰기 폼