코드 사본은 다음과 같습니다.
import java.io.bytearrayinputstream;
import java.io.BytearRayoutputStream;
import java.io.file;
import java.io.fileInputStream;
import java.io.fileoutputStream;
import java.io.inputstream;
import java.io.outputStream;
java.security.securerandom import;
javax.crypto.cipher 가져 오기;
import javax.crypto.cipherinputstream;
javax.crypto.secretkey import;
javax.crypto.secretkeyfactory import;
javax.crypto.spec.deskeyspec import;
import javax.crypto.spec.ivparameterspec;
공개 클래스 데스 툴 {
개인 정적 최종 문자열 passkey = "afasdf";
개인 정적 최종 문자열 deskey = "asfsdfsdf";
/**
* @comments : 파일을 암호화합니다
* @param filepath filepath는 암호화합니다
* @param filename 파일
* @param 모드 암호화 모드 암호화 : cipher.encrypt_mode 암호 해독 : cipher.decrypt_mode
* @반품
*/
public static string encoderordecoder (String FilePath, String filename, int mode) {
입력 스트림은 = null입니다.
outputStream out = null;
cipherinputstream cis = null;
노력하다 {
securerandom sr = new securerandom ();
deskeyspec dks = 새로운 deskeyspec (deskey.getBytes ());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "des");
Secretkey SecureKey = keyfactory.generatesEcret (DKS);
ivparameterspec iv = 새로운 ivparameterspec (passkey.getBytes ());
Cipher Cipher = cipher.getinstance ( "des/cbc/pkcs5padding");
cipher.init (모드, Securekey, IV, SR);
File EncoderFile = 새 파일 (FilePath + file.separator + "encoder");
if (! encoderfile.exists ()) {
encoderfile.mkdir ();
}
is = new FileInputStream (FilePath + file.separator + filename);
out = new FileOutputStream (FilePath + file.separator + "Encoder"
+ file.separator + filename);
cis = 새로운 암호화 푸트 스트림 (IS, 암호);
바이트 [] 버퍼 = 새로운 바이트 [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (버퍼, 0, r);
}
} catch (예외 e) {
e.printstacktrace ();
} 마지막으로 {
노력하다 {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (예외 e1) {
}
}
return filepath + file.separator + "encoder" + file.separator
+ filename;
}
/**@댓글 : 문자열을 암호화합니다
* @param src 소스 문자열
* @param 모드 암호화 모드 암호화 : cipher.encrypt_mode 암호 해독 : cipher.decrypt_mode
* @반품
*/
public static string encoderordecoder (문자열 src, int mode) {
문자열 태그 = "";
입력 스트림은 = null입니다.
outputStream out = null;
cipherinputstream cis = null;
노력하다 {
securerandom sr = new securerandom ();
deskeyspec dks = 새로운 deskeyspec (deskey.getBytes ());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance ( "des");
Secretkey SecureKey = keyfactory.generatesEcret (DKS);
ivparameterspec iv = 새로운 ivparameterspec (passkey.getBytes ());
Cipher Cipher = cipher.getinstance ( "des/cbc/pkcs5padding");
cipher.init (모드, Securekey, IV, SR);
CIS = 새로운 CIPHERINPUTSTREAM (New BytearRayinputStream (Src.getBytes ()), 암호);
out = new BytearRayoutputStream ();
바이트 [] 버퍼 = 새로운 바이트 [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (버퍼, 0, r);
}
tag = out.toString ();
} catch (예외 e) {
e.printstacktrace ();
} 마지막으로 {
노력하다 {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (예외 e1) {
}
}
반환 태그;
}
public static void main (String [] args) {
System.out.println ( "AAA");
문자열 t = encoderordeCoder ( "aaa", cipher.encrypt_mode);
System.out.println (t);
System.out.println (encoderordeCoder (t, cipher.decrypt_mode));
}
}