2013-11-21 81 views
0

我問之前搜索了很多令牌加密,但沒有我找到了工作,我的問題,所以這裏的想法是我的問題:3DES解密在Java在C#

  1. 在C#代碼(我無法更改,因爲它是來自另一個應用程序)進行加密後詳細說明。
  2. 我必須在Java中解密加密的令牌,但目前沒有任何工作,任何人都可以幫忙嗎?

對於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

你必須用Java/C#不同的填充模式。你確定它應該是這樣嗎? – DarkWanderer

+0

我已經理解PKCS7和PKCS5是兼容的(經過一些谷歌搜索),並且PKCS7不存在於標準的Java中。我也試着看看Base64編碼/解碼的差異,但沒有成功...... – user626791

回答

0

這裏是解決問題的方法(我終於能夠解決這個問題我自己):

在Java中,替換

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

有:

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

因爲C#的一面,沒有Base64是用於密鑰:

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