日期:2014-05-20  浏览次数:20946 次

请好心人帮忙解决下这个DES加密解密算法。
对于DES加密解密本人不是很熟悉,不过因为项目前期.net端用的是这个,所以android端也得用这个算法来加密解密了。
.net端的加密解密算法如下:
   
  private static byte[] key = { 0x01, 0x9E, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEE };
  private static byte[] iv = { 0x00, 0x01, 0x00, 0x00, 0xEF, 0x00, 0x00, 0x01 };
  #region 自定义加密算法
  public static string encrypt(string str)
  {
  byte[] byteArrayInput = Encoding.UTF8.GetBytes(str);
  byte[] byteArrayOutput;
  StringBuilder strBuilderOutput = new StringBuilder(1000);
  string strOutput;

  DES des = DES.Create();
  des.Key = key;
  des.IV = iv;

  MemoryStream ms = new MemoryStream();
  CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);

  cs.Write(byteArrayInput, 0, byteArrayInput.Length);
  cs.FlushFinalBlock();
  byteArrayOutput = ms.ToArray();
  for (int i = 0; i < byteArrayOutput.Length; i++)
  {
  strBuilderOutput.AppendFormat("{0:X2}", byteArrayOutput[i]);
  }
  strOutput = strBuilderOutput.ToString();

  des.Clear();
  return strOutput;
  }
  //自定义解密算法
  public static string decrypt(string str)
  {
  byte[] byteArrayInput = new byte[str.Length / 2];
  byte[] byteArrayOutput;
  string strOutput;

  try
  {
  for (int i = 0; i < str.Length / 2; i++)
  {
  int x = Convert.ToInt32(str.Substring(2 * i, 2), 16);
  byteArrayInput[i] = (byte)x;
  }
  }
  catch
  {
  return null;
  }

  DES des = DES.Create();
  des.Key = key;
  des.IV = iv;

  MemoryStream ms = new MemoryStream();
  CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

  cs.Write(byteArrayInput, 0, byteArrayInput.Length);
  cs.FlushFinalBlock();
  byteArrayOutput = ms.ToArray();
  strOutput = Encoding.UTF8.GetString(byteArrayOutput);

  des.Clear();

  return strOutput;
  }
  #endregion


现在就是在android端不知道怎么改成通用的DES加密解密算法,哪位朋友有相关的经验或者思路的,麻烦指点下,不甚感激。

------解决方案--------------------
Java code
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

import biz.source_code.base64Coder.*;

/**
 * @author Administrator
 * 
 */
public class TripleDEService {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

    private static String strkey = "d+KuEICNtJsujXBH23MU6AjAjheQhpqJ";
    private static String Algorithm = "desede/ECB/PKCS5Padding";

    /**
     * 加密String明文输入,String密文输出
     * 
     * @param input
     * @return
     */
    public static String DesEncrypt(String input) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        // BASE64Encoder base64en = new BASE64Encoder();
        try {
            byteMing = input.getBytes("UTF-8");
            byte[] key = Base64Coder.decode(strkey); // BASE64Decoder().decodeBuffer(strkey);
            byteMi = getEncCode(byteMing, key);
            strMi = Base64Coder.encodeLines(byteMi); // base64en.encode(byteMi);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // base64en = null;
            byteMing = null;
            byteMi = null;
        }
        return strMi;
    }

    /**
     * 加密以byte[]明文输入,byte[]密文输出
     * 
     * @param byteS
     * @return
     */
    private static byte[] getEncCode(byte[] byteS, byte[] key) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(key);
            SecretKeyFactory keyfactory = SecretKeyFactory
                    .getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.ENCRYPT_MODE, deskey);
            byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;
    }

    /**
     * 解密 以String密文输入,String明文输出
     * 
     * @param input
     * @return
     */
    public static String DesDecrypt(String input) {
        // BASE64Decoder base64De = new BASE64Decoder();
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
            byteMi = Base64Coder.decode(input); // base64De.decodeBuffer(input);
            byte[] key = Base64Coder.decode(strkey); // new
            // BASE64Decoder().decodeBuffer(strkey);
            byteMing = getDesCode(byteMi, key);
            strMing = new String(byteMing, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // base64De = null;
            byteMing = null;
            byteMi = null;
        }
        return strMing;
    }

    /**
     * 解密以byte[]密文输入,以byte[]明文输出
     * 
     * @param byteD
     * @return
     */
    private static byte[] getDesCode(byte[] byteD, byte[] key) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
            Key deskey = null;
            DESedeKeySpec spec = new DESedeKeySpec(key);
            SecretKeyFactory keyfactory = SecretKeyFactory
                    .getInstance("desede");
            deskey = keyfactory.generateSecret(spec);
            cipher = Cipher.getInstance(Algorithm);
            cipher.init(Cipher.DECRYPT_MODE, deskey);
            byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cipher = null;
        }
        return byteFina;

    }

}