Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,844 questions

51,765 answers

573 users

How to use XOR to encrypt and decrypt a string in Pascal

2 Answers

0 votes
program XORCipher;

function XORCipher(const Input: string; const Key: Char): string;
var
  i: Integer;
begin
  SetLength(XORCipher, Length(Input));
  for i := 1 to Length(Input) do
    XORCipher[i] := Chr(Ord(Input[i]) xor Ord(Key));
end;

var
  Str, EncryptedText, DecryptedText: string;
  Key: Char;
begin
  // Input data
  Str := 'May the Force be with you.';
  Key := 'K'; // Key for XOR operation

  // Encrypt the string
  EncryptedText := XORCipher(Str, Key);
  Writeln('Encrypted Text: ', EncryptedText);

  // Decrypt the string (same function, same key)
  DecryptedText := XORCipher(EncryptedText, Key);
  Writeln('Decrypted Text: ', DecryptedText);
end.



(*
run:

Encrypted Text: *2k?#.k$9(.k).k<"?#k2$>e
Decrypted Text: May the Force be with you.

*)

 



answered Aug 16, 2025 by avibootz
0 votes
program XORCipher;

function XORCipher(const Input, Key: string): string;
var
  i, keyIndex: Integer;
begin
  SetLength(XORCipher, Length(Input));
  for i := 1 to Length(Input) do
  begin
    keyIndex := ((i - 1) mod Length(Key)) + 1;
    XORCipher[i] := Chr(Ord(Input[i]) xor Ord(Key[keyIndex]));
  end;
end;

var
  Str, Key, EncryptedText, DecryptedText: string;
begin
  // Input data
  Str := 'May the Force be with you.';
  Key := 'Obelus'; // String key for XOR operation

  // Encrypt the string
  EncryptedText := XORCipher(Str, Key);
  Writeln('Encrypted Text: ', EncryptedText);

  // Decrypt the string (same function, same key)
  DecryptedText := XORCipher(EncryptedText, Key);
  Writeln('Decrypted Text: ', DecryptedText);
end.




(*
run:

Encrypted Text: L*B#*B	U&L:L
Decrypted Text: May the Force be with you.

*)

 



answered Aug 16, 2025 by avibootz
...