请教如何将C#的加密转成java,急需答案,谢谢

public static string Md5DesEncryption(string toEncrypt, string key)
{
try
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); // 用Md5 加密约定的公共密钥
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); // 用DES算法再次加密明文
return Convert.ToBase64String(resultArray, 0, resultArray.Length).Replace("+", "").Replace("&","").Replace("?","").Replace("=",""); //替换密文中的特殊字符
}
catch
{

}
return string.Empty;

}

我的写法是:
//des加密后base64
public String DESEncrypt(String inputData, String inputKey)
throws Exception {
byte[] DESkey = inputData.toString().getBytes();// 设置密钥
DESKeySpec keySpec = new DESKeySpec(DESkey);// 设置密钥参数
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
Key key = keyFactory.generateSecret(keySpec);// 得到密钥对象

    Cipher enCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");// 得到加密对象Cipher
    enCipher.init(Cipher.ENCRYPT_MODE, key);// 设置工作模式为加密模式,给出密钥和向量
    byte[] pasByte = enCipher.doFinal(inputData.getBytes("utf-8"));
    System.out.println(new String(Base64.encodeBase64(pasByte)));
    BASE64Encoder base64Encoder = new BASE64Encoder();
    return base64Encoder.encode(pasByte);
}但结果不匹配,求赐教

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问