Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:49:20+00:00 2026-06-15T11:49:20+00:00

I am trying to write the xor encryption in C# as sagepay encryption is

  • 0

I am trying to write the xor encryption in C# as sagepay encryption is only documented in VB.

the vb code is:

Public Shared Function simpleXor(ByVal strIn As String, ByVal strKey As String) As String
    Dim iInIndex As Integer
    Dim iKeyIndex As Integer
    Dim strReturn As String
    If Len(strIn) = 0 Or Len(strKey) = 0 Then
        simpleXor = ""
        Exit Function
    End If

    iInIndex = 1
    iKeyIndex = 1
    strReturn = ""

    '** Step through the plain text source XORing the character at each point with the next character in the key **
    '** Loop through the key characters as necessary **
    Do While iInIndex <= Len(strIn)
        strReturn = strReturn & Chr(Asc(Mid(strIn, iInIndex, 1)) Xor Asc(Mid(strKey, iKeyIndex, 1)))
        iInIndex = iInIndex + 1
        If iKeyIndex = Len(strKey) Then iKeyIndex = 0
        iKeyIndex = iKeyIndex + 1
    Loop

    simpleXor = strReturn
End Function

So far I have converted this to

        public static String SimpleXOR(String strIn, String strKey)
    {
        Int32 iInIndex, iKeyIndex;
        String strReturn;
        iInIndex = 1;
        iKeyIndex = 1;
        strReturn = "";

        while (iInIndex <= strIn.Length)
        {
            strReturn = strReturn & Strings.Chr(Strings.Asc(Strings.Mid(strIn, iInIndex, 1)) ^ Strings.Asc(Strings.Mid(strKey, iKeyIndex, 1)));
                iInIndex = iInIndex + 1;
            if (iKeyIndex == strKey.Length) iKeyIndex = 0;
            iKeyIndex = iKeyIndex + 1;

        }

    }

The problem is I didn’t understand what this line is doing

strReturn = strReturn & Chr(Asc(Mid(strIn, iInIndex, 1)) Xor Asc(Mid(strKey, iKeyIndex, 1)))

so I ran it through a vb to c# converter and got the above. But it clearly is not valid c# code as far as I am aware.

Can anyone help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:49:21+00:00Added an answer on June 15, 2026 at 11:49 am

    There are two ways I can think of to do this:

    First, grab the current System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage of the thread, pass that into a new Encoding class instance using the GetEncoding method, then convert the strings to byte arrays using the Encoding instance’s GetBytes method

        public static string SimpleXOR(string strIn, string strKey)
        {
            if (strIn.Length == 0 || strKey.Length == 0)
            {
                return string.Empty;
            }
    
            int inIndex = 0;
            int keyIndex = 0;
            string returnString = string.Empty;
    
            var currentCodePage = System.Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
            var encoding = Encoding.GetEncoding(currentCodePage);
    
            var inString = encoding.GetBytes(strIn);
            var keyString = encoding.GetBytes(strKey);
    
            while (inIndex < inString.Length)
            {
                returnString += (char)(inString[inIndex] ^ keyString[keyIndex]);
    
                inIndex++;
    
                if (keyIndex == keyString.Length - 1)
                {
                    keyIndex = 0;
                }
                else
                {
                    keyIndex++;
                }
            }
    
            return returnString;
        }
    

    The other, simpler way would be to add a reference to Microsoft.VisualBasic in your C# project and let the converter generated code run. The Strings class will be avaliable, allowing you to just do Strings.Chr, Strings.Asc, and Strings.Mid.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m trying write code that after reset set up rrpmax as 3000. It
I am trying write a function that generates simulated data but if the simulated
Trying to write a function to see how often an object exists and give
Trying to write a code at the moment that basically tests to see if
I've had problem when trying write a function which has a default value when
I am trying to learn python, I was trying write C/C++ code i used
I'm trying to write a function to do a particular job (in my case,
I am trying to create a truth table for the function F=(A&B) XOR (C&D').
I'm trying to convert a string I read with this code to binary and
I referred to this article http://www.codeproject.com/KB/security/DotNetCrypto.aspx and I am trying write an encrypted string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.