2012-05-14 2 views
0

웹 응용 프로그램에서 보호 된 데이터의 암호화 및 암호 해독에 "AesManaged"를 사용하고 있습니다. 내 시나리오에서는 사용자 로그인시 "Email + CurrentDate"를 기반으로 토큰을 만들고 암호화 된 형식으로 사용자에게 보냅니다 (암호화는 AESManaged 클래스를 사용하여 수행됨). 사용자가 다음 서버 측 메서드를 호출 할 때 " Show Report (보고서 표시) "를 선택하면 사용자/클라이언트 응용 프로그램도 요청과 함께 암호화 된 토큰을 보냅니다. 서버 측에서 토큰을 해독하고 해독 된 토큰을 기반으로 조건부 로직을 수행 한 후 주어진 사용자가이 방법에 대한 액세스 권한을 가지는지 (일종의 권한 확인) 결정됩니다.AESManaged (C#)를 피하는 방법 "해독 할 데이터의 길이가 유효하지 않습니다."

사용자가 서버에서 가져 오거나 동일한 길이이지만 사용자가 암호화 된 문자열의 문자를 바꿀 올바른 암호화 된 문자열을 제공하면 기본 흐름에 문제가 없습니다 (예상대로).

그러나 사용자가 (예를 들어) 54 자의 문자열을 얻지 만 서버에 7자를 보낼 때 문제가 발생합니다. 다음 예외가 발생합니다.

사용자가 잘못된 데이터를 제공하더라도이 예외가 발생하지 않도록하고 싶습니다. 그래서 기본적으로 문자열은 항상 해독되어야하며, 유효하지 않은 토큰이면 리소스에 대한 액세스를 제한 할 수 있습니다. 내가 어떻게 이걸 얻을 수 있니? 당신의 대답은 높이 평가 될 것입니다.

예외 정보 : 예외가 발생할

특정 코드 블록.

// Create the streams used for decryption. 
       using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
       { 
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
        { 
         using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
         { 
          plaintext = srDecrypt.ReadToEnd(); 
         } 
        } 
       } 

예외 : "해독 할 데이터의 길이가 유효하지 않습니다."

예외 정보 :

대상 사이트 : {바이트 [] TransformFinalBlock (바이트 [], INT32, INT32)}

선언 유형 : {이름 = "RijndaelManagedTransform"하면 FullName = "System.Security .Cryptography.RijndaelManagedTransform "}

이름 : TransformFinalBlock

참고 : 암호화와 암호 해독 방법 모두에 대해 동일한 키와 iv를 제공합니다.

코드 :

public string EncryptAuthenticationTokenAes(string plainText, byte[] Key, byte[] IV) 
     { 


      byte[] encrypted; 
      // Create an AesManaged object 
      // with the specified key and IV. 
      using (AesManaged aesAlg = new AesManaged()) 
      { 
       aesAlg.Key = Key; 
       aesAlg.IV = IV; 


       // Create a decrytor to perform the stream transform. 
       ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); 

       // Create the streams used for encryption. 
       using (MemoryStream msEncrypt = new MemoryStream()) 
       { 
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
        { 
         using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) 
         { 

          //Write all data to the stream. 
          swEncrypt.Write(plainText); 
         } 
         encrypted = msEncrypt.ToArray(); 
        } 
       } 
      } 
      // Return the encrypted bytes from the memory stream. 
      return Convert.ToBase64String(encrypted); 

     } 

     public string DecryptPasswordAes(string encryptedString, byte[] Key, byte[] IV) 
     { 
      // becuase it is base64, if mod4>0 then it is consider as invalid token 
      int mod4 = encryptedString.Length % 4; 
      if (mod4 > 0) 
      { 
       return string.Empty; 
      } 
      byte[] cipherText = Convert.FromBase64String(encryptedString); 
      // Declare the string used to hold 
      // the decrypted text. 
      string plaintext = null; 

      // Create an AesManaged object 
      // with the specified key and IV. 
      using (AesManaged aesAlg = new AesManaged()) 
      { 
       aesAlg.Key = Key; 
       aesAlg.IV = IV; 

       // Create a decrytor to perform the stream transform. 
       ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); 

       // Create the streams used for decryption. 
       using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
       { 
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
        { 
         using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
         { 
          plaintext = srDecrypt.ReadToEnd(); 
         } 
        } 
       } 

      } 

      return plaintext; 
     } 

답변

0

는 암호화 된 문자열이

  // Create the streams used for decryption. 
      using (MemoryStream msDecrypt = new MemoryStream(cipherText)) 
      { 
       using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) 
       { 
        using (StreamReader srDecrypt = new StreamReader(csDecrypt)) 
        { 
         if (encryptedString.Length != aesAlg.BlockSize) 
         { 
          // Handle invalid token here. 
         } 
         plaintext = srDecrypt.ReadToEnd(); 
        } 
       } 
      } 
1

그 7 개 문자를 가지고 그들에 어떤 의미있는 변환을 수행 할 방법이 없음을 말하고있다 - 그것은 이 그들을 사용하여 암호 해독의 어떤 종류를 수행 할 수 없습니다.

해독을 시도하기 전에 길이 검사를 수행하거나 예외를 잡으십시오. 그리고 두 경우 모두, "유효하지 않은 토큰이 수신되었습니다"논리의 경로를 따라 가십시오.

0

저장 길이를 시도하고 길이가 동일한 경우 다음 확인. 그렇다면 암호 해독 방법을 호출합니다.