HOWTO: Visual Basic .NETÀ» »ç¿ëÇÏ¿© ÆÄÀÏ ¾Ïȣȭ ¹× ÇØµ¶

±â¼ú ÀÚ·á: 301070 - ÀÌ ¹®¼­°¡ Àû¿ëµÇ´Â Á¦Ç° º¸±â.
ÀÌ ¹®¼­´Â ÀÌÀü¿¡ ´ÙÀ½ ID·Î ÃâÆÇµÇ¾úÀ½: KR301070
¸ðµÎ È®´ë | ¸ðµÎ Ãà¼Ò

ÀÌ ÆäÀÌÁö¿¡¼­

¿ä¾à

ÀÌ ¹®¼­¿¡¼­´Â Microsoft .NET Framework¿¡¼­ Á¦°øÇÏ´Â ¾Ïȣȭ Ŭ·¡½º¸¦ »ç¿ëÇÏ¿© ÅØ½ºÆ® ÆÄÀÏÀ» ÀÐÀ» ¼ö ¾ø´Â »óÅ·Π¾ÏȣȭÇß´Ù°¡ ´Ù½Ã ¿øº» ÆÄÀÏ Çü½ÄÀ¸·Î ÇØµ¶ÇÏ´Â ¹æ¹ýÀ» º¸¿©ÁÝ´Ï´Ù.

¿ä±¸ »çÇ×

´ÙÀ½Àº ±ÇÀå Çϵå¿þ¾î, ¼ÒÇÁÆ®¿þ¾î, ³×Æ®¿öÅ© ÀÎÇÁ¶ó ¹× ÇÊ¿äÇÑ ¼­ºñ½º ÆÑ ¸ñ·ÏÀÔ´Ï´Ù.
  • Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server ¶Ç´Â Windows NT 4.0 Server
  • Microsoft Visual Studio .NET

¾Ïȣȭ ¹× ÇØµ¶

Microsoft .NET FrameworkÀÇ System.Security.Cryptographic ³×ÀÓ½ºÆäÀ̽º¿¡¼­´Â ¿©·¯ °¡Áö À¯¿ëÇÑ ¾Ïȣȭ ¹× ÇØµ¶ µµ±¸¸¦ Á¦°øÇÕ´Ï´Ù. CryptoStream Ŭ·¡½º´Â ÀÌ·¯ÇÑ ¿ëµµ·Î Á¦°øµÇ´Â ¼ö¸¹Àº Ŭ·¡½º Áß Çϳª·Î, ÆÄÀÏ ³»¿ëÀ» À¯¿¬ÇÏ°Ô ¾ÏȣȭÇϰųª ÇØµ¶ÇÕ´Ï´Ù.

