John Bingham
John Bingham

Reputation: 2006

Encrypt string in VBA, decrypt in C#

I need to encrypt authentication information (strings) in VBA and then decrypt them correctly in C# (a WCF service).

We're not overly concerned about the type or strength of encryption used, just that the decryption must correctly produce the original text.

Can anyone advise me how to go about this, or point me at blocks of code to implement both in VBA & in C# to achieve what I need to do?

Many Thanks in advance

Upvotes: 1

Views: 2746

Answers (1)

JustJohn
JustJohn

Reputation: 1460

Ok, I am positive you can use this. I am using it for VBA, however, it is simple enough to use in vb.net as is, and one step away from being converting to C#. I got the code and explanation here: link to who built the function I just use it for VBA. I'v used tons of VBA functions in vb.net and I've converted enough vb.net to c# to know this could be used in c#.

Function RunRC4(sMessage, strKey)
Dim kLen, x, y, i, j, temp
Dim s(256), k(256)

'Init keystream'
klen = Len(strKey)
For i = 0 To 255
    s(i) = i
    k(i) = Asc(Mid(strKey, (i Mod klen) + 1, 1))
Next

j = 0
For i = 0 To 255
    j = (j + k(i) + s(i)) Mod 255
    temp = s(i)
    s(i) = s(j)
    s(j) = temp
Next

'Drop n bytes from keystream'
x = 0
y = 0
For i = 1 To 3072
    x = (x + 1) Mod 255
    y = (y + s(x)) Mod 255
    temp = s(x)
    s(x) = s(y)
    s(y) = temp
Next

'Encode/Decode'
For i = 1 To Len(sMessage)
    x = (x + 1) Mod 255
    y = (y + s(x)) Mod 255
    temp = s(x)
    s(x) = s(y)
    s(y) = temp

    RunRC4 = RunRC4 & Chr(s((s(x) + s(y)) Mod 255) Xor Asc(Mid(sMessage, i, 1)))
Next
End Function

Upvotes: 2

Related Questions