java DES encryption

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.MessageDigest;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;


{class DESUtil public
/ **
* offset variable, representing a fixed 8-bit bytes
* /
Private IV_PARAMETER Final static String = "12345678";
/ **
* Key Algorithm
* /
Private static String the ALGORITHM = Final "the DES";
/ **
* encryption / decryption algorithm - operating mode - stuffing mode
* /
Private static String CIPHER_ALGORITHM = Final "the DES / the CBC / PKCS5Padding";
/ **
* default encoding
* /
Private Final static String the CHARSET = "UTF-. 8";

/**
* 生成key
*
* @param password
* @return
* @throws Exception
*/
private static Key generateKey(String password) throws Exception {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(password.getBytes("UTF-8"));
String encodestr = byte2Hex(messageDigest.digest());

DESKeySpec dks = new DESKeySpec(encodestr.getBytes(CHARSET));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
return keyFactory.generateSecret(dks);
}

/**
* 将byte转为16进制
*
* @param bytes
* @return
*/
private static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
// 1得到一位的进行补0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}

/ **
* the DES encrypted string
*
* @param password
* encrypted password, the length can not be less than 8
* @param Data
* to be encrypted string
* @return the encrypted content
* /
public static String the encrypt (password String, String Data ) {
IF (password == null || password.length () <. 8) {
the throw a RuntimeException new new ( "encryption fails, key not less than 8");
}
IF (Data == null)
return null;
the try {
Key secretKey the generateKey = (password);
the cipher cipher = Cipher.getInstance (CIPHER_ALGORITHM);
IvParameterSpec IV = new new IvParameterSpec (IV_PARAMETER.getBytes (the CHARSET));
cipher.init (Cipher.ENCRYPT_MODE, secretKey, IV);
byte [] bytes = cipher. doFinal (data.getBytes (CHARSET));

// JDK1.8 and above can be used directly Base64, JDK1.7 and below can be used Base64Encoder
// Android platform can be used android.util.Base64
return new new String (Base64.getEncoder () encode (bytes).);

}
catch (Exception e) {
e.printStackTrace();
return data;
}
}

/ **
* the DES decryption string
*
* @param password
* decrypt password, the length can not be less than 8
* @param Data
* to be decrypted string
* @return the decrypted content
* /
public static String the decrypt (String password, String Data ) {
IF (password == null || password.length () <. 8) {
the throw a RuntimeException new new ( "encryption fails, key not less than 8");
}
IF (Data == null)
return null;
the try {
Key secretKey the generateKey = (password);
the cipher cipher = Cipher.getInstance (CIPHER_ALGORITHM);
IvParameterSpec IV = new new IvParameterSpec (IV_PARAMETER.getBytes (the CHARSET));
cipher.init (Cipher.DECRYPT_MODE, secretKey, IV);
return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(CHARSET))), CHARSET);
}
catch (Exception e) {
e.printStackTrace();
return data;
}
}

/ **
* the DES encrypted files
*
* @param srcFile
* files to be encrypted
* @param the destFile
* encrypted stored file path
* @return the encrypted file path
* /
public static String EncryptFile (String password, srcFile String, String destFile) {

if (password == null || password.length() < 8) {
throw new RuntimeException("加密失败,key不能小于8位");
}
try {
IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, generateKey(password), iv);
InputStream is = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(destFile);
CipherInputStream cis = new CipherInputStream(is, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = cis.read(buffer)) > 0) {
out.write(buffer, 0, r);
}
cis.close();
is.close();
out.close();
return destFile;
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

/ **
* DES decrypt files
*
* @param srcFile
* encrypted files
* @param the destFile
* decrypted file storage path
* @return the file path after the decryption
* /
public static String DecryptFile (String password, String srcFile, String the destFile) {
IF (password == null || password.length () <. 8) {
the throw a RuntimeException new new ( "encryption fails, key not less than 8");
}
the try {
File File = new new File (the destFile);
IF ( ! File.Exists ()) {
file.getParentFile () mkdirs ();.
file.createNewFile ();
}
IvParameterSpec IV = new new IvParameterSpec (IV_PARAMETER.getBytes (the CHARSET));
the cipher cipher = Cipher.getInstance (CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, generateKey(password), iv);
InputStream is = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(destFile);
CipherOutputStream cos = new CipherOutputStream(out, cipher);
byte[] buffer = new byte[1024];
int r;
while ((r = is.read(buffer)) >= 0) {
cos.write(buffer, 0, r);
}
cos.close();
is.close();
out.close();
return destFile;
}
catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}

Guess you like

Origin www.cnblogs.com/RealWorld/p/12559584.html