ÆÄÀÏÀ» ¾ÏȣȭÇÏ·Á¸é ´ÙÀ½°ú °°ÀÌ ÇϽʽÿÀ.
  1. Visual Studio .NETÀ» ¿±´Ï´Ù.
  2. Microsoft Visual Basic .NET¿¡¼­ »õ ÄÜ¼Ö ÀÀ¿ë ÇÁ·Î±×·¥À» ¸¸µì´Ï´Ù. ±×·¯¸é Visual Basic .NET¿¡¼­ ºó Main() ÇÁ·Î½ÃÀú°¡ ÀÖ´Â ¸ðµâÀ» ÀÚµ¿À¸·Î ¸¸µì´Ï´Ù.
  3. System, System.Security ¹× System.Security.Cryptography, System.Text ¹× System.IO ³×ÀÓ½ºÆäÀ̽º¿¡ Imports ¹®À» »ç¿ëÇÕ´Ï´Ù. ±×·¯¸é ³ªÁß¿¡ Äڵ忡¼­ ÀÌ ³×ÀÓ½ºÆäÀ̽ºÀÇ ¼±¾ðÀ» ÇÑÁ¤ÇÏÁö ¾Ê¾Æµµ µË´Ï´Ù. ÀÌ ¸í·É¹®Àº Ç×»ó ¼±¾ð ¾Õ¿¡ »ç¿ëÇØ¾ß ÇÕ´Ï´Ù.
    Imports System
    Imports System.IO
    Imports System.Security
    Imports System.Security.Cryptography
    Imports System.Text
  4. ¾Ïȣȭ/ÇØµ¶¿ë ºñ¹Ð ۸¦ ³ªÅ¸³»´Â »ó¼ö¸¦ Ŭ·¡½º³ª ¸ðµâ¿¡ Ãß°¡ÇÕ´Ï´Ù.
    'Must be 64 bits, 8 bytes.
    Private Const sSecretKey As String = "Password"
  5. Ŭ·¡½º ³»¿¡¼­ ¼¼ °³ÀÇ ¸Å°³ º¯¼ö, Áï sInputFile, sOutputFile ¹× sKey(ÆÄÀÏ ¾Ïȣȭ/ÇØµ¶¿¡ »ç¿ëµÇ´Â ºñ¹Ð Ű)°¡ ÀÖ´Â EncryptFileÀ̶ó´Â ¸Þ¼­µå¸¦ ¸¸µì´Ï´Ù.
        Sub EncryptFile(ByVal sInputFilename As String, _
                       ByVal sOutputFilename As String, _
                       ByVal sKey As String)
        End Sub
  6. EncryptFile ÇÁ·Î½ÃÀú¿¡¼­ ´ë»ó ÆÄÀÏ¿¡ ´ëÇÑ Àбâ/¾²±â¸¦ ó¸®ÇÏ´Â ÀÔ·Â ¹× Ãâ·Â FileStream °³Ã¼¸¦ ¸¸µì´Ï´Ù.
            Dim fsInput As New FileStream(sInputFilename, _
                                        FileMode.Open, FileAccess.Read)
            Dim fsEncrypted As New FileStream(sOutputFilename, _
                                        FileMode.Create, FileAccess.Write)
  7. ÆÄÀÏ¿¡ »ç¿ëÇÒ ½ÇÁ¦ ¾Ïȣȭ ¹× ÇØµ¶ ±â¼úÀ» ³ªÅ¸³»´Â DESCryptoServiceProvider Ŭ·¡½ºÀÇ ÀνºÅϽº¸¦ ¼±¾ðÇÕ´Ï´Ù. ÀÌÁ¦ ´Ù¸¥ °ø±ÞÀÚ¸¦ ¸¸µé¾î RSA³ª ´Ù¸¥ ¾Ïȣȭ ±â¼úÀ» »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
    Dim DES As New DESCryptoServiceProvider()
  8. ¾Ïȣȭ °ø±ÞÀÚ¸¦ Á¦°øÇÒ ¶§´Â ºñ¹Ð Ű(¹ÙÀÌÆ® ¹è¿­)¸¦ ÇÔ²² Á¦°øÇØ¾ß ÇÕ´Ï´Ù. System.Text ³×ÀÓ½ºÆäÀ̽ºÀÇ ÀÎÄÚµù ±â´É Áß¿¡´Â ¹®ÀÚ¿­À» »ç¿ëÇÏ¿© ¹ÙÀÌÆ® ¹è¿­À» ¹ÝȯÇÏ´Â À¯¿ëÇÑ GetBytes() ÇÔ¼ö°¡ Æ÷ÇԵǾî ÀÖ½À´Ï´Ù. ŰÀÇ Å©±â´Â °¢°¢ÀÇ ¾Ïȣȭ ±â¼ú¸¶´Ù ´Ù¸¥µ¥, DES¿¡¼­´Â 8¹ÙÀÌÆ®³ª 8¹®ÀÚ¿¡ ÇØ´çÇÏ´Â 64ºñÆ® ۸¦ »ç¿ëÇÕ´Ï´Ù.

    ۸¦ Á¦°øÇÏÁö ¾ÊÀ¸¸é °ø±ÞÀÚ°¡ ۸¦ ÀÓÀÇ·Î »ý¼ºÇϹǷΠÆÄÀÏÀÌ ÇØµ¶ÇÒ ¼ö ¾ø´Â »óÅ·ΠÀÎÄÚµùµË´Ï´Ù. ¶ÇÇÑ ÃʱâÈ­ º¤ÅÍ(IV)µµ Á¦°øÇØ¾ß Çϴµ¥, ÀÌ °ªÀº ¾Ïȣȭ °úÁ¤¿¡¼­ »ç¿ëµÇ¸ç Á¦°øÇÏÁö ¾ÊÀº °æ¿ì ÀÓÀÇ·Î »ý¼ºµË´Ï´Ù. ÀÌ °ªÀº ¾Ïȣȭ¿Í ÇØµ¶¿¡¼­ ¸ðµÎ °°¾Æ¾ß ÇϹǷΠÀÓÀÇ·Î »ý¼ºÇÏÁö ¾Ê´Â °ÍÀÌ ÁÁ½À´Ï´Ù.
    DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)
    DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
  9. ¾Ïȣȭ °ø±ÞÀÚ¸¦ »ç¿ëÇÏ¿© ¾Ïȣȭ °³Ã¼(CreateEncryptor)¿Í ±âÁ¸ Ãâ·Â FileStream °³Ã¼¸¦ »ý¼ºÀÚ¿¡ Æ÷ÇÔ½ÃŰ´Â CryptoStream Ŭ·¡½ºÀÇ ÀνºÅϽº¸¦ ¸¸µì´Ï´Ù.
    Dim cryptostream As New CryptoStream(fsEncrypted, _
                                         desencrypt, _
                                         CryptoStreamMode.Write)
  10. ¸¶Áö¸·À¸·Î, ÀÔ·Â ÆÄÀÏÀ» Àаí CryptoStream °³Ã¼¸¦ Åë°úÇÏ´Â Ãâ·Â ÆÄÀÏ¿¡ À̸¦ ±â·ÏÇÕ´Ï´Ù. ÀÌ·¸°Ô ±â·ÏÇÑ ³»¿ëÀº Á¦°øµÈ ۸¦ »ç¿ëÇÏ¿© ¾ÏȣȭµË´Ï´Ù.
    Dim bytearrayinput(fsInput.Length - 1) As Byte
    fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)
    cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)
  11. DecryptFileÀ̶ó´Â ¸Þ¼­µå¸¦ ¸¸µì´Ï´Ù. ÇØµ¶ °úÁ¤Àº ¾Ïȣȭ °úÁ¤°ú ¸Å¿ì ºñ½ÁÇѵ¥, DecryptFile ÇÁ·Î½ÃÀú´Â EncryptFile ÇÁ·Î½ÃÀú¿Í µÎ °¡Áö »çÇ×ÀÌ Å©°Ô ´Ù¸¨´Ï´Ù. ù°, CryptoStream °³Ã¼¸¦ ¸¸µé ¶§ CreateEncryptor°¡ ¾Æ´Ñ CreateDecryptor°¡ °³Ã¼ »ç¿ë ¹æ¹ýÀ» ÁöÁ¤ÇÕ´Ï´Ù. µÑ°, ÇØµ¶µÈ ÅØ½ºÆ®¸¦ ´ë»ó ÆÄÀÏ¿¡ ±â·ÏÇÒ ¶§ CryptoStream °³Ã¼´Â ´ë»ó ½ºÆ®¸²ÀÌ ¾Æ´Ñ ¿øº» ½ºÆ®¸²ÀÔ´Ï´Ù.
    Sub DecryptFile(ByVal sInputFilename As String, _
        ByVal sOutputFilename As String, _
        ByVal sKey As String)
    
        Dim DES As New DESCryptoServiceProvider()
        'A 64 Bit Key and IV is required for this provider.
        'Set secret key For DES algorithm.
        DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)
        'Set initialization vector.
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)
    
        'create file stream to read encrypted file back
        Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)
        'create DES Decryptor from our des instance
        Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()
        'create crypto stream set to read and do a des decryption transform on incoming bytes
        Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)
        'print out the contents of the decrypted file
        Dim fsDecrypted As New StreamWriter(sOutputFilename)
        fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)
        fsDecrypted.Flush()
        fsDecrypted.Close()
    End Sub
  12. EncryptFile°ú DecryptFileÀ» µÑ ´Ù È£ÃâÇÏ´Â ÁÙÀ» Main() ÇÁ·Î½ÃÀú¿¡ Ãß°¡ÇÕ´Ï´Ù.
    Public Sub Main()
        EncryptFile("c:\temp\test.txt", _
                        "c:\temp\Encrypted.txt", _
                        sSecretKey)
        DecryptFile("c:\temp\Encrypted.txt", _
                    "c:\temp\Decrypted.txt", _
                    sSecretKey)
    End Sub
  13. ÀÀ¿ë ÇÁ·Î±×·¥À» ÀúÀåÇÑ ´ÙÀ½ ½ÇÇàÇÕ´Ï´Ù. ÀÌ ¶§ ÀÔ·Â ÆÄÀÏ À̸§¿¡ »ç¿ëµÇ´Â °æ·Î´Â ±×´ÙÁö Áß¿äÇÏÁö ¾ÊÀº ±âÁ¸ ÆÄÀÏÀ» °¡¸®ÄÑ¾ß ÇÕ´Ï´Ù.

ÀÛµ¿ ¿©ºÎ È®ÀÎ

ÅØ½ºÆ® ÆÄÀÏ(.txt)¿¡¼­ ÀÌ Äڵ带 Å×½ºÆ®ÇÏ¿© ÆÄÀÏÀÌ ¿Ã¹Ù¸£°Ô ¾Ïȣȭ/ÇØµ¶µÇ´ÂÁö È®ÀÎÇÕ´Ï´Ù. ÆÄÀÏÀ» ÇØµ¶ÇÒ ¶§´Â ¿øº» ÆÄÀÏ¿¡ ÇØµ¶ÇÏÁö ¸»°í ÀÌ ¹®¼­ÀÇ Sub Main() ÇÁ·Î½ÃÀú¿¡ ÀÖ´Â °Íó·³ »õ ÆÄÀÏ¿¡ ÇØµ¶ÇØ¾ß ÇÕ´Ï´Ù. ÇØµ¶µÈ ÆÄÀÏÀ» °Ë»çÇÏ°í ¿øº»°ú ºñ±³ÇÕ´Ï´Ù.

Àüü ÄÚµå ¿¹Á¦

 Imports System Imports System.IO Imports System.Security Imports System.Security.Cryptography Imports System.Text   Module Module1     'Must be 64 bits, 8 bytes.     Private Const sSecretKey As String = "Password"      Public Sub Main()         EncryptFile("c:\temp\test.txt", _                         "c:\temp\Encrypted.txt", _                         sSecretKey)         DecryptFile("c:\temp\Encrypted.txt", _                     "c:\temp\Decrypted.txt", _                     sSecretKey)     End Sub      Sub EncryptFile(ByVal sInputFilename As String, _                    ByVal sOutputFilename As String, _                    ByVal sKey As String)          Dim fsInput As New FileStream(sInputFilename, _                                     FileMode.Open, FileAccess.Read)         Dim fsEncrypted As New FileStream(sOutputFilename, _                                     FileMode.Create, FileAccess.Write)          Dim DES As New DESCryptoServiceProvider()          'Set secret key For DES algorithm.         'A 64 Bit Key and IV is required for this provider         DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey)          'Set initialization vector.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)          'create DES Encryptor from this instance         Dim desencrypt As ICryptoTransform = DES.CreateEncryptor()         'Create Crypto Stream that transforms file stream using des encryption         Dim cryptostream As New CryptoStream(fsEncrypted, _                                             desencrypt, _                                             CryptoStreamMode.Write)          'Read the file text into our byte array         Dim bytearrayinput(fsInput.Length - 1) As Byte         fsInput.Read(bytearrayinput, 0, bytearrayinput.Length)         'write out DES encrypted file         cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length)         cryptostream.Close()     End Sub      Sub DecryptFile(ByVal sInputFilename As String, _         ByVal sOutputFilename As String, _         ByVal sKey As String)          Dim DES As New DESCryptoServiceProvider()         'A 64 Bit Key and IV is required for this provider.         'Set secret key For DES algorithm.         DES.Key() = ASCIIEncoding.ASCII.GetBytes(sKey)         'Set initialization vector.         DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey)          'create file stream to read encrypted file back         Dim fsread As New FileStream(sInputFilename, FileMode.Open, FileAccess.Read)         'create DES Decryptor from our des instance         Dim desdecrypt As ICryptoTransform = DES.CreateDecryptor()         'create crypto stream set to read and do a des decryption transform on incoming bytes         Dim cryptostreamDecr As New CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read)         'print out the contents of the decrypted file         Dim fsDecrypted As New StreamWriter(sOutputFilename)         fsDecrypted.Write(New StreamReader(cryptostreamDecr).ReadToEnd)         fsDecrypted.Flush()         fsDecrypted.Close()     End Sub  End Module

ÂüÁ¶

.NETÀÇ ¾Ïȣȭ ±â´É »ç¿ë°ú ÀϹÝÀûÀÎ ¾Ïȣȭ¿¡ ´ëÇÑ ÀÚ¼¼ÇÑ ³»¿ëÀº ´ÙÀ½ À¥ »çÀÌÆ®¸¦ ÂüÁ¶ÇϽʽÿÀ.

¼Ó¼º

±â¼ú ÀÚ·á: 301070 - ¸¶Áö¸· °ËÅä: 2002³â 1¿ù 15ÀÏ È­¿äÀÏ - ¼öÁ¤: 1.0
º» ¹®¼­ÀÇ Á¤º¸´Â ´ÙÀ½ÀÇ Á¦Ç°¿¡ Àû¿ëµË´Ï´Ù.
  • Microsoft Visual Basic .NET 2002 Standard Edition
Ű¿öµå:?
kbhowto kbhowtomaster KB301070

Çǵå¹é º¸³»±â