Кода -копия выглядит следующим образом:
импортировать java.io.bytearrayinputstream;
импортировать java.io.bytearrayoutputstream;
Импорт java.io.file;
импортировать java.io.fileinputstream;
Импорт java.io.fileOutputStream;
импортировать java.io.inputstream;
импортировать java.io.outputstream;
Импорт java.security.securerandom;
Импорт javax.crypto.cipher;
импортировать javax.crypto.cipherinputstream;
Импорт javax.crypto.secretkey;
импортировать javax.crypto.secretkeyfactory;
импортировать javax.crypto.spec.deskeyspec;
Импорт javax.crypto.spec.ivparameterspec;
открытый класс destool {
частная статическая финальная строка PassKey = "afasdf";
частная статическая конечная строка deskey = "asfsdfsdf";
/**
* @Comments: шифровать файл
* @param filepath filepath, чтобы зашифровать
* @param файл файла файла файла
* @param режим режима
* @возвращаться
*/
Public Static String Encoderordecoder (String filePath, String FileName, Int Mode) {
InputStream IS = null;
OutputStream Out = null;
Cipherinptstream cis = null;
пытаться {
Securerandom sr = new Securerandom ();
Deskeyspec dks = new deskeyspec (deskey.getbytes ());
SecretKeyFactory KeyFactory = secretKeyFactory.getInstance ("des");
SecretKey SecureKey = keyFactory.GenerateCret (DKS);
Ivparameterspec iv = new ivparameterspec (passkey.getbytes ());
Шифр Cipher = cipher.getInstance ("des/cbc/pkcs5padding");
cipher.init (mode, secureKey, iv, sr);
File encoderfile = new File (filePath + file.separator + "encoder");
if (! encoderfile.exists ()) {
encoderfile.mkdir ();
}
is = new FileInputStream (filePath + file.separator + имя файла);
out = new FileOutputStream (filePath + file.separator + "Encoder"
+ File.separator + имя файла);
cis = new CipherinputStream (IS, шифр);
Byte [] buffer = новый байт [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
} catch (Exception e) {
e.printstacktrace ();
} окончательно {
пытаться {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (Exception e1) {
}
}
return filepath + file.separator + "encoder" + file.separator
+ имя файла;
}
/**@Комментарии: зашифровать струны
* @param SRC Source String
* @param режим режима
* @возвращаться
*/
Public Static String EncoderordeCoder (String Src, Int Mode) {
String Tag = "";
InputStream IS = null;
OutputStream Out = null;
Cipherinptstream cis = null;
пытаться {
Securerandom sr = new Securerandom ();
Deskeyspec dks = new deskeyspec (deskey.getbytes ());
SecretKeyFactory KeyFactory = secretKeyFactory.getInstance ("des");
SecretKey SecureKey = keyFactory.GenerateCret (DKS);
Ivparameterspec iv = new ivparameterspec (passkey.getbytes ());
Шифр Cipher = cipher.getInstance ("des/cbc/pkcs5padding");
cipher.init (mode, secureKey, iv, sr);
cis = new CipherinputStream (New BytearrayinputStream (src.getbytes ()), шифр);
out = new BytearrayOutputStream ();
Byte [] buffer = новый байт [1024];
int r;
while ((r = cis.read (buffer))> 0) {
out.write (buffer, 0, r);
}
tag = out.tostring ();
} catch (Exception e) {
e.printstacktrace ();
} окончательно {
пытаться {
if (is! = null) {
is.close ();
}
if (cis! = null) {
cis.close ();
}
if (out! = null) {
out.close ();
}
} catch (Exception e1) {
}
}
вернуть тег;
}
public static void main (string [] args) {
System.out.println ("AAA");
String t = encoderordecoder ("aaa", cipher.encrypt_mode);
System.out.println (t);
System.out.println (encoderordecoder (t, cipher.decrypt_mode));
}
}