2011-08-15 3 views
0

암호를 입력 할 수 없습니다. 암호가 잘못 입력되었지만 프로그램이 중단 될 때 내 프로그램이 암호화하고 해독 할 수 있습니다. 내가 그것을 해결하는 방법을 궁금해했다? 예 : 올바른 암호는 12345678입니다하지만 난 문제가 암호 해독 만에 될 것입니다 생각하지 않습니다 코멘트에 말했듯이 동일 12,341,234에 내가 키를 8 자리하지만 잘못된 키는 decrpytion는은 암호를 잘못 입력 할 수는 없지만 C#

 //Encrypt Method 
    public bool DESEncrypt(String input, String output, String key) 
    { 
     bool success = false; 
     try 
     { 
      int requiredLength = 8; 


      FileStream fsInput = new FileStream(input, FileMode.Open, FileAccess.Read); 

      FileStream fsEncrypted = new FileStream(output, FileMode.Create, FileAccess.Write); 


      DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 


      DES.Padding = PaddingMode.PKCS7; 
      if (key.Length < requiredLength) 
      { 
       key = key.PadRight(requiredLength); 
      } 
      else if (key.Length > requiredLength) 
      { 
       key = key.Substring(0, requiredLength); 
      } 

      DES.Key = ASCIIEncoding.ASCII.GetBytes(key); 
      DES.IV = ASCIIEncoding.ASCII.GetBytes(key); 



      ICryptoTransform desEncrypt = DES.CreateEncryptor(); 
      CryptoStream cryptoStream = new CryptoStream(fsEncrypted, desEncrypt, CryptoStreamMode.Write); 

      byte[] byteInput = new byte[fsInput.Length]; 

      fsInput.Read(byteInput, 0, byteInput.Length); 


      cryptoStream.Write(byteInput, 0, byteInput.Length); 

      cryptoStream.Flush(); 
      cryptoStream.Close(); 
      fsInput.Close(); 
      fsEncrypted.Close(); 

      success = true; 
      MessageBox.Show("Lock Success!"); 

     } 
     catch (Exception ex) 
     { 
      success = false; 
      MessageBox.Show("Encryption Unsuccessful!" + ex); 
      //To Be Continue..... 
      //File being processed error 
      //try .Dispose()? 
     } 
     return success; 
    } 


    //Decrypt method 
    public bool Decrypt(String input, String output, String key) 
    { 
     bool success = false; 
     try 
     { 

      int requiredLength = 8; 
      DESCryptoServiceProvider DES = new DESCryptoServiceProvider(); 
      DES.Padding = PaddingMode.PKCS7; 
      if (key.Length < requiredLength) 
      { 
       key = key.PadRight(requiredLength); 
      } 
      else if (key.Length > requiredLength) 
      { 
       key = key.Substring(0, requiredLength); 
      } 

      //Set secret key For DES algorithm. 
      DES.Key = ASCIIEncoding.ASCII.GetBytes(key); 
      //Set initialization vector. 
      DES.IV = ASCIIEncoding.ASCII.GetBytes(key); 

      //Create a file stream to read the encrypted file back. 
      FileStream fsInput = new FileStream(input, FileMode.Open, FileAccess.Read); 
      //Create a DES decryptor from the DES instance. 
      ICryptoTransform desDecrypt = DES.CreateDecryptor(); 
      //Create crypto stream set to read and do a 
      //DES decryption transform on incoming bytes. 
      CryptoStream cryptostreamDecr = new CryptoStream(fsInput, desDecrypt, CryptoStreamMode.Read); 
      //Print the contents of the decrypted file. 

      BinaryWriter bw = new BinaryWriter(new FileStream(output, FileMode.Create, FileAccess.Write)); 
      byte[] buffer = new byte[500]; 
      int bytesRead = -1; 
      while (true) 
      { 
       bytesRead = cryptostreamDecr.Read(buffer, 0, 500); 
       if (bytesRead == 0) 
       { 
        break; 
       } 
       bw.Write(buffer, 0, bytesRead); 
      } 


      //StreamWriter fsDecrypted = new StreamWriter(output); 
      //fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd()); 
      bw.Flush(); 
      fsInput.Close(); 
      cryptostreamDecr.Close(); 
      bw.Close(); 

      success = true; 
      MessageBox.Show("Unlock Success!"); 
     } 
     catch (Exception ex) 
     { 
      success = false; 
      MessageBox.Show("Decryption Unsuccessful!" + ex); 
      //Try memory stream 
     } 

     return success; 
    } 

} 
} 
+0

테스트를 실행하고 잘못된 키를 입력 한 다음 Visual Studio 디버깅 세션에서 일시 중지 단추를 누를 수 있습니까? 그런 다음 스크립트가 차단중인 호출에 대한 스택 추적을 확인하십시오. – Polity

+2

틀린 키가있는 데이터를 해독하는 것이 해킹되는 해독이 맞습니까? 수학적 프로세스로 제대로 작동하지만 출력은 횡설수설합니다. 출력을 사용할 때 문제가 발생할 것이라고 예상합니다. – Patrick

답변

0

를 중단됩니다 출력을 처리하는 방법.

그러나 문제는 잘못된 키가 사용 된시기를 식별 할 수 있어야한다는 것입니다. 이렇게하는 가장 간단한 방법은 암호화하기 전에 암호화하려는 모든 항목의 시작 부분에 고정 된 알려진 값을 포함시키는 것입니다. 그런 다음 암호를 해독 할 때 일반 텍스트가 알려진 값으로 시작하는지 확인한 다음 알려진 값을 버리고 그렇지 않은 경우 오류를 발생시킵니다.

+0

좋습니다. 시도해보십시오. 제안에 대한 감사합니다. –

관련 문제