3DES解密java中用C#加密的标记

时间:2013-11-21 12:30:51

标签: c# java encryption base64 3des

我在询问之前已经搜索了很多,但我找到的所有想法都没有解决我的问题,所以这就是我的问题:

  1. 在C#中,代码(我无法更改,因为它来自其他应用程序)进行加密后详细说明。
  2. 我必须用Java解密加密的令牌但到目前为止没有任何作用,任何人都可以帮忙吗?
  3. 对于1. C#代码:

        static public string Encrypt3DES(string toEncrypt, string SecKey, string IV){
        byte[] keyArray;
        try
        {
            byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
    
        MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecKey));
            hashmd5.Clear();      
    
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
    
            tdes.Key = keyArray;      
            tdes.Mode = CipherMode.CBC;
        tdes.Padding = PaddingMode.PKCS7;
            tdes.IV = UTF8Encoding.UTF8.GetBytes(IV);
    
            ICryptoTransform cTransform = tdes.CreateEncryptor();
    
            //transform the specified region of bytes array to resultArray
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        tdes.Clear();
    
        //Return the encrypted data into unreadable string format
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }
        catch (Exception e) { return string.Empty; }
        }
    

    对于不起作用的2个Java代码:

        public class TripleDesTest {
    
    private KeySpec keySpec;
    private SecretKey key;
    private IvParameterSpec iv;
    
    public TripleDesTest() {
        String keyString = "THE_KEY";
        String ivString = "THE_IV";
    
        try {
            final MessageDigest md = MessageDigest.getInstance("md5");
            final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("UTF-8")));            
            final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
            for (int j = 0, k = 16; j < 8;) {
                keyBytes[k++] = keyBytes[j++];
            }
    
            keySpec = new DESedeKeySpec(keyBytes);
    
            key = SecretKeyFactory.getInstance("DESede").generateSecret(keySpec);
    
            iv = new IvParameterSpec(ivString.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    
    }
    
    
    public String decrypt(String value) {
    
        try {
            Cipher dcipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "SunJCE");
            dcipher.init(Cipher.DECRYPT_MODE, key, iv);
    
            if (value == null)
                return null;
    
            // Decode base64 to get bytes
            byte[] dec = Base64.decodeBase64(value.getBytes("UTF-8"));
    
            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);
    
            // Decode using UTF-8
            return new String(utf8, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    
    }
        }
    

1 个答案:

答案 0 :(得分:0)

这是问题的解决方案(我最终能够自己解决这个问题):

在Java中,替换

final byte[] digestOfPassword = md.digest(Base64.decodeBase64(keyString.getBytes("UTF-8")));`

with:

final byte[] digestOfPassword = md.digest(keyString.getBytes("UTF-8"));

因为在C#端,没有Base64用于密钥:

keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(SecKey));