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

高分求将C# 3DES加密翻译成Java
C# code

public string Encrypt(string strTobeEnCrypted, string strKEY, string strIV)
        {
            if (strTobeEnCrypted == "") return "";
            try
            {
                byte[] pKEY = HexStringToByteArray(strKEY);
                byte[] pIV = HexStringToByteArray(strIV);
                byte[] Encrypted;
                if (Encrypt(pKEY, pIV, ConvertStringToByteArray(strTobeEnCrypted), out Encrypted))
                {
                    return ToBase64String(Encrypted);
                }
                else
                {
                    return "";
                }
            }
            catch
            { }
            return "";
        }
public byte[] HexStringToByteArray(string s)
        {
            Byte[] buf = new byte[s.Length / 2];
            for (int i = 0; i < buf.Length; i++)
            {
                buf[i] = (byte)(chr2hex(s.Substring(i * 2, 1)) * 0x10 + chr2hex(s.Substring(i * 2 + 1, 1)));
            }
            return buf;
        }
  private bool Encrypt(byte[] KEY, byte[] IV, byte[] TobeEncrypted, out  byte[] Encrypted)
        {
            Encrypted = null;
            try
            {
                byte[] tmpiv ={ 0, 1, 2, 3, 4, 5, 6, 7 };
                for (int ii = 0; ii < 8; ii++)
                {
                    tmpiv[ii] = IV[ii];
                }
                byte[] tmpkey ={ 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7 };
                for (int ii = 0; ii < 24; ii++)
                {
                    tmpkey[ii] = KEY[ii];
                }
private System.Security.Cryptography.TripleDESCryptoServiceProvider des;
ICryptoTransform tridesencrypt = des.CreateEncryptor(tmpkey, tmpiv);
Encrypted = tridesencrypt.TransformFinalBlock(TobeEncrypted, 0, TobeEncrypted.Length);
                des.Clear();
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }



------解决方案--------------------
不会,C#。

但帮你顶下帖子吧。
------解决方案--------------------
什么意思,你想把这段代码搞成java吗。是翻译代码还是翻译密码?
------解决方案--------------------
JCE中就有3DES,直接使用就是。这个连接就是个和.net交互的
如何用Java进行3DES加密解密
------解决方案--------------------
你这个代码是不是不全,缺少了几个关键的方法

ConvertStringToByteArray(string)

ToBase64String(string)


chr2hex(string)

System.Security.Cryptography.TripleDESCryptoServiceProvider des

也没有初始化,这个代码边在C#下都会报错,如何用java翻译??