0
0

[Excel VBA] ヴィジュネル暗号で暗号化する

Last updated at Posted at 2024-09-04

ざっくり説明

  • 渡された文字列をヴィジュネル暗号に従って暗号化します
  • 渡された暗号文をヴィジュネル暗号に従って復号します

コード

暗号化(ヴィジュネル暗号)
Function EncryptWithVigenereCipher(ByVal plaintext As String, ByVal key As String) As String
'================================
'用途  :ヴィジュネル暗号による暗号化
'--------------------------------
'第一引数:平文 (String型)
'第二引数:暗号化に用いる鍵 (String型)
'戻り値 :暗号文 (String型)
'================================
    key = UCase(key)
    plaintext = UCase(plaintext)

    Dim VS_ARR As Variant
    VS_ARR = CreateVigenereSquare

    Dim convertedText As String
    Dim i As Long
    For i = 1 To Len(plaintext)
        Dim keyChr As String
        Dim plaintextChr As String
        keyChr = Mid(key, (i - 1) Mod Len(key) + 1, 1)
        plaintextChr = Mid(plaintext, i, 1)

        Dim columnNumber As Long
        Dim rowNumber As Long
        columnNumber = ConvertColValueMutually(keyChr)
        rowNumber = ConvertColValueMutually(plaintextChr)

        Dim convertedCharacter As String
        convertedCharacter = VS_ARR(columnNumber, rowNumber)
        convertedText = convertedText + convertedCharacter
    Next

    EncryptWithVigenereCipher = convertedText
End Function
復号(ヴィジュネル暗号)
Function DecryptWithVigenereCipher(ByVal ciphertext As String, ByVal key As String) As String
'================================
'用途  :ヴィジュネル暗号による復号
'--------------------------------
'第一引数:暗号文 (String型)
'第二引数:復号に用いる鍵 (String型)
'戻り値 :平文 (String型)
'================================
    key = UCase(key)
    ciphertext = UCase(ciphertext)

    Dim VS_ARR As Variant
    VS_ARR = CreateVigenereSquare

    Dim convertedText As String
    Dim i As Long
    For i = 1 To Len(ciphertext)
        Dim keyChr As String
        Dim ciphertextChr As String
        keyChr = Mid(key, (i - 1) Mod Len(key) + 1, 1)
        ciphertextChr = Mid(ciphertext, i, 1)

        Dim columnNumber As Long
        columnNumber = ConvertColValueMutually(keyChr)

        Dim j As Long
        For j = 1 To 26
            If ciphertextChr = VS_ARR(columnNumber, j) Then
                Dim convertedCharacter As String
                convertedCharacter = ConvertColValueMutually(j)
                convertedText = convertedText + convertedCharacter
                Exit For
            End If
        Next
    Next

    DecryptWithVigenereCipher = convertedText
End Function
ヴィジュネル方陣の生成
Function CreateVigenereSquare() As Variant
'================================
'用途  :ヴィジュネル方陣の生成
'--------------------------------
'戻り値 :ヴィジュネル方陣の配列 (Variant型)
'================================
    Dim VS(1 To 26, 1 To 26) As String
    Dim i As Long
    Dim j As Long
    
    For j = 1 To 26
        VS(1, j) = ChrW(64 + j)
    Next

    For i = 2 To 26
        For j = 1 To 26
            VS(i, j) = VS(i - 1, j Mod 26 + 1)
        Next
    Next

    CreateVigenereSquare = VS
End Function

注意
その他に、下記の記事のConvertColNumberToColLetter()も用いています

使用方法

第一引数に暗号化/復号対象の文字列
第二引数に暗号化/復号に用いる文字列(鍵)
を渡します。

CreateVigenereSquare()という3つ目のFunctionは共通の部品なので、
どちらを使用する場合も導入が必要であることと、
以前、私が書いたConvertColNumberToColLetter()というFunctionも用います。
※記事へのリンクは少し上にあります

仕様等

  • 第一・第二引数に入力できる文字列には a~z と A~Z を用いることができますが、出力される平文/暗号文は全て大文字となります
  • ヴィジュネル方陣は関数内で生成しているためシートから読み込む必要はありません

注意点

特になし

その他

ヴィジュネル方陣の配列を作るアルゴリズム、4つぐらい考えたけど
書いてあるやつが一番早かったです。つかれました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0