[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1682
  • Last Modified:

Encrypt , decrypt in delphi and C#

aAyone can show me two functions encrypt(input,key) and decrypt(input,key)
that working both in c# and delphi.
because I want to encypt in delphi then can decrypt in c#
encypt in c# then can decrypt in delphi.
Thank experts.
1
lhquang
Asked:
lhquang
  • 4
  • 2
1 Solution
 
Jase-CoderCommented:
here are some very simple examples


encrypt in C#

        public char [] Encrypt(string Text, Int32 Shift)
        {
            char []Str = new char[Text.Length];
            for (int i = 0; i < Text.Length; i++)
            {
                Str[i] = Convert.ToChar(Convert.ToByte(Text[i]) + Shift);    
            }
            return Str;
        }



decrpty in Delphi:

procedure Decrypt(Text : String, Shift : Integer)
begin
  for int i := 0 to Length(Text) do
    Text[i] -= Integer;
end;


I have not beenable to test the delphi since I no longer have the compiler. Just make sure the Shift value used is the same in the C# code as in the Delphi code;
0
 
Jase-CoderCommented:
BTW another simple encryption method is to XOR the current letter with a number. This will generate a new character. To get the old value back you just simple XOR the character again using the same key value
0
 
lhquangAuthor Commented:
please show me the both encrypt and decrypt function in both delphi and c# (total 4 functions)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Jase-CoderCommented:
       public char [] Encrypt(string Text, Int32 Shift)
        {
            char []Str = new char[Text.Length];
            for (int i = 0; i < Text.Length; i++)
            {
                Str[i] = Convert.ToChar(Convert.ToByte(Text[i]) + Shift);    
            }
            return Str;
        }

        public char [] Decrypt(string Text, Int32 Shift)
        {
            char []Str = new char[Text.Length];
            for (int i = 0; i < Text.Length; i++)
            {
                Str[i] = Convert.ToChar(Convert.ToByte(Text[i]) - Shift);    
            }
            return Str;
        }

--------------------------------------------------------------------------------------------------------

delphi


procedure Decrypt(Text : String, Shift : Integer)
begin
  for int i := 0 to Length(Text) do
    Text[i] := char(ord(Text[i]) - Integer);
end;

procedure Encrypt(Text : String, Shift : Integer)
begin
  for int i := 0 to Length(Text) do
    Text[i] := char(ord(Text[i]) + Integer);
end;
0
 
Jase-CoderCommented:
I made a mistake in the delphi code:

Text[i] := char(ord(Text[i]) + Integer);

should be:  Text[i] := char(ord(Text[i]) + Shift);   & Text[i] := char(ord(Text[i]) - Shift);
0
 
lhquangAuthor Commented:
wow, thanks you very much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now