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 7938459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:49:10+00:00 2026-06-03T22:49:10+00:00

I just inherited this code which is accessed by multiple threads. I just introduced

  • 0

I just inherited this code which is accessed by multiple threads. I just introduced two locks — but I am wondering if there’s anything else I should know. I have no significant experience working in multi-threaded applications.

namespace Helpers.Security
{
    public static class Encryption
    {
        #region "Random Numbers"
        static readonly int[] _randData = {
            //A gigantic list of numbers...
        };
        #endregion

        private static int _randIdx = 0;

        private static readonly object _encryptLock = new object();
        private static readonly object _decryptLock = new object();

        //HG 2009-JUN-11 - Added Reverse Methods from PF's Merge updates in [CDataServerBootStrapper]
        public static string EncryptStringReverse(string c_string)
        {
            return Encrypt(ReverseString(c_string));
        }
        public static string DecryptStringReverse(string c_string)
        {
            return Decrypt(ReverseString(c_string));
        }
        private static string ReverseString(string inputString)
        {
            string result = string.Empty;
            for (int pos = inputString.Length - 1; pos >= 0; pos--)
                result += inputString[pos];

            return result;
        }

        public static string Encrypt(string c_string)
        {
            if (c_string == null || c_string.Equals(string.Empty)) return string.Empty;
            int[] sasc = new int[224];
            char[] chash = new char[224];
            bool isExisting = false;
            string encstr = "";
            int sl = c_string.Length;

            lock (_encryptLock)
            {
                _randIdx = 0;

                for (int v = 0; v < 223; v++)
                {
                    sasc[v] = '\0';
                }
                for (int cl = 0; cl < sl; cl++)
                {
                    for (int a = 0; a < 223; a++)
                    {
                        int rnum = _randData[_randIdx++];
                        for (int y = 0; y < 223; y++)
                        {
                            if (sasc[y] == rnum)
                            {
                                isExisting = true;
                            }
                        }
                        if (isExisting == false)
                        {
                            sasc[a] = rnum;
                            chash[a] = (char) rnum;
                        }
                        else
                            a--;
                        isExisting = false;
                    }
                    chash[223] = '\0';
                    string strhash = new string(chash);
                    for (int v = 0; v < 223; v++)
                    {
                        sasc[v] = '\0';
                    }
                    encstr = encstr + strhash[c_string[cl] - 30];
                }
            }

            // Convert the wide-character string to multibyte string
            string sWholeHex = "";
            foreach (char c in encstr)
            {
                byte val = (byte) c;

                sWholeHex += val.ToString("X2");
            }

            return (sWholeHex.Trim().Replace("\0", ""));
        }

        public static string Decrypt(string c_string)
        {
            if (c_string == null || c_string.Equals(string.Empty)) return string.Empty;

            string szTemp = c_string;
            int nCtr = 0;
            byte[] byToDecrypt = new byte[1024];
            char[] chash = new char[223];
            char[] cencstr = new char[5000];
            int[] sasc = new int[223];
            bool isExisting = false;

            lock (_decryptLock)
            {
                for (int b = 0; b < 1024; b++)
                    byToDecrypt[b] = 0;

                int r;
                string sToDecrypt = string.Empty;
                for (r = 0; r < szTemp.Length - 1; r += 2)
                {
                    byte b2 = 0;
                    char c = szTemp[r];
                    if (c >= '0' && c <= '9')
                        b2 += (byte) (c - '0');
                    else if (c >= 'A' && c <= 'Z')
                        b2 += (byte) (c - 'A' + 10);

                    b2 *= 16;
                    c = szTemp[r + 1];
                    if (c >= '0' && c <= '9')
                        b2 += (byte) (c - '0');
                    else if (c >= 'A' && c <= 'Z')
                        b2 += (byte) (c - 'A' + 10);

                    byToDecrypt[nCtr++] = b2;
                    sToDecrypt += (char) b2;
                }

                _randIdx = 0;

                int sl = sToDecrypt.Length;

                for (int v = 0; v < 223; v++)
                {
                    sasc[v] = '\0';
                }
                int cl;
                for (cl = 0; cl < sl; cl++)
                {
                    for (int a = 0; a < 223; a++)
                    {
                        int rnum = _randData[_randIdx++];
                        for (int y = 0; y < 223; y++)
                        {
                            if (sasc[y] == rnum)
                            {
                                isExisting = true;
                            }
                        }
                        if (isExisting == false)
                        {
                            sasc[a] = rnum;
                            chash[a] = (char) rnum;
                        }
                        else
                        {
                            a--;
                        }
                        isExisting = false;
                    }
                    string strhash = new string(chash);
                    int v;
                    for (v = 0; v < 223; v++)
                    {
                        if (sToDecrypt[cl] == strhash[v])
                            cencstr[cl] = (char) ((byte) (v + 30));
                    }

                    for (v = 0; v < 223; v++)
                    {
                        sasc[v] = 0;
                    }
                }
                cencstr[cl] = '\0';
            }

            string encstr = new string(cencstr);

            return (encstr.Trim().Replace("\0", ""));
        }
    }
}

My only thoughts were that there’s a lot of work going on here so I should wrap all the work in a lock to make sure I don’t just push the locking issue elsewhere. That being said — only two variables are globally accessible… _randIdx and _randData. I am not sure if that plays a factor in deciding what to lock.

  • 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-03T22:49:12+00:00Added an answer on June 3, 2026 at 10:49 pm

    That depends on what kind of thread safety you need.

    _randIdx can be both set and got by two different threads because your encrypt and decrypt method use different objects to lock around. You’re only performing reads on the _randData array in each lock, so that’s a non issue.

    It seems like it would be trivially easy to make this non lock required by simply using a temporary index local to just that one method. Whether that is valid or not is up to you.

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

Sidebar

Related Questions

I've just inherited this code in PHP which seems to do some kind of
I just inherited some code which makes me uneasy: There is a testing library,
I just inherited code which hides/shows rows a UITableView by using the delegate heightForRowAtIndexPath
I'm looking at some vb.net code I just inherited, and cannot fathom why the
I have just inherited an asp.net mvc 3 site operating on .net 4 which
So I've inherited a large c++ code base which does a lot of mysql
I have inherited a piece of code which has a series of functions like
I've just inherited (ahem) a QNX realtime project which uses a void*/downcasting/case statement mechanism
I have some code I inherited which has a lot of warnings that I
I thought I could just throw this out there and just ask: I have

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.