2015-07-22 77 views
1
public static string tripleDESEncryptor(string toEncrypt, string keyString) 
{ 

    var crypt = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcb); 
    IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8); 
    IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(keyString, BinaryStringEncoding.Utf8); 
    CryptographicKey key = crypt.CreateSymmetricKey(keyBuffer);    
    var sigBuffer = CryptographicEngine.Sign(key, buffer); // throws exception here 
    string signature = CryptographicBuffer.EncodeToBase64String(sigBuffer); 
    return signature; 
} 

這引發了一個InvalidCastException,我似乎無法弄清楚爲什麼或者我做錯了什麼。通用應用c#WP 8.1中的TripleDES加密

該keyString的長度爲24.

任何幫助,將不勝感激。幫助

+0

任何細節例外呢?消息,數據,堆棧跟蹤?您是否嘗試過使用'CryptographicBuffer.GenerateRandom(24)'並將其結果與您自己的密鑰緩衝區(實際類型,長度,內容類型)進行比較? –

+0

消息:指定的演員表無效。 內部異常:null。 堆棧跟蹤:在Windows.Security.Cryptography.Core.CryptographicEngine.Sign(CryptographicKey鍵,IBuffer數據) –

+0

@PieterWitvoet:關鍵緩衝器是顯示長度和容量24一樣,同樣使用CryptographicBuffer.GenerateRandom(24)。緩衝區顯示的長度和容量爲36. –

回答

1

謝謝,這個工作我出去 -

public static string tripleDESEncryptor(string toEncrypt, string keyString) 
{ 
    var crypt = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.TripleDesEcbPkcs7); 

      IBuffer buffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8); 
      IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(keyString, BinaryStringEncoding.Utf8); 

      CryptographicKey key = crypt.CreateSymmetricKey(keyBuffer); 

      IBuffer signed = CryptographicEngine.Encrypt(key, buffer, null); 

      string signature = CryptographicBuffer.EncodeToBase64String(signed); 
      return signature; 
} 
+0

hi @ nipun.birla,我在我的windows phone應用程序中嘗試了同樣的事情,但得到一個異常pn創建了對稱密鑰方法「值不在預期的範圍內。」。你能幫我嗎 – asitis

+0

@asitis你的鑰匙串的長度是多少? –

+0

喜,密鑰長度爲20 – asitis

相關問題