现在需要一份Java版的3DES加密。
但是对方给的是一份C#版本的。
求哪个大神帮忙转化一下。
或者告知C#代码的思路。
key是9762D45A7F5F49FDA284FB686A4F99B9
向量是01,02,03,04,05,06,07,08
public static string EncryptSP(string toEncryptStr, string key, string iv)
{
string ErrorDesc = "";
if (toEncryptStr == "") return "";
try
{
if (key.Length < 24)
{
key = key.PadRight(24, '1');
}
byte[] Encrypted, ivArray, keyArray;
// 获取加密向量和CP密钥
ivArray = CryptographyUtility.HexStringToByteArray(iv);
keyArray = Encoding.Default.GetBytes(key);
if (EncryptSP(keyArray, ivArray, ConvertStringToByteArray(toEncryptStr), out Encrypted))
{
return ByteArrayToHexString(Encrypted);
}
else
return "";
}
catch (Exception e)
{
ErrorDesc = e.Message;
throw new Exception("CommonEncrypt(使用固定Key对字符串进行加密)-》" + ErrorDesc);
}
catch
{
ErrorDesc = CryptogramErrorDefinition.E_Cryptogram_SYSTEM_ERROR_DESC;
throw new Exception("CommonEncrypt(使用固定Key对字符串进行加密)-》" + ErrorDesc);
}
}
/// <summary>
/// 将16进制字符串转换成byte数组
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static 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;
}
/// <summary>
/// 字符转换成16显示
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static byte chr2hex(string chr)
{
switch (chr)
{
case "0":
return 0x00;
case "1":
return 0x01;
case "2":
return 0x02;
case "3":
return 0x03;
case "4":
return 0x04;
case "5":
return 0x05;
case "6":
return 0x06;
case "7":
return 0x07;
case "8":
return 0x08;
case "9":
return 0x09;
case "A":
return 0x0a;
case "B":
return 0x0b;
case "C":
return 0x0c;
case "D":
return 0x0d;
case "E":
return 0x0e;
case "F":
return 0x0f;
}
return 0x00;
}
/// <summary>
/// 加密(3DES)
/// </summary>
/// <param name="KEY">加密密钥</param>
/// <param name="IV">加密向量</param>
/// <param name="TobeEncrypted">加密字符串</param>
/// <param name="Encrypted">加密后Byte数组</param>
/// <returns></returns>
public static bool EncryptSP(byte[] KEY, byte[] IV, byte[] TobeEncrypted, out byte[] Encrypted)
{
string ErrorDesc = "";
if (KEY == null || IV == null)
throw new Exception("Encrypt(加密)-》" + ErrorDesc);
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];
}
//tridesencrypt.Dispose();
des3.Mode = CipherMode.ECB;
ICryptoTransform tridesencrypt = des3.CreateEncryptor(tmpkey, tmpiv);
//tridesencrypt = des.CreateEncryptor(KEY,tmpiv);
Encrypted = tridesencrypt.TransformFinalBlock(TobeEncrypted, 0, TobeEncrypted.Length);
//tridesencrypt.Dispose();
des3.Clear();
return true;
}
catch (Exception e)
{
ErrorDesc = e.Message;
throw new Exception("Encrypt(加密)-》" + ErrorDesc);
}
catch
{
ErrorDesc = CryptogramErrorDefinition.E_Cryptogram_SYSTEM_ERROR_DESC;
throw new Exception("Encrypt(加密)-》" + ErrorDesc);
}
}
public static byte[] ConvertStringToByteArray(String s)
{
return System.Text.Encoding.GetEncoding("utf-8").GetBytes(s);//gb2312
}
public static string ByteArrayToHexString(byte[] buf)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < buf.Length; i++)
{
sb.Append(buf[i].ToString("X").Length == 2 ? buf[i].ToString("X") : "0" + buf[i].ToString("X"));
}
return sb.ToString();
}