• Status: Solved
• Priority: Medium
• Security: Public
• Views: 519

# Encryption using XOR

I'm doing at school project about simple cryptography.
My assignement is too write a simple program that takes an input (string), converts it to binary form and then uses a 8 bit key to encrypt the message (using the XOR function).
The answer should be shown both in binary form (1 and 0's and in ascii).
Could anyone write an example, i'm kinda in hurry...
This is the function i use to convert to binary :

Function ConvertToBinary(TheInt: Integer): String;
Const
Var
SizeOfVar,Bytes,I: Byte;
WorkByte: ^Byte;
OutPutString: String;

Begin
OutPutString:='';
WorkByte:=Ptr(Seg(TheInt),Ofs(TheInt));
For Bytes:=1 to 2 do
Begin
For I:=0 to 7 do
Begin
If (WorkByte^ And BitMask) > 0 Then
OutPutString:='1'+OutPutString
Else
OutPutString:='0'+OutPutString;
End;
Inc(WorkByte);
End;
ConvertToBinary:=OutPutString;
End;
0
PsychoFox
• 2
• 2
1 Solution

Commented:
you don't have to convert it. you just can go trough the string :

***********************************************************

uses crt;

var instr,outstr:string;i:integer;

procedure encode(cv:byte;instr:sting; var outstr:string);

begin
outstr:=instr;   {put the correct lenght of instr}
for i:=1 to length(instr) do {go trough the string}
mem[seg(outstr):ofs(outstr)+i]:=(mem[seg(instr):ofs(instr)+i]) xor cv;  {encode it}
end;

begin
instr:='this is a little test, and a very good text !'
writeln(instr);
encode(53,instr,outstr);  {encode with 53 as value}
writeln(outstr);
encode(53,outstr,instr);  {decode with 53 as value}
writeln(instr);  {here should appear the same text again}
end.

**********************************************************

if you've got any questions, mail me : columbo666@iname.com

hope, 'could help
columbo666
0

Author Commented:
Thank you! The encoding works great.

I will accept your answer as soon as my program is ready (i may have some more questions :) ).
0

Commented:
cool. mail me
0

Author Commented:
Explanation :
1011       This is the first key
abcd       Just to make i more clear

1        XOR       1             =    0
a                  a

1        XOR       0             =    1
a                  b

1        XOR       1             =    0
a                  c

1        XOR       1             =    0
a                  d

From the first bit we got this new key  0100

We go on to the next bit

0        XOR       1             =    1
b                  a

0        XOR       0             =    0
b                  b

0        XOR       1             =    1
b                  c

0        XOR       1             =    1
b                  d

This will become                     1011

And so on with all the four bits until we have a
16bit key.

Could you help me writing a procedure that does that?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.