일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 |
- windows 8.1 update1
- 네트워크
- 안드로이드 VPN
- portscan
- 프로그래밍
- C언어
- vpn
- 대구지법
- Android
- lightworks
- update1 leak
- 사이트우회
- 차단사이트 우회
- 안드로이드 스미싱
- 32자 제한
- 우분투 7.04
- 윈도우8.1 update1
- 안드로이드 사이트 우회
- Mac
- warrning.or.kr
- HAXM
- 포트스캔
- 보안
- windows 8.1
- 안드로이드
- 계정 32자
- 차단된사이트
- Linux
- 안드로이드 우회
- mysql 긴 계정
- Today
- 0
- Total
- 215,195
Heizelnut의 IT이야기
이미지 암호화처리 본문
암호화 처리할때
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) {}
}
}
}
}
}