|
|||||
<このサンプルの概要> DESとはData Encryption Standard(データ暗号化標準)の略で共通鍵暗号を採用した暗号化 アルゴリズムです。64ビットブロック毎に暗号します。鍵の長さも64ビットですが8ビット 毎に1ビットのパリティがつくため、実質的には56ビットになります。 DESは多くのコンピュータを使用すれば比較的簡単に解読できるので、DESの代わりに トリプルDESが使用されるようになっています。トリプルDESとは単純にDESを3回繰り返す 事で、2つか3つの鍵を使用して暗号→復号→暗号の順で暗号化します。 DESが比較的簡単に解読出来るとは言っても、その道の博士のような人たちがお金と時間を 使って可能になる事で、一般人にとっては全然問題ない暗号化方式だと思います。 どんな暗号化方式も解読される可能性があると認識して使用する事が大事だと思います。 例えば、パスワードを時々変更するとか。。。 DES暗号化アルゴリズムは公開されているので、そのアルゴリズムをVB6で実現すると、 このサンプルのようになります。 VB.NET版はこちらです。 DES暗号化のサンプル(VB.NET) このサンプルをライブラリ(dll)化したものを、手っ取り早くご試用頂けます。 CryptMin暗号化ライブラリ(VB6版) Private Sub Command1_Click() Dim datStr As String Dim keyStr As String Dim encStr As String Dim decStr As String Dim datBytes() As Byte Dim keyBytes() As Byte Dim encBytes() As Byte Dim decBytes() As Byte datStr = "1111111100000000101010100101010100000000111111110101010110101010" keyStr = "1000000101000001001000010001000100001001000001010000001111111111" datBytes = StrConv(datStr, vbFromUnicode) keyBytes = StrConv(keyStr, vbFromUnicode) InitCryptDes Debug.Print "開始:" & Now Debug.Print "暗号化前:" & datStr encBytes = CryptDesBit(datBytes, True, keyBytes) encStr = StrConv(encBytes, vbUnicode) Debug.Print "暗号化後:" & encStr decBytes = CryptDesBit(encBytes, False, keyBytes) decStr = StrConv(decBytes, vbUnicode) Debug.Print "復号化後:" & decStr Debug.Print "終了:" & Now End Sub 実行結果例 開始:2007/03/25 16:25:42 暗号化前:1111111100000000101010100101010100000000111111110101010110101010 暗号化後:1011111001001010110111110010100101101001010011011101111010101101 復号化後:1111111100000000101010100101010100000000111111110101010110101010 終了:2007/03/25 16:25:42 Private PC1 Private PC2 Private LS Private IP Private MP Private FP Private EP Private SBOX(7) ' 関数名 : CryptDesBit ' 返り値 : 暗号化/復号化した64ビット配列(64個の配列) ' : エラー時は配列数-1で返す ' 引き数 : data(i) : 暗号化/復号化前のデータ64ビット配列(64個の配列) ' : crypt(i): 暗号化(TRUE)または復号化(FALSE) ' : key(i) : 暗号化キー64ビット配列(64個の配列) ' 機能説明 : DESアルゴリズムで暗号化または復号化する ' 著作権 : Copyright(C) 2006 のん All right reserved ' --------------------------------------------------------------------------- ' 使用条件 : このサイトの内容を使用(流用/改変/転載/等全て)した成果物を不特定 ' : 多数に公開/配布する場合は、このサイトを参考にした旨を記述してく ' : ださい。(例)WEBページやReadMeにリンクを貼ってください ' --------------------------------------------------------------------------- Private Function CryptDesBit(ByRef data() As Byte, ByVal crypt As Boolean, _ ByRef key() As Byte) As Byte() ReDim CryptDesBit(-1 To -1) Dim i As Long Dim j As Long Dim k As Long Dim t As Long Dim block(63) As Byte Dim dataLplusR(63) As Byte Dim keyPC1(55) As Byte Dim keyPC2(15, 47) As Byte Dim preSbox(47) As Byte Dim keyC(27) As Byte Dim keyD(27) As Byte Dim keyCD(55) As Byte Dim dataL(31) As Byte Dim dataR(31) As Byte Dim dataRback(31) As Byte Dim sboxBit(47) As Byte If UBound(key) <> 63 Or _ UBound(data) <> 63 Then Exit Function End If For i = 0 To 63 block(i) = key(i) Next i For i = 0 To 55 keyPC1(i) = block(PC1(i) - 1) Next i For i = 0 To 27 keyC(i) = keyPC1(i) keyD(i) = keyPC1(i + 28) Next i For i = 0 To 15 For k = 0 To LS(i) - 1 t = keyC(0) For j = 0 To 28 - 2 keyC(j) = keyC(j + 1) Next j keyC(27) = t t = keyD(0) For j = 0 To 28 - 2 keyD(j) = keyD(j + 1) Next j keyD(27) = t Next k For j = 0 To 27 keyCD(j) = keyC(j) keyCD(j + 28) = keyD(j) Next j For j = 0 To 47 keyPC2(i, j) = keyCD(PC2(j) - 1) Next j Next i For i = 0 To 63 block(i) = data(i) Next i For i = 0 To 31 dataL(i) = block(IP(i) - 1) Next i For i = 32 To 63 dataR(i - 32) = block(IP(i) - 1) Next i For i = 0 To 15 For j = 0 To 31 dataRback(j) = dataR(j) Next j For j = 0 To 47 If crypt Then preSbox(j) = dataR(EP(j) - 1) Xor keyPC2(i, j) Else preSbox(j) = dataR(EP(j) - 1) Xor keyPC2(15 - i, j) End If Next j For j = 0 To 7 t = 6 * j k = SBOX(j)( _ (((preSbox(t + 0) * 2) + (preSbox(t + 5) * 1)) * 16) + _ ((preSbox(t + 1) * 8) + (preSbox(t + 2) * 4) + _ (preSbox(t + 3) * 2) + (preSbox(t + 4) * 1))) t = 4 * j sboxBit(t + 0) = (k \ 8) And 1 sboxBit(t + 1) = (k \ 4) And 1 sboxBit(t + 2) = (k \ 2) And 1 sboxBit(t + 3) = (k \ 1) And 1 Next j For j = 0 To 31 dataR(j) = sboxBit(MP(j) - 1) Xor dataL(j) Next j For j = 0 To 31 dataL(j) = dataRback(j) Next j Next i For i = 0 To 31 t = dataL(i) dataL(i) = dataR(i) dataR(i) = t Next i For i = 0 To 31 dataLplusR(i) = dataL(i) Next i For i = 32 To 63 dataLplusR(i) = dataR(i - 32) Next i For i = 0 To 63 block(i) = dataLplusR(FP(i) - 1) Next i CryptDesBit = block Exit Function End Function ' 関数名 : InitCryptDes ' 返り値 : 無し ' 引き数 : 無し ' 機能説明 : DESアルゴリズムの初期化処理を行なう ' 著作権 : Copyright(C) 2006 のん All right reserved Private Sub InitCryptDes() PC1 = Array( _ 57, 49, 41, 33, 25, 17, 9, _ 1, 58, 50, 42, 34, 26, 18, _ 10, 2, 59, 51, 43, 35, 27, _ 19, 11, 3, 60, 52, 44, 36, _ 63, 55, 47, 39, 31, 23, 15, _ 7, 62, 54, 46, 38, 30, 22, _ 14, 6, 61, 53, 45, 37, 29, _ 21, 13, 5, 28, 20, 12, 4) PC2 = Array( _ 14, 17, 11, 24, 1, 5, _ 3, 28, 15, 6, 21, 10, _ 23, 19, 12, 4, 26, 8, _ 16, 7, 27, 20, 13, 2, _ 41, 52, 31, 37, 47, 55, _ 30, 40, 51, 45, 33, 48, _ 44, 49, 39, 56, 34, 53, _ 46, 42, 50, 36, 29, 32) LS = Array(1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1) IP = Array( _ 58, 50, 42, 34, 26, 18, 10, 2, _ 60, 52, 44, 36, 28, 20, 12, 4, _ 62, 54, 46, 38, 30, 22, 14, 6, _ 64, 56, 48, 40, 32, 24, 16, 8, _ 57, 49, 41, 33, 25, 17, 9, 1, _ 59, 51, 43, 35, 27, 19, 11, 3, _ 61, 53, 45, 37, 29, 21, 13, 5, _ 63, 55, 47, 39, 31, 23, 15, 7) MP = Array( _ 16, 7, 20, 21, 29, 12, 28, 17, _ 1, 15, 23, 26, 5, 18, 31, 10, _ 2, 8, 24, 14, 32, 27, 3, 9, _ 19, 13, 30, 6, 22, 11, 4, 25) FP = Array( _ 40, 8, 48, 16, 56, 24, 64, 32, _ 39, 7, 47, 15, 55, 23, 63, 31, _ 38, 6, 46, 14, 54, 22, 62, 30, _ 37, 5, 45, 13, 53, 21, 61, 29, _ 36, 4, 44, 12, 52, 20, 60, 28, _ 35, 3, 43, 11, 51, 19, 59, 27, _ 34, 2, 42, 10, 50, 18, 58, 26, _ 33, 1, 41, 9, 49, 17, 57, 25) EP = Array( _ 32, 1, 2, 3, 4, 5, _ 4, 5, 6, 7, 8, 9, _ 8, 9, 10, 11, 12, 13, _ 12, 13, 14, 15, 16, 17, _ 16, 17, 18, 19, 20, 21, _ 20, 21, 22, 23, 24, 25, _ 24, 25, 26, 27, 28, 29, _ 28, 29, 30, 31, 32, 1) SBOX(0) = Array( _ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, _ 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, _ 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, _ 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13) SBOX(1) = Array( _ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, _ 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, _ 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, _ 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9) SBOX(2) = Array( _ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, _ 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, _ 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, _ 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12) SBOX(3) = Array( _ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, _ 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, _ 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, _ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14) SBOX(4) = Array( _ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, _ 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, _ 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, _ 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3) SBOX(5) = Array( _ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, _ 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, _ 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, _ 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13) SBOX(6) = Array( _ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, _ 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, _ 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, _ 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12) SBOX(7) = Array( _ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, _ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, _ 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, _ 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11) Exit Sub End Sub |