熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

JAVA DES加密解密實現

2013-11-23 18:42:21  來源: Java核心技術 

  package comtxltest;

  import javasecuritySecureRandom;

  import javaxcryptoCipher;

  import javaxcryptoSecretKey;

  import javaxcryptoSecretKeyFactory;

  import javaxcryptospecDESKeySpec;

  /**

  * DES加解密支持與delphi交互(字符串編碼需統一為UTF)

  *

  * @author wym

  */

  public class DESCipherCrossoverDelphi {

  /**

  * 密鑰

  */

  public static final String KEY = uGquZ;

  private final static String DES = DES;

  /**

  * 加密

  *

  * @param src

  *            明文(字節)

  * @param key

  *            密鑰長度必須是的倍數

  * @return 密文(字節)

  * @throws Exception

  */

  public static byte[] encrypt(byte[] src byte[] key) throws Exception {

  // DES算法要求有一個可信任的隨機數源

  SecureRandom sr = new SecureRandom();

  // 從原始密匙數據創建DESKeySpec對象

  DESKeySpec dks = new DESKeySpec(key);

  // 創建一個密匙工廠然後用它把DESKeySpec轉換成

  // 一個SecretKey對象

  SecretKeyFactory keyFactory = SecretKeyFactorygetInstance(DES);

  SecretKey securekey = keyFactorygenerateSecret(dks);

  // Cipher對象實際完成加密操作

  Cipher cipher = CiphergetInstance(DES);

  // 用密匙初始化Cipher對象

  cipherinit(CipherENCRYPT_MODE securekey sr);

  // 現在獲取數據並加密

  // 正式執行加密操作

  return cipherdoFinal(src);

  }

  /**

  * 解密

  *

  * @param src

  *            密文(字節)

  * @param key

  *            密鑰長度必須是的倍數

  * @return 明文(字節)

  * @throws Exception

  */

  public static byte[] decrypt(byte[] src byte[] key) throws Exception {

  // DES算法要求有一個可信任的隨機數源

  SecureRandom sr = new SecureRandom();

  // 從原始密匙數據創建一個DESKeySpec對象

  DESKeySpec dks = new DESKeySpec(key);

  // 創建一個密匙工廠然後用它把DESKeySpec對象轉換成

  // 一個SecretKey對象

  SecretKeyFactory keyFactory = SecretKeyFactorygetInstance(DES);

  SecretKey securekey = keyFactorygenerateSecret(dks);

  // Cipher對象實際完成解密操作

  Cipher cipher = CiphergetInstance(DES);

  // 用密匙初始化Cipher對象

  cipherinit(CipherDECRYPT_MODE securekey sr);

  // 現在獲取數據並解密

  // 正式執行解密操作

  return cipherdoFinal(src);

  }

  /**

  * 加密

  *

  * @param src

  *            明文(字節)

  * @return 密文(字節)

  * @throws Exception

  */

  public static byte[] encrypt(byte[] src) throws Exception {

  return encrypt(src KEYgetBytes());

  }

  /**

  * 解密

  *

  * @param src

  *            密文(字節)

  * @return 明文(字節)

  * @throws Exception

  */

  public static byte[] decrypt(byte[] src) throws Exception {

  return decrypt(src KEYgetBytes());

  }

  /**

  * 加密

  *

  * @param src

  *            明文(字符串)

  * @return 密文(進制字符串)

  * @throws Exception

  */

  public final static String encrypt(String src) {

  try {

  return bytehex(encrypt(srcgetBytes() KEYgetBytes()));

  } catch (Exception e) {

  eprintStackTrace();

  }

  return null;

  }

  /**

  * 解密

  *

  * @param src

  *            密文(字符串)

  * @return 明文(字符串)

  * @throws Exception

  */

  public final static String decrypt(String src) {

  try {

  return new String(decrypt(hexbyte(srcgetBytes()) KEYgetBytes()));

  } catch (Exception e) {

  eprintStackTrace();

  }

  return null;

  }

  /**

  * 加密

  *

  * @param src

  *            明文(字節)

  * @return 密文(進制字符串)

  * @throws Exception

  */

  public static String encryptToString(byte[] src) throws Exception {

  return encrypt(new String(src));

  }

  /**

  * 解密

  *

  * @param src

  *            密文(字節)

  * @return 明文(字符串)

  * @throws Exception

  */

  public static String decryptToString(byte[] src) throws Exception {

  return decrypt(new String(src));

  }

  public static String bytehex(byte[] b) {

  String hs = ;

  String stmp = ;

  for (int n = ; n < blength; n++) {

  stmp = (javalangIntegertoHexString(b[n] & XFF));

  if (stmplength() == )

  hs = hs + + stmp;

  else

  hs = hs + stmp;

  }

  return hstoUpperCase();

  }

  public static byte[] hexbyte(byte[] b) {

  if ((blength % ) != )

  throw new IllegalArgumentException(長度不是偶數);

  byte[] b = new byte[blength / ];

  for (int n = ; n < blength; n += ) {

  String item = new String(b n );

  b[n / ] = (byte) IntegerparseInt(item );

  }

  return b;

  }

  public static void main(String[] args) {

  try {

  String src = hello;

  String crypto = DESCipherCrossoverDelphiencrypt(src);

  Systemoutprintln(密文[ + src + ]: + crypto);

  Systemoutprintln(解密後:

  + DESCipherCrossoverDelphidecrypt(crypto));

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  ============================把文件進行解密加密===================================

  public static File encrypt(File file String path)

  {

  File EncFile = new File(path);

  if (!EncFileexists())

  try

  {

  EncFilecreateNewFile();

  }

  catch (Exception e)

  {

  eprintStackTrace();

  }

  try

  {

  FileInputStream fin = new FileInputStream(file);

  ByteArrayOutputStream bout = new ByteArrayOutputStream(finavailable());

  byte b[] = new byte[finavailable()];

  int n;

  while ((n = finread(b)) != )

  {

  byte temp[] = encrypt(b keygetBytes());

  boutwrite(temp templength);

  }

  finclose();

  boutclose();

  FileOutputStream fout = new FileOutputStream(EncFile);

  BufferedOutputStream buffout = new BufferedOutputStream(fout);

  buffoutwrite(bouttoByteArray());

  buffoutclose();

  foutclose();

  }

  catch (Exception e)

  {

  eprintStackTrace();

  }

  return EncFile;

  }

  public static File decrypt(File file String path)

  {

  File desFile = new File(path);

  if (!desFileexists())

  try

  {

  desFilecreateNewFile();

  }

  catch (Exception e)

  {

  eprintStackTrace();

  }

  try

  {

  FileInputStream fin = new FileInputStream(file);

  int i=finavailable()finavailable()%keylength();

  ByteArrayOutputStream bout = new ByteArrayOutputStream(i);

  byte b[] = new byte[i];

  int n;

  while((n = finread(b)) !=)

  {

  byte temp[] = decrypt(b keygetBytes());

  boutwrite(temp templength);

  }

  finclose();

  boutclose();

  FileOutputStream fout = new FileOutputStream(desFile);

  BufferedOutputStream buffout = new BufferedOutputStream(fout);

  buffoutwrite(bouttoByteArray());

  buffoutclose();

  foutclose();

  }

  catch (Exception e)

  {

  eprintStackTrace();

  }

  return desFile;

  }

  結合JAVAIOZipOutputStream 可以用來加密解密壓縮文件


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25606.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.