2013-04-26 2 views
1

나는 rijndael 알고리즘을 사용하여 파일을 ecnrypt하고 해독 할 수있는 프로그램을 작성합니다.mscorlib.dll에서 'System.ArgumentNullException'유형의 첫 번째 예외가 발생했습니다.

Imports System 
Imports System.IO 
Imports System.Security 
Imports System.Security.Cryptography 

Public Class crypter 
#Region "1. Global Variables " 

    '************************* 
    '** Global Variables 
    '************************* 

    Dim strFileToEncrypt As String 
    Dim strFileToDecrypt As String 
    Dim strOutputEncrypt As String 
    Dim strOutputDecrypt As String 
    Dim fsInput As System.IO.FileStream 
    Dim fsOutput As System.IO.FileStream 

#End Region 


#Region "2. Create A Key " 

    '************************* 
    '** Create A Key 
    '************************* 

    Private Function CreateKey(ByVal strPassword As String) As Byte() 
     'Convert strPassword to an array and store in chrData. 
     Dim chrData() As Char = strPassword.ToCharArray 
     'Use intLength to get strPassword size. 
     Dim intLength As Integer = chrData.GetUpperBound(0) 
     'Declare bytDataToHash and make it the same size as chrData. 
     Dim bytDataToHash(intLength) As Byte 

     'Use For Next to convert and store chrData into bytDataToHash. 
     For i As Integer = 0 To chrData.GetUpperBound(0) 
      bytDataToHash(i) = CByte(Asc(chrData(i))) 
     Next 

     'Declare what hash to use. 
     Dim SHA512 As New System.Security.Cryptography.SHA512Managed 
     'Declare bytResult, Hash bytDataToHash and store it in bytResult. 
     Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash) 
     'Declare bytKey(31). It will hold 256 bits. 
     Dim bytKey(31) As Byte 

     'Use For Next to put a specific size (256 bits) of 
     'bytResult into bytKey. The 0 To 31 will put the first 256 bits 
     'of 512 bits into bytKey. 
     For i As Integer = 0 To 31 
      bytKey(i) = bytResult(i) 
     Next 

     Return bytKey 'Return the key. 
    End Function 

#End Region 


#Region "3. Create An IV " 

    '************************* 
    '** Create An IV 
    '************************* 

    Private Function CreateIV(ByVal strPassword As String) As Byte() 
     'Convert strPassword to an array and store in chrData. 
     Dim chrData() As Char = strPassword.ToCharArray 
     'Use intLength to get strPassword size. 
     Dim intLength As Integer = chrData.GetUpperBound(0) 
     'Declare bytDataToHash and make it the same size as chrData. 
     Dim bytDataToHash(intLength) As Byte 

     'Use For Next to convert and store chrData into bytDataToHash. 
     For i As Integer = 0 To chrData.GetUpperBound(0) 
      bytDataToHash(i) = CByte(Asc(chrData(i))) 
     Next 

     'Declare what hash to use. 
     Dim SHA512 As New System.Security.Cryptography.SHA512Managed 
     'Declare bytResult, Hash bytDataToHash and store it in bytResult. 
     Dim bytResult As Byte() = SHA512.ComputeHash(bytDataToHash) 
     'Declare bytIV(15). It will hold 128 bits. 
     Dim bytIV(15) As Byte 

     'Use For Next to put a specific size (128 bits) of 
     'bytResult into bytIV. The 0 To 30 for bytKey used the first 256 bits. 
     'of the hashed password. The 32 To 47 will put the next 128 bits into bytIV. 
     For i As Integer = 32 To 47 
      bytIV(i - 32) = bytResult(i) 
     Next 

     Return bytIV 'return the IV 
    End Function 

#End Region 


