Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to call VFP function from .NET C# code?
Message
De
05/01/2015 04:08:56
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
VB 9.0
OS:
Windows Server 2012
Network:
Windows 2008 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01612997
Message ID:
01613045
Vues:
59
>>>>>>>>If all this function does is encrypt a password there are lots of examples written in c# out there ( Google is your friend )
>>>>>>>
>>>>>>>I did but so for I could not find a simple enough c# encrypt function that can be "duplicated" in VFP. You see I need a function that would do the same from VFP and c# end.
>>>>>>>Thank you.
>>>>>>
>>>>>>Presumably you mean that the C# version should be usable with strings already encoded using the VFP cipher library that you mentioned?
>>>>>>
>>>>>>Al's pointer to Rick's paper is still valid - you could create a small VFP com interop which could be accessed from .net.
>>>>>>
>>>>>>But which functions of that library do you actually use ? - the core encryption is fairly straightforward.
>>>>>>I took a look at the original C code and it would seem that a C# version should be simple enough to implement......
>>>>>
>>>>>The function cipher() I am using in VFP code is written in VFP; not in C. Maybe Sergey also wrote it in C but the one he recommended to me years ago was the VFP function. To be honest, I don't even understand all VFP functions he used in creating the cipher(). So converting/rewriting it to C# would be a great challenge.
>>>>
>>>>The VFP code was based on a C conversion and there's a link to the C source - you might find that easier to convert to C# (or not :-} )
>>>
>>>Do you know where I can find the DLL (I presume tha the C code was built into a DLL) file that uses this C code? And maybe it has the C code there as well.
>>
>>Does this work (not properly tested and could certainly be improved):
using System;
>>using System.Linq;
>>using System.Text;
>>
>>namespace Cipher
>>{
>>    public class Cipher
>>    {
>>        private const int PwMinNum = 1000;
>>
>>        public string Encrypt(string tcStr, string tcPassword)
>>        {
>>            byte[] tcStrBytes = Encoding.ASCII.GetBytes(tcStr);
>>            int tcStrLength = tcStrBytes.Length;
>>
>>            string lcPassword = tcPassword + '0';
>>            byte[] lcPasswordBytes = Encoding.ASCII.GetBytes(lcPassword);
>>            int lnPassLen = lcPasswordBytes.Length - 1;
>>
>>            int lnPassPos = 0;
>> 
>>            string lcStrOut = string.Empty;
>>
>>            int lnPassNum = ((CipherGetPnum(lcPassword)/997 - 1)%254) + 1;
>>            
>>            for (int i = 0; i < tcStrLength;i++)
>>            {
>>                int lnNum01 = ((lnPassNum + (i - tcStrLength)) - 1);
>>                lnPassNum = (Math.Abs(lnNum01)%254)*Math.Sign(lnNum01) + 1;
>>                var lnByte = tcStrBytes[i] ^ (lnPassNum ^ lcPasswordBytes[lnPassPos]);
>>                lnByte = lnByte & 0xFF;
>>                lcStrOut = lcStrOut + (lnByte == 0 ? Convert.ToChar( tcStrBytes[i + 1]) : Convert.ToChar(lnByte));
>>                lnPassPos = lnPassPos>=lnPassLen ? 1:lnPassPos + 1;
>>            }
>>            return lcStrOut;
>>        }
>>
>>        private int CipherGetPnum(string tcStr)
>>        {
>>            byte[] ascii = Encoding.ASCII.GetBytes(tcStr);
>>            int liRet = 1 + ascii.Select((t, i) => t + i).Sum();
>>
>>            while (liRet < PwMinNum)
>>            {
>>                liRet = liRet << 1;
>>            }
>>            return liRet;
>>        }
>>    }
>>}
>
>This is my C# code:
>
>   public class VfpCipher
>    {
>
>        public static string Cipher(string cString, string tcPassword)
>        {
>            string lcPassword;
>            int lnPassLen;
>            int lnPassPos;
>            int iAsciiValue;
>            int lnPassNum;
>            int lnStrLen;
>
>            lnStrLen = cString.Length; 
>
>            char ChrZero;
>            ChrZero = (char)0;
>
>            lcPassword = tcPassword + ChrZero.ToString(); // in VFP this was add CHR(0) to the Password string
>            lnPassLen = lcPassword.Length; 
>
>            int[] laPassword = new int[lnPassLen+1];
>            for (lnPassPos = 0; lnPassPos < lnPassLen; lnPassPos++)
>            {
>                // Get one-character from the Password string and store the ASC()/ASCII value of it to the array.
>                var charByte = System.Text.Encoding.GetEncoding(1252).GetBytes(lcPassword.Substring(lnPassPos, 1));
>                laPassword[lnPassPos] = Convert.ToInt32(charByte[0]);
>            }
>
>            // Get seed value
>            lnPassNum = (int)((((CipherGetPnum(lcPassword) / 997) - 1) % 254) + 1);
>
>            int lnByte;
>            string lcStrOut = "";
>            lnPassPos = 0;  // in VFP this is 1 but in C# the array is zero-based.
>            int lnNum01;
>            
>            //  Encode/decode each character
>            for (int lnInPos=0; lnInPos < lnStrLen-1; lnInPos++)
>            {
>                // Get new seed value
>                lnNum01 = (( lnPassNum + (lnInPos - lnStrLen)) - 1);
>                lnPassNum = ( Math.Abs(lnNum01) % 254 ) * Math.Sign(lnNum01) + 1;
>
>                // Get the character byte and extract the ASCII value of the character.
>                var charByte = System.Text.Encoding.GetEncoding(1252).GetBytes(cString.Substring(lnInPos, 1));
>
>                iAsciiValue = Convert.ToInt32(charByte[0]);
>                lnByte = (iAsciiValue ^ (lnPassNum ^ laPassword[lnPassPos]));
>
>                // Convert signed value to unsigned, if necessary
>	            lnByte = lnByte & 0xFF;
>	            
>                // If result is zero, use current character
>                // Get one character string value of lnByte
>                char cOneChar;
>                cOneChar = (char)lnByte;
>                string sOneChar;
>                sOneChar = cOneChar.ToString();
>	            lcStrOut = lcStrOut + (lnByte.Equals(0) ? cString.Substring(lnInPos, 1) : sOneChar);
>
>                lnPassPos = (lnPassPos.CompareTo(lnPassLen-1) >= 0 ? 0 : lnPassPos + 1);
>            }
>
>            return lcStrOut;
>
>        }
>
>        public static int CipherGetPnum(string SeedString)
>        {
>            int liRet = 1;
>            int iAsciiValue;
>            for (int lnPos = 0; lnPos < SeedString.Length; lnPos++)
>            {
>                var charByte = System.Text.Encoding.GetEncoding(1252).GetBytes(SeedString.Substring(lnPos, 1));
>                iAsciiValue = Convert.ToInt32(charByte[0]);
>                liRet = liRet + iAsciiValue + lnPos;
>            }
>
>            // Note that in Sergey's code the value 1000 is declared as constant.
>            do 
>            {
>                liRet = liRet << 1;
>            } while (liRet < 1000);
>
>            return liRet;
>
>        }
>    }
>
Hmm. Does that work correctly - looks as if you are not processing the last character of the string to be encrypted ?
Not important but you don't need to loop through each character in the string to build an array - GetBytes() against the whole string will do it for you....
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform