2007.02.06

DES(Data Encryption Standard)による簡単な暗号化・復号化(VB.NET版)

System.Security.Cryptography名前空間にあるDESCryptoServiceProviderというクラスを使うと、DES(Data Encryption Standard)と呼ばれるアルゴリズムで暗号化を行うことができます。 また、これを使えばDESで暗号化された文書を復号化(暗号の解読)することもできます。

下記はサンプルVB.NETコードです.

    '-------------------------------------------------------------------
    ' [名前]    Encrypt
    '
    ' [概要] 文字列を暗号化する
    ' [引数]    originalStr         元文字列
    '          
    ' [戻値]    暗号化した文字列
    '-------------------------------------------------------------------
    Public Shared Function Encrypt(ByVal originalStr As String) As String
        Dim original() As Byte
        Dim encrypted() As Byte

        ' 鍵は128ビット(16バイト)である必要がある
        Dim key() As Byte = Encoding.ASCII.GetBytes("xxxxxxxxxxxxxxxx")
        Dim iv() As Byte = Encoding.ASCII.GetBytes("xxxxxxxx")

        original = Encoding.ASCII.GetBytes(originalStr)

        ' DESと呼ばれる方法で暗号化するためのオブジェクト
        Dim des As New TripleDESCryptoServiceProvider

        des.Key = key
        des.IV = iv

        Dim transform As ICryptoTransform = des.CreateEncryptor
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, transform, CryptoStreamMode.Write)

        cs.Write(original, 0, original.Length)
        cs.Close()

        encrypted = ms.ToArray()
        ms.Close()

        Dim result As New StringBuilder()
        Dim b As Byte
        For Each b In encrypted
            result.Append(b.ToString("x2"))
        Next
        Return result.ToString
    End Function

    '-------------------------------------------------------------------
    ' [名前]    Encrypt
    '
    ' [概要] 文字列を復号化する
    ' [引数]    originalStr         暗号化文字列
    '          
    ' [戻値]    復号化した文字列
    '-------------------------------------------------------------------
    Public Shared Function Decrypt(ByVal ecytStr As String) As String
        Dim original() As Byte


        ' 鍵は128ビット(16バイト)である必要がある
        Dim key() As Byte = Encoding.ASCII.GetBytes("xxxxxxxxxxxxxxxx")
        Dim iv() As Byte = Encoding.ASCII.GetBytes("xxxxxxxx")

        'encrypted = Encoding.ASCII.GetBytes(HttpUtility.UrlDecode(ecytStr))

        Dim strLen As Integer = CInt(ecytStr.Length / 2)
        Dim encrypted(strLen - 1) As Byte

        For i As Integer = 0 To strLen - 1
            Dim tempStr As String = ecytStr.Substring(i * 2, 2)
            encrypted(i) = Byte.Parse(tempStr, Globalization.NumberStyles.AllowHexSpecifier)
        Next i

        ' DESと呼ばれる方法で暗号化するためのオブジェクト
        Dim des As New TripleDESCryptoServiceProvider

        des.Key = key
        des.IV = iv

        Dim transform As ICryptoTransform = des.CreateDecryptor
        Dim ms As New MemoryStream
        Dim cs As New CryptoStream(ms, transform, CryptoStreamMode.Write)

        cs.Write(encrypted, 0, encrypted.Length)
        cs.Close()
        original = ms.ToArray()
        ms.Close()

        Return Encoding.ASCII.GetString(original)
    End Function

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)

photo
wu