#Region "4. Encrypt/Decrypt File " 

    '**************************** 
    '** Encrypt/Decrypt File 
    '**************************** 

    Private Enum CryptoAction 
     'Define the enumeration for CryptoAction. 
     ActionEncrypt = 1 
     ActionDecrypt = 2 
    End Enum 

    Private Sub EncryptOrDecryptFile(ByVal strInputFile As String, _ 
            ByVal strOutputFile As String, _ 
            ByVal bytKey() As Byte, _ 
            ByVal bytIV() As Byte, _ 
            ByVal Direction As CryptoAction) 
     Try 'In case of errors. 

      'Setup file streams to handle input and output. 
      fsInput = New System.IO.FileStream(strInputFile, FileMode.Open, _ 
               FileAccess.Read) 
      fsOutput = New System.IO.FileStream(strOutputFile, FileMode.OpenOrCreate, _ 
               FileAccess.Write) 
      fsOutput.SetLength(0) 'make sure fsOutput is empty 

      'Declare variables for encrypt/decrypt process. 
      Dim bytBuffer(4096) As Byte 'holds a block of bytes for processing 
      Dim lngBytesProcessed As Long = 0 'running count of bytes processed 
      Dim lngFileLength As Long = fsInput.Length 'the input file's length 
      Dim intBytesInCurrentBlock As Integer 'current bytes being processed 
     Dim csCryptoStream As CryptoStream 
      'Declare your CryptoServiceProvider. 
      Dim cspRijndael As New System.Security.Cryptography.RijndaelManaged 


      'Determine if ecryption or decryption and setup CryptoStream. 
      Select Case Direction 
       Case CryptoAction.ActionEncrypt 
        csCryptoStream = New CryptoStream(fsOutput, _ 
        cspRijndael.CreateEncryptor(bytKey, bytIV), _ 
        CryptoStreamMode.Write) 

       Case CryptoAction.ActionDecrypt 
        csCryptoStream = New CryptoStream(fsOutput, _ 
        cspRijndael.CreateDecryptor(bytKey, bytIV), _ 
        CryptoStreamMode.Write) 
      End Select 

      'Use While to loop until all of the file is processed. 
      While lngBytesProcessed < lngFileLength 
       'Read file with the input filestream. 
       intBytesInCurrentBlock = fsInput.Read(bytBuffer, 0, 4096) 
       'Write output file with the cryptostream. 
       csCryptoStream.Write(bytBuffer, 0, intBytesInCurrentBlock) 
       'Update lngBytesProcessed 
       lngBytesProcessed = lngBytesProcessed + CLng(intBytesInCurrentBlock) 
      End While 

      'Close FileStreams and CryptoStream. 
      csCryptoStream.Close() 
      fsInput.Close() 
      fsOutput.Close() 

      'If encrypting then delete the original unencrypted file. 
      If Direction = CryptoAction.ActionEncrypt Then 
       Dim fileOriginal As New FileInfo(strFileToEncrypt) 
       fileOriginal.Delete() 
      End If 

      'If decrypting then delete the encrypted file. 
      If Direction = CryptoAction.ActionDecrypt Then 
       Dim fileEncrypted As New FileInfo(strFileToDecrypt) 
       fileEncrypted.Delete() 
      End If 

     Catch 
      fsInput.Close() 
      fsOutput.Close() 

      If Direction = CryptoAction.ActionDecrypt Then 
       Dim fileDelete As New FileInfo(strOutputFile) 
       fileDelete.Delete() 

      Else 
       Dim fileDelete As New FileInfo(strOutputFile) 
       fileDelete.Delete() 


      End If 

     End Try 
    End Sub 

#End Region 

End Class 

아무도 아이디어가 있습니까

Imports crypter.crypter 
Public Class Form1 

    Private Sub btnEncrypt_Click(sender As System.Object, e As System.EventArgs) Handles btnEncrypt.Click 
    Dim objCrypter As New crypter.crypter 
    Dim strPass As String = txtPass.Text 
    Dim bytkey As Byte() 
    Dim bytIV As Byte() 

    bytkey = objCrypter.CreateKey(strPass) 
    bytIV = objCrypter.CreateIV(strPass) 

    objCrypter.EncryptOrDecryptFile(txtSource.Text, txtDestination.Text, bytkey, bytIV, CryptoAction.ActionEncrypt) 


    End Sub 


    Private Sub btnDecrypt_Click(sender As Object, e As System.EventArgs) Handles btnDecrypt.Click 
    Dim objCrypter As New crypter.crypter 
    Dim strPass As String = txtPass.Text 
    Dim bytkey As Byte() 
    Dim bytIV As Byte() 

    bytkey = objCrypter.CreateKey(strPass) 
    bytIV = objCrypter.CreateIV(strPass) 

    objCrypter.EncryptOrDecryptFile(txtSource.Text, txtDestination.Text, bytkey, bytIV, crypter.crypter.CryptoAction.ActionDecrypt) 

    End Sub 
End Class 

이 내 클래스 crypter에서 내 코드입니다 : 이것은 내 코드입니다

A first chance exception of type 'System.ArgumentNullException' occurred in mscorlib.dll

: 내가 디버깅하지만 나는 다음과 같은 오류가 발생합니다 내 실수 일 수도 있겠 니? 나는 특정 문제를 알 수 없지만 사전에

덕분에,

+3

오, 내 생각에 누군가가 그 코드의 벽을 읽으 리라 기대합니까? 실패한 메소드를 게시하고 예외를 throw하는 정확한 라인을 표시 할 수 있습니까? 조언을 주신 덕분에 – Steve

답변

13

, 방법은 내가 두 상자를 확인, 비주얼 스튜디오 디버그 메뉴 -> 예외 항목을 선택로 이동합니다 첫 번째 예외의 원인을 찾을 공용 언어 런타임 예외.

이제 디버깅 할 때 IDE는 모든 예외에 대해 중단 되더라도 중단됩니다.

코드를 다시 실행하면 mscorlib에 호출되는 행이 표시되고 Null 예외가 발생합니다.

행운을 빈다.

+0

! 이것은 나를 많이 도왔다. – kasperB

관련 문제