Solved

Modify CaesarShift  - Rot-13

Posted on 2010-09-01
4
524 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:ewangoya
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:ewangoya
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:
ewangoya 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 Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Article by: btan
Provide an easy one stop to quickly get the relevant information on common asked question on Ransomware in Expert Exchange.
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now