Hello Everyone!

My problem is this. I have a VBscript encrypting passwords using capicom.dll RC2. I want to use C# to decrypt this password. Which I can't get to work for the life of me. Keeps coming back saying 'Specified key is not a valid size for this algorithm.'

(but i can decrypt in vbscript without an issue! >.<)

Below is how I encrypt the data and how I'm trying to decrypt the data in C#

classic ASP/VBscript Code
ENCRYPT

Function AESEncrypt(ByVal String)

    if isnull(String) or String="" then
        AESEncrypt=""
    else
        set fs=Server.CreateObject("Scripting.FileSystemObject")
        path=fs.GetAbsolutePathName("c:\test.txt")
        if fs.FileExists(path) then
            On Error Resume Next
            Err.Clear
            set f=fs.OpenTextFile("c:\test.txt",1)
            KeyLine=f.ReadLine
            f.Close
            Set f = Nothing
            if Err.Number<>0 then
                Response.Write("Configuration Error 308.3: unable to read encryption file")
                Response.End
            end if
            Err.Clear
            Set Ce=CreateObject("CAPICOM.EncryptedData")           
            Ce.Content=String
            Ce.SetSecret(KeyLine)

            AESEncrypt=Ce.Encrypt

            Set Ce = Nothing ' Clear from memory
            if Err.Number<>0 then
                Response.Write("Configuration Error 308.31: password encryption error")
                Response.End
            end if
        else
            Response.Write("Configuration Error 308.4: encryption file does not exist")
            Response.End
        end if
        Set fs = Nothing

    end if

End Function

DECRYPT

Function AESDecrypt(ByVal String)
    if isnull(String) or String="" then
        AESDecrypt=""
    else
        set fs=Server.CreateObject("Scripting.FileSystemObject")
        path=fs.GetAbsolutePathName("c:\test.txt")
        if fs.FileExists(path) then
            On Error Resume Next
            Err.Clear
            set f=fs.OpenTextFile("c:\test.txt",1)
            KeyLine=f.ReadLine
            f.Close
            Set f = Nothing
            if Err.Number<>0 then
                Response.Write("Configuration Error 308.5: unable to read encryption file")
                Response.End
            end if
            Err.Clear
            Set Cd=CreateObject("CAPICOM.EncryptedData")           
            Cd.SetSecret(KeyLine)
            Cd.Decrypt(String)

            AESDecrypt=Cd.Content

            Set Cd = Nothing ' Clear from memory
            if Err.Number<>0 then
                Response.Write("Configuration Error 308.51:  password decryption error")
                Response.End
            end if
        else
            Response.Write("Configuration Error 308.6:  encryption file does not exist")
            Response.End
        end if
        Set fs = Nothing
    end if

End Function

The C# Code

public string DecryptAES(string strInput)
        {
            StreamReader reader = new StreamReader("C:\\test.txt");
            string strKey = reader.ReadLine();
            reader.Close(); reader.Dispose();//close/remove

            byte[] byteInput = Encoding.ASCII.GetBytes(strInput);
            byte[] byteKey = Encoding.ASCII.GetBytes(strKey);
            MemoryStream MS = new MemoryStream();
            RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();
            CryptoStream CS = new CryptoStream(MS, RC2.CreateDecryptor(byteKey, RC2.IV), CryptoStreamMode.Write);
            CS.Write(byteInput, 0, byteInput.Length);
            CS.FlushFinalBlock();
            return Encoding.UTF8.GetString(MS.ToArray());
        }

Any pointers or help would be great, I've tried so many things, which I don't even know where to start if i would list them all haha.

While not being too keen on encryption algorithms, I can tell you that it seems like you are referencing two different encryption classes between VB.NET and C#.

Maybe you should consider using the same libraries, since they should both be compatible between the .Net languages.

This might be of use

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.