Solved

Modify CaesarShift  - Rot-13

Posted on 2010-09-01
4
548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

What, When and Where - Security Threats from Q1

Join Corey Nachreiner, CTO, and Marc Laliberte, Information Security Threat Analyst, on July 26th as they explore their key findings from the first quarter of 2017.

Question has a verified solution.

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

When the confidentiality and security of your data is a must, trust the highly encrypted cloud fax portfolio used by 12 million businesses worldwide, including nearly half of the Fortune 500.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

627 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