-1

I have two functions:

function EnCryptSymbolVij(text, key:char; abc:string):char;
var st: string;
    positionText: word;
begin
  if text=' ' then begin EnCryptSymbolVij:=' '; exit; end;
  PositionText:=pos(text,abc);
  if positionText=0 then exit;
  st:=MoveVij(length(abc)-pos(Key,abc)+1, abc);
  EnCryptSymbolVij:=st[positionText];
end;

function EnCryptVij(text, key, abc:string):string;
var i, n, j: longword;
    st:string;
begin
setlength(st,length(text));
j:=1;
for i:=1 to length(text) do
  begin
    st[i]:=EnCryptSymbolVij(text[i], Key[j], abc);
    inc(j);
    if j>length(key) then j:=1;
  end;
EnCryptVij:=st;
End;

In the first function, I add space support. But when I encrypt the text with spaces, I get a wrong encryption, because the second function counts spaces like a part of alphavit. How can I fix this and ignore encrypt spaces in the second function? Please help to resolve this trouble.

Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
  • 1
    What is the specification of this function? We can't guess what it is meant to do. Also, EnCryptSymbolVij can return without assigning to the return value. The compiler will warn about that. Don't ignore warnings. – David Heffernan Jul 23 '17 at 18:19
  • ISTM that a true Vigenère cipher only encrypts plain alpha. No punctuation, no spaces, etc. This one seems to be able to handle punctuation, lower and upper case and spaces too: https://rosettacode.org/wiki/Vigen%C3%A8re_cipher#Pascal. Of course punctuation and spaces are simply ignored and letters are made upper case, so the result is slightly less readable, e.g. `WEATTACKATDAWN` instead of `We attack at dawn!`. – Rudy Velthuis Jul 23 '17 at 20:59
  • So do the following: take the original message. Remove all punctuation and spaces. Convert it to upper case. Do exactly the same with the key. Then combine those two using a (virtual) Vigenère square and put the result in a third string. – Rudy Velthuis Jul 23 '17 at 21:24
  • @RudyVelthuis, Thank you for your answer! But i can't understand how removing punctuation and spaces and converting to upper case can help to add spaces in result. Thank you. – Ranshe Byloluchshe pipe00000 Jul 24 '17 at 05:39
  • We can't understand what the specification of your function is. Do you know? – David Heffernan Jul 24 '17 at 06:04
  • You can't have spaces in the result. Vigenère only works on the letters A-Z. Everything else must be removed and will be lost forever. Take a look at the code I linked to. – Rudy Velthuis Jul 24 '17 at 06:19
  • I already said that encoding and then decoding "We attack at dawn" will result in "WEATTACKATDAWN". – Rudy Velthuis Jul 24 '17 at 06:24
  • Need to do spaces between words like in this website [Link](https://planetcalc.com/2463/?language_select=en). (Not encrypt spaces in cipher. Ignore space symbol in second function, i dont know how to do it right.) – Ranshe Byloluchshe pipe00000 Jul 24 '17 at 07:40

1 Answers1

1

You only support alphabetic characters and spaces. If you want to do more you need to be a bit cleverer in you code. Spaces are not encrypted, and nothing encrypts to a space so you just need to add that test in your decryption. If you later support other non alphabetic characters you will need to do the same for these too, but the test is the same for both encryption and decryption. So for spaces, your decryption code for a symbol will start just as your encryption does, like this

function DeCryptSymbolVij(text, key:char; abc:string):char;
var st: string;
    positionText: word;
begin
  if text=' ' then begin DeCryptSymbolVij:=' '; exit; end;

Personally I prefer to use 'Result' rather than the function name, but that is just personal preference.

Edit

To cater for spaces in the second function, you could just surround the inc(j) with a test for spaces, like this

function EnCryptVij(text, key, abc:string):string;
var i, n, j: longword;
    st:string;
begin
setlength(st,length(text));
j:=1;
for i:=1 to length(text) do
  begin
    st[i]:=EnCryptSymbolVij(text[i], Key[j], abc);
    if st[ I ] <> ' ' then
    begin
      inc(j);
    end;
    if j>length(key) then j:=1;
  end;
EnCryptVij:=st;
End;

but to 'future proof' it you might be better adding a Var boolean parameter to the first function and setting it if a substitution has occurred:

function EnCryptSymbolVij(text, key:char; abc:string; var changed : boolean):char;
var st: string;
    positionText: word;
begin
  changed := FALSE; 

  PositionText:=pos(text,abc);
  if positionText=0 then exit;
  // else
  changed := TRUE;
  st:=MoveVij(length(abc)-pos(Key,abc)+1, abc);
  EnCryptSymbolVij:=st[positionText];
end;

function EnCryptVij(text, key, abc:string):string;
var i, n, j: longword;
  iChanged : Boolean;
    st:string;
begin
setlength(st,length(text));
j:=1;
for i:=1 to length(text) do
  begin
    st[i]:=EnCryptSymbolVij(text[i], Key[j], abc, iChanged);
    if iChanged then
    begin
      inc(j);
    end;
    if j>length(key) then j:=1;
  end;
EnCryptVij:=st;
End;

Note that the space test is not required in the first function (assuming there is not a space in 'abc')

Dsm
  • 5,870
  • 20
  • 24
  • Thanks for answer, but with this code in encryption `if text=' ' then begin EnCryptSymbolVij:=' '; exit; end;` i have wrong encryption, like: (text: "attack at down"; key: "lemon"; result: "lxfopv mh osib" instead "lxfopv ef rbhr") because second function think space " " is part of alphabet what need to encrypt. – Ranshe Byloluchshe pipe00000 Jul 24 '17 at 09:07
  • Thank you so much! This is exactly what i needed for. You are geneus. Thanks! – Ranshe Byloluchshe pipe00000 Jul 24 '17 at 10:26