File encryption and string encryption using the Java self-band des encryption algorithm _java

Source: Internet
Author: User
Tags return tag

Copy Code code as follows:

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;
Import Java.security.SecureRandom;
Import Javax.crypto.Cipher;
Import Javax.crypto.CipherInputStream;
Import Javax.crypto.SecretKey;
Import Javax.crypto.SecretKeyFactory;
Import Javax.crypto.spec.DESKeySpec;
Import Javax.crypto.spec.IvParameterSpec;
public class Destool {
private static final String passkey = "AFASDF";
private static final String Deskey = "ASFSDFSDF";
/**
* @Comments: Encrypt files
* @param filePath the file path to encrypt
* @param filename File
* @param mode encryption mode encryption: Cipher.encrypt_mode decryption: Cipher.decrypt_mode
* @return
*/
public static string Encoderordecoder (String FilePath, string fileName, int mode) {

InputStream is = null;
OutputStream out = null;
CipherInputStream cis = NULL;

try {
securerandom sr = new SecureRandom ();
Deskeyspec DKs = new Deskeyspec (Deskey.getbytes ());
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Secretkey SecureKey = Keyfactory.generatesecret (DKS);
Ivparameterspec IV = new Ivparameterspec (Passkey.getbytes ());
Cipher 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 + fileName);
out = new FileOutputStream (FilePath + file.separator + "encoder"
+ File.separator + fileName);

CIS = new CipherInputStream (IS, cipher);
byte[] buffer = new byte[1024];
int R;
while ((r = cis.read (buffer)) > 0) {
Out.write (buffer, 0, R);
}

  } catch (Exception e) {
   e.printstacktrace ();
 &NBSP} finally {
   try {
    if (is!= null) {
      is.close ();
    }
    if (cis!= null) {
     cis.close ();
   &NBSP}
    if (out!= null) {
     out.close ();
    }
   } catch (Exception E1) {

}
}
Return FilePath + file.separator + "encoder" + file.separator
+ FileName;
}

/** @Comments: Encrypt strings
* @param src Source string
* @param mode encryption mode encryption: Cipher.encrypt_mode decryption: Cipher.decrypt_mode
* @return
*/
public static string Encoderordecoder (String src, int mode) {
String tag= "";
InputStream is = null;
OutputStream out = null;
CipherInputStream cis = NULL;

try {
securerandom sr = new SecureRandom ();
Deskeyspec DKs = new Deskeyspec (Deskey.getbytes ());
Secretkeyfactory keyfactory = secretkeyfactory.getinstance ("DES");
Secretkey SecureKey = Keyfactory.generatesecret (DKS);
Ivparameterspec IV = new Ivparameterspec (Passkey.getbytes ());
Cipher Cipher = cipher.getinstance ("des/cbc/pkcs5padding");
Cipher.init (mode, SecureKey, IV, SR);
CIS = new CipherInputStream (New Bytearrayinputstream (Src.getbytes ()), cipher);
Out=new Bytearrayoutputstream ();
byte[] buffer = new byte[1024];
int R;
while ((r = cis.read (buffer)) > 0) {
Out.write (buffer, 0, R);
}
Tag=out.tostring ();
catch (Exception e) {
E.printstacktrace ();
finally {
try {
if (is!= null) {
Is.close ();
}
if (CIS!= null) {
Cis.close ();
}
if (out!= null) {
Out.close ();
}
catch (Exception E1) {

}
}
return tag;
}
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));
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.