Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Modify CaesarShift - Rot-13

Modify CaesarShift  -Rot-13

i have the following code, which i would like to extend. It currently is only ROT-13.
I would like to add all the various shifts

i call it as such:
memMain.Lines.Text:= CaesarShift(trim(memMain.Lines.Text));

I  would like to add all the shifts (not just 13) as found on this page:

http://web.forret.com/tools/rot13.asp?rot=13&cipher=V%27Z+GELVAT+GB+GRNPU+GUR+PNIRZRA+GB+CYNL+FPENOOYR.+VG%27F+HCUVYY+JBEX.+GUR+BAYL+JBEQ+GURL+XABJ+VF+%27HAU%27%2C+NAQ+GURL+QBA%27G+XABJ+UBJ+GB+FCRYY+VG.%09&dir=decrypt&clear=I%27M+TRYING+TO+TEACH+THE+CAVEMEN+TO+PLAY+SCRABBLE.+IT%27S+UPHILL+WORK.+THE+ONLY+WORD+THEY+KNOW+IS+%27UNH%27%2C+AND+THEY+DON%27T+KNOW+HOW+TO+SPELL+IT.%09

I would like to add a parameter for passing in the shift (Alphabet Shift)

-13
-12
-11
-10
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
1
2
3
4
5
6
7
8
9
10
11
12
13


ALSO THE OPTIONS FOR :  

1.)Case-sensitive (and leave accents)
2.) Reverse input first


thanks


function CaesarShift(AStr: String): String;
var
  i: integer;
  c: char;
begin
  for i := 1 to Length(AStr) do
  begin
    c := AStr[i];
    if (((c >= 'A') AND (c <= 'M')) OR ((c >= 'a') AND (c <= 'm'))) then
      c := chr(ord(c) + 13)
    else if (((c >= 'N') AND (c <= 'Z')) OR ((c >= 'n') AND (c <= 'z'))) then
      c := chr(ord(c) - 13);
    Result := Result + c;
  end;
end;

Open in new window

0
Looking_4_Answers
Asked:
Looking_4_Answers
  • 3
1 Solution
 
Ephraim WangoyaCommented:
try
type
  TCeaserMethod = (cmDecrypt, cmEncrypt);

  TCeaserShift = class(TObject)
  public
    class function Resolve(const AValue: string; AMethod: TCeaserMethod; const Delta: Integer): string;
  end;

{ TCeaserShift }

class function TCeaserShift.Resolve(const AValue: string; AMethod: TCeaserMethod;
  const Delta: Integer): string;
var
  c: char;
  I, lDelta, Len: Integer;
begin
  if (Delta > 13) or (Delta < -13) then
    raise exception.Create('Error Message');

  Len := Length(AValue);
  SetLength(Result, Len);

  if AMethod = cmDecrypt then
    lDelta := -Delta
  else
    lDelta := Delta;

  for I := 0 to Len do
  begin
    c := AValue[I];
    case c of
      'A'..'Z':
      begin
        c := char(Ord(c) + lDelta);
        if c > 'Z' then
          c := Char((Ord(c) - Ord('Z')) + Ord('A') - 1)
        else if c < 'A' then
          c := char(Ord('Z') - (Ord('A') - Ord(c)) + 1);
      end;
      'a'..'z':
      begin
        c := char(Ord(c) + lDelta);
        if c > 'z' then
          c := Char((Ord(c) - Ord('z')) + Ord('a') - 1)
        else if c < 'a' then
          c := char(Ord('z') - (Ord('a') - Ord(c)) + 1);
      end;
    end;
    Result[I] := c;
  end;
end;

use
  EncryptedText := TCeaserShift.Resolve(PlainText, cmEncrypt, 3);
  PlainText := TCeaserShift.Resolve(EncryptedText, cmDecrypt, 3);
0
 
Ephraim WangoyaCommented:
Add reverse as follows

type
  TCeaserMethod = (cmDecrypt, cmEncrypt);

  TCeaserShift = class(TObject)
  public
    class function Resolve(const AValue: string; AMethod: TCeaserMethod;
      const Delta: Integer; AReverse: Boolean = False): string;
    class function Reverse(const AValue: string): string;
  end;

{ TCeaserShift }

class function TCeaserShift.Resolve(const AValue: string; AMethod: TCeaserMethod;
  const Delta: Integer; AReverse: Boolean): string;
var
  c: char;
  I, lDelta, Len: Integer;
  lValue: string;
begin
  if (Abs(Delta) > 13) or (Abs(Delta) < 1) then
    raise exception.Create('Error Message');

  Len := Length(AValue);
  SetLength(Result, Len);

  lValue := AValue;
  if AMethod = cmEncrypt then
  begin
    lDelta := Delta;
    if AReverse then
      lValue := Reverse(lValue);
  end
  else
    lDelta := -Delta;

  for I := 1 to Len do
  begin
    c := lValue[I];
    case c of
      'A'..'Z':
      begin
        c := char(Ord(c) + lDelta);
        if c > 'Z' then
          c := Char((Ord(c) - Ord('Z')) + Ord('A') - 1)
        else if c < 'A' then
          c := char(Ord('Z') - (Ord('A') - Ord(c)) + 1);
      end;
      'a'..'z':
      begin
        c := char(Ord(c) + lDelta);
        if c > 'z' then
          c := Char((Ord(c) - Ord('z')) + Ord('a') - 1)
        else if c < 'a' then
          c := char(Ord('z') - (Ord('a') - Ord(c)) + 1);
      end;
    end;
    Result[I] := c;
  end;

  if (AMethod = cmDecrypt) and AReverse then
    Result := Reverse(Result);
end;

class function TCeaserShift.Reverse(const AValue: string): string;
var
  I, J, Len: Integer;
begin
  Len := Length(AValue);
  SetLength(Result, Len);

  I := 1;
  J := Len;
  while I <= Len do
  begin
    Result[I] := AValue[J];
    Inc(I);
    Dec(J);
  end;
end;

Case sensitivity makes no difference
0
 
Ephraim WangoyaCommented:
Oops
change this  
  if (Abs(Delta) > 13) or (Abs(Delta) < 1) then
    raise exception.Create('Error Message');
to as in first example
if (Delta > 13) or (Delta < -13) then
    raise exception.Create('Error Message');
0
 
Looking_4_AnswersAuthor Commented:
VERY NICE!  THANK YOU!

0

Featured Post

Who's Defending Your Organization from Threats?

Protecting against advanced threats requires an IT dream team – a well-oiled machine of people and solutions working together to defend your organization. Download our resource kit today to learn more about the tools you need to build you IT Dream Team!

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