塵芥回顧録

掃き溜めのようなブログ(?)

ヴィジュネル暗号に関するユーザー定義関数

ヴィジュネル暗号の関数です。
VIGENEREが暗号化
RE_VIGENEREが復号化

本当は第三引数にOptical Booleanいれて一つの関数にしたかったけど、
「BASICのシンタックスエラー. 必要項目:)。」と表示されできず。

Function VIGENERE(vigeneretxt As String, pass As String) As String

	Dim SEARCH As String
	Dim RESULT As String
	Dim JUDGE As Byte
	Dim KEY As Byte
	Dim CONVER As Byte
	Dim KEY_COUNT As Byte
	
	KEY_COUNT = 1

	For i = 1 To LEN(vigeneretxt)
		
		SEARCH = MID(vigeneretxt,i,1)
		
		If 64 < Asc(SEARCH) And Asc(SEARCH) < 91 Then '大文字
			JUDGE = 1
		ElseIf 96 < Asc(SEARCH) And Asc(SEARCH) < 123 Then '小文字
			JUDGE = 2
		Else
			JUDGE = 0			
		End If
		
		If JUDGE = 1 Or JUDGE = 2 Then
			CONVER = Asc(SEARCH)
			KEY = Asc(UCase(MID(pass,KEY_COUNT,1))) - 65
			If JUDGE = 1 Then CONVER = CONVER - 65
			If JUDGE = 2 Then CONVER = CONVER - 97
			CONVER = (CONVER + KEY) Mod 26
			If JUDGE = 1 Then CONVER = CONVER + 65
			If JUDGE = 2 Then CONVER = CONVER + 97
			RESULT = RESULT & Chr(CONVER)
			KEY_COUNT = KEY_COUNT Mod LEN(pass) + 1
		Else
			RESULT = RESULT & SEARCH
		EndIf
		
	Next i
	
	VIGENERE = RESULT
	
End Function
Function RE_VIGENERE(vigeneretxt As String, pass As String) As String

	Dim SEARCH As String
	Dim RESULT As String
	Dim JUDGE As Byte
	Dim KEY As Byte
	Dim CONVER As Byte
	Dim KEY_COUNT As Byte
	
	KEY_COUNT = 1

	For i = 1 To LEN(vigeneretxt)
		
		SEARCH = MID(vigeneretxt,i,1)
		
		If 64 < Asc(SEARCH) And Asc(SEARCH) < 91 Then '大文字
			JUDGE = 1
		ElseIf 96 < Asc(SEARCH) And Asc(SEARCH) < 123 Then '小文字
			JUDGE = 2
		Else
			JUDGE = 0			
		End If
		
		If JUDGE = 1 Or JUDGE = 2 Then
			CONVER = Asc(SEARCH)
			KEY = Asc(UCase(MID(pass,KEY_COUNT,1))) - 65
			If JUDGE = 1 Then CONVER = CONVER - 65
			If JUDGE = 2 Then CONVER = CONVER - 97
			CONVER = (CONVER - KEY + 26) Mod 26 'Modは負数を扱えない
			If JUDGE = 1 Then CONVER = CONVER + 65
			If JUDGE = 2 Then CONVER = CONVER + 97
			RESULT = RESULT & Chr(CONVER)
			KEY_COUNT = KEY_COUNT Mod LEN(pass) + 1
		Else
			RESULT = RESULT & SEARCH
		EndIf
		
	Next i
	
	RE_VIGENERE = RESULT
	
End Function