2

I have the libsodium-net NuGet loaded into my PoC solution and have the 32-bit and 64-bit libsodium.dll files in their respective directories (System32 and SysWOW64). Whenever I go to run the program in debug mode, I get the BadImageFormatException error. Is it enough to have the libsodium.dll files in their respective System directories and I'm just missing something else, or do I need to have copies of those dll files in my solution as well?

I have tried compiling using both x86 and x64 build configurations since I read here: BadImageFormatException during .Net assembly load issue that, that could be the cause of this error but I still got the same error in both areas.

I also tried the answer I found here: How to include libsodium.net on ASP.NET for incorporating libsodium-net into VS, where it said to install Visual C++ Redistributable 2015 as well, but the problem there is when I tried that, I got an error from the installer stating that it was already installed on my computer. When I checked though, all I had were the 2008, 2013 and 2017 versions, not 2015, so I'm still at a loss on why I couldn't install the 2015 Redistributable but that's for another time.

Here's my procedure and at least according to intellisense, everything here's hooked up correctly.

Option Explicit On
Imports Sodium
Imports System.Text

Public Class Form1
    Dim textToEncrypt, decrypted As String
    Shared encoder As New UTF8Encoding

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim key, hashedBytes, salt As Byte()

        textToEncrypt = TextBox1.Text
        Dim textBytes As Byte() = encoder.GetBytes(textToEncrypt)
        hashedBytes = CryptoHash.Sha512(textToEncrypt)

        salt = PasswordHash.ScryptGenerateSalt()
        key = SecretBox.GenerateKey()

        Dim passEnc As Byte() = PasswordHash.ScryptHashBinary(textBytes, salt, PasswordHash.Strength.Medium)

        TextBox2.Text = Convert.ToBase64String(passEnc)
        TextBox6S.Text = Convert.ToBase64String(salt)
        TextBox3PBK.Text = Convert.ToBase64String(key)
        TextBox5H.Text = Convert.ToBase64String(hashedBytes)
    End Sub
End Class

As you can see in the above procedure, this program's fairly straight forward and should just take an input, run it through a couple of Sodium functions, then convert to Base 64 strings and print the results to some text boxes but I get the BadImageFormatException error on each Sodium function the procedure calls.

TOTM
  • 107
  • 7
  • 1
    I tried to repro, but couldn't. However in the process after adding the `libsodium-net NuGet` I had a compile error. This was fixed by checking Nuget and seeing that there was an update for `Baseclass.Contrib.Nuget.Output`. When I updated that, everything worked - including your code above. – djv May 09 '19 at 16:02
  • But I am confused as to why you are mentioning system32 and syswow64, because the nuget assemblies automatically are added to your solution directory and are automatically referenced. When the application is built, the dlls will be copied to the binary directory like any other referenced assembly. You shouldn't need to move any assemblies around when using Nuget, usually (in this case you don't). – djv May 09 '19 at 16:03
  • I checked in the NuGet manager and both libsodium and Sodium.Core are up to date and running the current stable models. I only mentioned the System32 and SysWOW64 directories because it was something I had tried while trying to find solutions that other people had found and placing the dlls in those directories had solved the issue for one a couple people so I tried it to see if it would solve mine as well, which it didn't. – TOTM May 09 '19 at 16:30

0 Answers0