2015-05-09 91 views
1

我使用RSA加密來編碼/解碼信息 加密工作正常,但解密時遇到此錯誤。解密數據時出錯

rsa.Decrypt(dataByte, false); 

描述:在當前web請求的執行過程中發生了未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。

異常詳細信息:System.Security.Cryptography.CryptographicException:參數不正確。

代碼:

string en= x509_Encrypt(Current_Record_Hmac, PFXFile, s_pass); 
string de= ByteToString(X509_Decrypt(en, PFXFile, s_pass)); 


public static byte[] X509_Decrypt(string data, string certificateFile, string password) 
{ 

     var dataArray = data.Split(new char[] { ',' }); 
     byte[] dataByte = new byte[dataArray.Length]; 
     for (int i = 0; i < dataArray.Length; i++) 
     { 
      dataByte[i] = Convert.ToByte(dataArray[i]); 
     } 


     X509Certificate2 cert = new X509Certificate2(certificateFile, password); 
     var rsa = new RSACryptoServiceProvider();  
     var x509_privateKey = cert.PrivateKey; 
     string pri = x509_privateKey.ToString(); 
     string x509_privateKey_ToString = x509_privateKey.ToString(); 
     string X509_publicKey = ByteToString(cert.GetPublicKey()); 
     x509_privateKey_ToString = rsa.ToXmlString(true); 
     X509_publicKey = rsa.ToXmlString(false);  
     rsa.FromXmlString(x509_privateKey_ToString); 
     var decryptedByte = rsa.Decrypt(dataByte, false); 
     return (decryptedByte);     
    } 

    public string x509_Encrypt(string input, string certificateFile, string password) 
    { 


     var dataToEncrypt = _encoder.GetBytes(input);   
     var encoding = new System.Text.ASCIIEncoding(); 

     X509Certificate2 cert = new X509Certificate2(certificateFile, password); 
     var x509_privateKey = cert.PrivateKey; 
     string x509_privateKey_ToString = ByteToString(encoding.GetBytes(x509_privateKey.ToString())); 

     string X509_publicKey = ByteToString(cert.GetPublicKey()); 


    //Encrypting the text using the public key 
     RSACryptoServiceProvider cipher = new RSACryptoServiceProvider(); 
     x509_privateKey_ToString = cipher.ToXmlString(true); 
     X509_publicKey = cipher.ToXmlString(false);  
     cipher.FromXmlString(X509_publicKey); 

    var encryptedByteArray = cipher.Encrypt(dataToEncrypt, false).ToArray(); 
    var length = encryptedByteArray.Count(); 
    var item = 0; 
    var sb = new StringBuilder(); 
    foreach (var x in encryptedByteArray) 
    { 
     item++; 
     sb.Append(x); 

     if (item < length) 
      sb.Append(","); 
    } 

    return sb.ToString(); 

    } 

回答

0

你不能只是將字節轉換爲這樣的人物。如果要將密文傳輸爲字符串,則需要在解密之前使用編碼,例如base 64編碼和解碼。

1

試試這個爲你解密方法:

public string X509_Decrypt(string inputString, string pathToCertFile, string password) 
    { 
     if (inputString == null) 
     { 
      return null; 
     } 

     X509Certificate2 certificate = new X509Certificate2(pathToCertFile, password, X509KeyStorageFlags.MachineKeySet); 

     try 
     { 
      var cryptoProvider = (RSACryptoServiceProvider)certificate.PrivateKey; 
      int dwKeySize = cryptoProvider.KeySize; 
      int blockSize = ((dwKeySize/8) % 3 != 0) ? (((dwKeySize/8)/3) * 4) + 4 : ((dwKeySize/8)/3) * 4; 
      int iterations = inputString.Length/blockSize; 

      var arrayList = new ArrayList(); 
      for (int i = 0; i < iterations; i++) 
      { 
       byte[] encryptedBytes = Convert.FromBase64String(
        inputString.Substring(blockSize * i, blockSize)); 

       Array.Reverse(encryptedBytes); 
       arrayList.AddRange(cryptoProvider.Decrypt(encryptedBytes, true)); 
      } 

      return Encoding.UTF32.GetString(arrayList.ToArray(Type.GetType("System.Byte")) as byte[]); 
     } 
     catch (Exception ex) 
     { 
      throw new SystemException(ex.Message); 
     } 
    } 

而且嘗試一下本作的加密消息:

public string X509_Encrypt(string inputString, string pathToCertFile, string password) 
    { 
     if (inputString == null) 
     { 
      return null; 
     } 

     X509Certificate2 certificate = new X509Certificate2(pathToCertFile, password, X509KeyStorageFlags.MachineKeySet); 

     try 
     { 
      // TODO: Add Proper Exception Handlers 
      var rsaCryptoServiceProvider = (RSACryptoServiceProvider)certificate.PublicKey.Key; 

      int keySize = rsaCryptoServiceProvider.KeySize/8; 
      byte[] bytes = Encoding.UTF32.GetBytes(inputString); 
      int maxLength = keySize - 42; 
      int dataLength = bytes.Length; 
      int iterations = dataLength/maxLength; 

      var stringBuilder = new StringBuilder(); 
      for (int i = 0; i <= iterations; i++) 
      { 
       var tempBytes = new byte[ (dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i]; 

       Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length); 
       byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true); 
       Array.Reverse(encryptedBytes); 
       stringBuilder.Append(Convert.ToBase64String(encryptedBytes)); 
      } 
      return stringBuilder.ToString(); 
     } 
     catch (Exception ex) 
     { 
      throw new SystemException(ex.Message); 
     } 
    } 
相關問題