2012-02-11 17 views
3

少し修正したthis codeのバージョンを使用しています。ミッションクリティカルなアプリケーションを作成します。暗号化されるファイルは非常に重要です。これは、これと一緒にやらなければならないいくつかのことがあるので、ゼロから行う必要があります。VB.NET Rijndael Managed EncryptionまたはAESはどれくらい安全ですか?

これはどの程度安全ですか?この暗号化権利を解読することは不可能ですか?

非常に残念ですが、実際のリンクです。 http://www.codeproject.com/Articles/12092/Encrypt-Decrypt-Files-in-VB-NET-Using-Rijndael

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


'************************* 
'** 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 



'************************* 
'** 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 


'************************* 
'** 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 

暗号化と復号には

'**************************** 
'** 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 
     'Setup Progress Bar 
     pbStatus.Value = 0 
     pbStatus.Maximum = 100 

     '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) 
      'Update Progress Bar 
      pbStatus.Value = CInt((lngBytesProcessed/lngFileLength) * 100) 
     End While 

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

私はここに、そこからメインのコードを貼り付けています。

+0

リンクはもう動作しません。質問を更新し、コードを追加してください。リンクは揮発性で、stackoverflowに関する質問/回答はあってはいけません。 –

+0

リンクを更新し、メインコードをここに追加しました。 – esafwan

+0

あなたの質問をクールにしてくれました。 –

答えて

9

クラッキングすることは不可能ではないと言われているように、潜在的な攻撃者にとって可能な限りベストプラクティスに従うことができます。

実際には、あなたがリンクしているコードは、(それまでの場合は)最先端のものとはみなされません。パスワードをハッシュして対称暗号化キーを作成します。これは、パスワードが辞書に基づいて洗練された攻撃を阻止するのに十分なエントロピーを一般に持たないために悪いことです。さらに、saltまたは同等のものを使用しないので、事前計算テーブルでこれを攻撃するのは簡単です。

可能な限り、安全なPRNG(擬似乱数ジェネレータ)で対称鍵を生成する必要があります。パスワードを特に必要としない場合は、パスワードを使用しないでください。絶対にパスワードでなければならない場合は、PKCS5からPBKDF2を使用するか、bcryptやscryptのような方法を使用してください。

また、IVは常に安全なPRNGから生成され、可能であれば再利用されることはありません。リンクした例に示すように、パスワードから派生させる必要はありません。 IVは公開されているため、安全に公開することができますが、予測不可能でランダムな状態を保つことが必須です。そうしないと、GCMなどの認証済み暗号化モードを使用している場合を除き、dedicated attacksの影響を受けます。

これらのトピックに慣れていて不明な点がある場合は、専門家に相談することを強くおすすめします。保護されるデータがあなたの言うことと同じくらい重要であるならば、余分なお金は十分に費やされるべきです。この地域で経験がなければ、自分のソリューションを手作りするときに重要なことを見落とす可能性が非常に高いです。

3

弱いIVまたはキーを使用する場合は、特に不可能なことはありません。しかし、統計的に有意ではないので、夜には十分に眠れなければなりません。ミッションクリティカルな暗号化にこのアルゴリズムを他の人が使用しているかどうかを尋ねている場合は、答えはもっともです。

関連する問題