Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Modify CaesarShift  - Rot-13

Posted on 2010-09-01
4
Medium Priority
?
563 Views
Last Modified: 2012-05-10
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
Comment
Question by:Looking_4_Answers
  • 3
4 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33581938
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
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 33582118
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
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 2000 total points
ID: 33598839
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
 

Author Closing Comment

by:Looking_4_Answers
ID: 33613302
VERY NICE!  THANK YOU!

0

Featured Post

Put Machine Learning to Work--Protect Your Clients

Machine learning means Smarter Cybersecurity™ Solutions.
As technology continues to advance, managing and analyzing massive data sets just can’t be accomplished by humans alone. It requires huge amounts of memory and storage, as well as the high-speed power of the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you thought ransomware was bad, think again! Doxware has the potential to be even more damaging.
The conference as a whole was very interesting, although if one has to make a choice between this one and some others, you may want to check out the others.  This conference is aimed mainly at government agencies.  So it addresses the various compli…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question