文字列をDES暗号化する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。

 System.Security.Cryptography名前空間のDESCryptoServiceProviderを使用すると、DES暗号化アルゴリズムによって文字列(バイト配列)を暗号化する事ができる。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
Sub Main()

    ' 鍵は8バイト
    Dim key() As Byte = Encoding.ASCII.GetBytes("password")

    ' 暗号化される文字列
    Dim source As Byte() = Encoding.Unicode.GetBytes("この文字列は暗号化されます。")

    ' 暗号化された後のバイト配列
    Dim encrypted As Byte()

    ' 暗号化
    encrypted = EncryptByDES(source, key)

    ' 復号化された後の文字列
    Dim decripted As Byte()

    decripted = DecryptByDES(encrypted, key)

    ' 文字列化して表示
    Console.WriteLine(Encoding.Unicode.GetString(decripted))

End Sub

' DESによってバイト配列を暗号化する
Function EncryptByDES(ByVal str As Byte(), ByVal encryptionKey() As Byte) As Byte()

    ' DESで暗号化するためのオブジェクト
    Dim des As New DESCryptoServiceProvider()

    ' 鍵は64ビット(8バイト)のバイト配列である必要がある
    des.Key = encryptionKey

    ' 初期化ベクタ(暗号化と復号化の際には同じIVを必要とする)
    des.IV = encryptionKey

    ' CryptoStreamオブジェクトを作成する
    Dim transform As ICryptoTransform = des.CreateEncryptor() ' Encryptorを作成する

    ' バイト配列を暗号化する
    Dim encrypted As Byte() = transform.TransformFinalBlock(str, 0, str.Length)

    transform.Dispose()

    Return encrypted

End Function

' DES暗号化されたバイト配列を復号化する
Function DecryptByDES(ByVal encrypted() As Byte, ByVal decryptionKey() As Byte) As Byte()

    ' DESで復号化するためのオブジェクト
    Dim des As New DESCryptoServiceProvider()

    ' 鍵は64ビット(8バイト)のバイト配列である必要がある
    des.Key = decryptionKey

    ' 初期化ベクタ(暗号化と復号化の際には同じIVを必要とする)
    des.IV = decryptionKey

    ' Decryptorを作成する
    Dim transform As ICryptoTransform = des.CreateDecryptor()

    ' バイト配列を復号化する
    Dim decrypted As Byte() = transform.TransformFinalBlock(encrypted, 0, encrypted.Length)

    transform.Dispose()

    Return decrypted

End Function
C#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
static void Main()
{
    // 鍵は8バイト
    byte[] key = Encoding.ASCII.GetBytes( "password" );

    // 暗号化される文字列
    byte[] source = Encoding.Unicode.GetBytes( "この文字列は暗号化されます。" );

    // 暗号化された後のバイト配列
    byte[] encrypted;

    // 暗号化
    encrypted = EncryptByDES( source, key );

    // 復号化された後の文字列
    byte[] decripted;

    decripted = DecryptByDES( encrypted, key );

    // 文字列化して表示
    Console.WriteLine( Encoding.Unicode.GetString( decripted ) );
}

// DESによってバイト配列を暗号化する
static byte[] EncryptByDES( byte[] str, byte[] encryptionKey )
{
    // DESで暗号化するためのオブジェクト
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

    // 鍵は64ビット(8バイト)のバイト配列である必要がある
    des.Key = encryptionKey;

    // 初期化ベクタ(暗号化と復号化の際には同じIVを必要とする)
    des.IV = encryptionKey;

    // Encryptorを作成する
    ICryptoTransform transform = des.CreateEncryptor();

    // バイト配列を暗号化する
    byte[] encrypted = transform.TransformFinalBlock( str, 0, str.Length );

    transform.Dispose();

    return encrypted;
}

// DESによってバイト配列を暗号化する
static byte[] DecryptByDES( byte[] encrypted, byte[] decryptionKey )
{
    // DESで暗号化するためのオブジェクト
    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

    // 鍵は64ビット(8バイト)のバイト配列である必要がある
    des.Key = decryptionKey;

    // 初期化ベクタ(暗号化と復号化の際には同じIVを必要とする)
    des.IV = decryptionKey;

    // Decryptorを作成する
    ICryptoTransform transform = des.CreateDecryptor();

    // バイト配列を復号化する
    byte[] decrypted = transform.TransformFinalBlock( encrypted, 0, encrypted.Length );

    transform.Dispose();

    return decrypted;
}
出力結果
この文字列は暗号化されます。

次の数の羅列は暗号化されたバイト配列encryptedをダンプしたもの

CA 0F EE 39 40 2F 54 BF 8E A8 AF 61 DD D4 FD FC
8B C4 9B 91 6A 7B 2C FA 3F DF 3F BD FD EE 5C AD

 暗号化と復号化の流れは基本的に同じ。 暗号化・復号化に際して、文字列はバイト配列にする必要がある。 暗号化・復号化はバイト配列に対して行われるので、文字列だけでなくてもバイト配列の形であればどのようなデータも暗号化・復号化できる。