마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 AES 암호화를 이용한 파일 암호화시 암호화파일 오류

0 추천
public class MainActivity extends Activity {
static int kill = 0;
String File_path = "/sdcard/";
FileList _FileList;
private static final String algorithm = "AES";
private static final String transformation = algorithm
+ "/ECB/PKCS5Padding";
private static String sKeyString = "";
static String path, file;
 
private static Key key;
 
public void FileCoder(Key key) {
this.key = key;
}
 
public void FileCoder(String p, String f) throws Exception {
// path = p;
// file = f;
SecretKeySpec key = new SecretKeySpec(toBytes(
"696d697373796f7568616e6765656e61", 16), algorithm);
FileCoder(key);
 
encrypt(new File(p + f), new File(p + "new" + f));
 
decrypt(new File(p + "new" + f), new File(p + "back" + f));
}
 
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;
}
 
/**
* <p>
* 원본 파일을 암호화해서 대상 파일을 만든다.
* </p>
* @param source
*            원본 파일
* @param dest
*            대상 파일
* @throws Exception
*/
public void encrypt(File source, File dest) throws Exception {
crypt(Cipher.ENCRYPT_MODE, source, dest);
}
 
/**
* <p>
* 원본 파일을 복호화해서 대상 파일을 만든다.
* </p>
* @param source
*            원본 파일
* @param dest
*            대상 파일
* @throws Exception
*/
public void decrypt(File source, File dest) throws Exception {
crypt(Cipher.DECRYPT_MODE, source, dest);
}
 
/**
* <p>
* 원본 파일을 암/복호화해서 대상 파일을 만든다.
* </p>
* @param mode
*            암/복호화 모드
* @param source
*            원본 파일
* @param dest
*            대상 파일
* @throws Exception
*/
private void crypt(int mode, File source, File dest)
throws Exception {
Cipher cipher = Cipher.getInstance("AES");
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;
Toast.makeText(this, "" +input.read(buffer), Toast.LENGTH_LONG)
.show();
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) {
}
}
}
}
 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
 
_FileList = new FileList(this);
 
_FileList.setOnPathChangedListener(new OnPathChangedListener() {
@Override
public void onChanged(String path) {
// TODO Auto-generated method stub
((TextView) findViewById(R.id.FilePath)).setText("경로: " + path);
File_path = path;
}
});
 
_FileList.setOnFileSelected(new OnFileSelectedListener() {
@Override
public void onSelected(String path, String fileName) {
// TODO Auto-generated method stub
((TextView) findViewById(R.id.FilePath)).setText("경로: " + path
+ fileName);
try {
FileCoder(path, fileName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
 
_FileList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
 
return true;
}
});
 
LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout01);
layout.addView(_FileList);
 
_FileList.setPath(File_path);
_FileList.setFocusable(true);
_FileList.setFocusableInTouchMode(true);
}
 
안드로이드 AES 암호화를 이용한 파일 암호화하려고 만든 소스코드인데 암호화를 해서 파일을 생성하면 파일의 내용이 없어지는데 왜 그럴까요? 컴퓨터로 할때는 작동했었습니다.
ㅋ드로이드 (120 포인트) 님이 2014년 6월 15일 질문
ㅋ드로이드님이 2014년 6월 16일 수정

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...