fork download
  1. Public Function rsaEncryptBytes(abMessage() As Byte, strPublicKey As String) As Variant
  2. ' Uses RSA public key to encrypt a message using default PKCS-1-v1.5 encoding method
  3. ' Returns the resulting ciphertext as an array of bytes passed as a Variant,
  4. ' or an empty value on error.
  5. Dim lngRet As Long
  6. Dim abDummy(0) As Byte
  7. Dim abBlock() As Byte
  8. ' All lengths are in octets (i.e. 8-bit bytes)
  9. Dim nkLen As Long ' length, k, of RSA key modulus
  10. Dim nmLen As Long ' length of message, M
  11.  
  12. rsaEncryptBytes = abDummy
  13.  
  14. ' Compute lengths
  15. nmLen = BytesLength(abMessage)
  16. nkLen = RSA_KeyBytes(strPublicKey)
  17.  
  18. ' Encode using EME
  19. ReDim abBlock(nkLen - 1)
  20. lngRet = RSA_EncodeMsg(abBlock(0), nkLen, abMessage(0), nmLen, PKI_EME_DEFAULT)
  21. If lngRet <> 0 Then Exit Function
  22.  
  23. ' Encrypt using RSA public key
  24. lngRet = RSA_RawPublic(abBlock(0), nkLen, strPublicKey, 0)
  25. If lngRet <> 0 Then Exit Function
  26.  
  27. ' Return ciphertext block
  28. rsaEncryptBytes = abBlock
  29. End Function
  30.  
  31. Public Function rsaDecryptBytes(abCipher() As Byte, strPrivateKey As String) As Variant
  32. ' Decrypts RSA-encrypted ciphertext using the RSA private key passed as a string.
  33. ' Returns the resulting message as an array of bytes passed as a Variant,
  34. ' or an empty value on error.
  35. Dim abDummy(0) As Byte
  36. Dim abMessage() As Byte
  37. Dim lngRet As Long
  38. Dim abBlock() As Byte
  39. Dim nkLen As Long ' length, k, of RSA key modulus
  40. Dim nmLen As Long ' length of message, M
  41.  
  42. rsaDecryptBytes = abDummy
  43.  
  44. nkLen = BytesLength(abCipher)
  45. If nkLen <> RSA_KeyBytes(strPrivateKey) Then
  46. Exit Function
  47. End If
  48. abBlock = abCipher
  49.  
  50. ' Decrypt
  51. lngRet = RSA_RawPrivate(abBlock(0), nkLen, strPrivateKey, 0)
  52. If lngRet <> 0 Then
  53. Exit Function
  54. End If
  55.  
  56. ' Now we decode according to EME-PKCS-V1_5-DECODE
  57. nmLen = RSA_DecodeMsg(0, 0, abBlock(0), nkLen, PKI_EME_DEFAULT)
  58. If nmLen <= 0 Then Exit Function
  59. ReDim abMessage(nmLen - 1)
  60. nmLen = RSA_DecodeMsg(abMessage(0), nmLen, abBlock(0), nkLen, PKI_EME_DEFAULT)
  61.  
  62. ' Output M.
  63. rsaDecryptBytes = abMessage
  64.  
  65. End Function
  66.  
  67. Private Function BytesLength(abBytes() As Byte) As Long
  68. ' General function to return length of byte array
  69. ' Trap error if array is empty
  70. On Error Resume Next
  71. BytesLength = UBound(abBytes) - LBound(abBytes) + 1
  72. End Function
  73.  
  74.  
  75. Public Function TestEncryptAndDecrypt()
  76. Dim abMessage() As Byte
  77. Dim abBlock() As Byte
  78. Dim strPublicKey As String
  79. Dim strPubKeyFile As String
  80. strPubKeyFile = "mykeypub.bin"
  81.  
  82. ' Convert ANSI text to bytes
  83. abMessage = StrConv("Hello world!", vbFromUnicode)
  84.  
  85. Debug.Print "M (ansi): " & StrConv(abMessage, vbUnicode)
  86. Debug.Print "M (hex): " & cnvHexStrFromBytes(abMessage)
  87.  
  88. ' Read in the public key from file
  89. strPublicKey = rsaReadPublicKey(strPubKeyFile)
  90. If Len(strPublicKey) = 0 Then
  91. MsgBox "Cannot read RSA public key file '" & strPubKeyFile & "'", vbCritical
  92. Exit Function
  93. End If
  94.  
  95. ' Call encrypt function
  96. abBlock = rsaEncryptBytes(abMessage, strPublicKey)
  97. Debug.Print "CT: " & cnvHexStrFromBytes(abBlock)
  98.  
  99. ' DECRYPTION
  100. Dim strPrivateKey As String
  101. Dim strPriKeyFile As String
  102. Dim abPlain() As Byte
  103. strPriKeyFile = "mykeypri.bin"
  104.  
  105. ' Read in the private key from file
  106. strPrivateKey = rsaReadPrivateKey(strPriKeyFile, "password")
  107. If Len(strPrivateKey) = 0 Then
  108. MsgBox "Cannot read RSA private key file '" & strPriKeyFile & "'", vbCritical
  109. Exit Function
  110. End If
  111.  
  112. ' Call decrypt function
  113. abPlain = rsaDecryptBytes(abBlock, strPrivateKey)
  114. Debug.Print "PT (hex) : " & cnvHexStrFromBytes(abPlain)
  115. Debug.Print "PT (ansi): " & StrConv(abPlain, vbUnicode)
  116.  
  117. End Function
  118.  
Runtime error #stdin #stdout #stderr 0.01s 2736KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Fatal error: exception Unlambda.ParseError