Last active
January 4, 2023 03:15
-
-
Save ooltcloud/f9b3d1f933d3d0b13cf6 to your computer and use it in GitHub Desktop.
VBA(Excel) で RSA 暗号を Encode/Decode してみる
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sub Main() | |
' 鍵生成 | |
Call GetKey(publicKey, privateKey) | |
Debug.Print publicKey | |
Debug.Print privateKey | |
' 暗号化 | |
encryptString = Encrypt(publicKey, "あいう") | |
Debug.Print encryptString | |
' 復号 | |
planeString = Decrypt(privateKey, encryptString) | |
Debug.Print planeString & "*" | |
End Sub | |
' 鍵生成 | |
Sub GetKey(publicKey, privateKey) | |
Set rsa = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider") | |
publicKey = rsa.ToXmlString(False) | |
privateKey = rsa.ToXmlString(True) | |
End Sub | |
' 暗号化 | |
Function Encrypt(key, value) As String | |
Set rsa = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider") | |
' 文字列を Byte 列に (UTF-16) | |
Dim byteString() As Byte | |
byteString = value | |
' 暗号化 | |
Call rsa.FromXmlString(key) | |
encryptData = rsa.Encrypt(byteString, False) | |
' 暗号 Byte 列 を文字列 に | |
encryptString = "" | |
For Each v In encryptData | |
encryptString = encryptString & Right("00" & Hex(v), 2) | |
Next | |
' return | |
Encrypt = encryptString | |
End Function | |
' 復号 | |
Function Decrypt(key, value) As String | |
Set rsa = CreateObject("System.Security.Cryptography.RSACryptoServiceProvider") | |
' 文字列を Byte 列 に | |
byteLength = Len(value) \ 2 | |
Dim encryptData() As Byte | |
ReDim encryptData(byteLength - 1) | |
For i = 0 To byteLength - 1 | |
encryptData(i) = CByte("&H" & Mid(value, i * 2 + 1, 2)) | |
Next | |
' 復号 | |
Call rsa.FromXmlString(key) | |
planeData = rsa.Decrypt(encryptData, False) | |
' return | |
Decrypt = planeData | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
What about using RSA with SHA256/512 (SHA2 instead of SHA1)?
regards,