Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Modify CaesarShift  - Rot-13

Posted on 2010-09-01
4
Medium Priority
?
556 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 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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
Encryption for Business Encryption (https://en.wikipedia.org/wiki/Encryption) ensures the safety of our data when sending emails. In most cases, to read an encrypted email you must enter a secret key that will enable you to decrypt the email. T…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Suggested Courses

721 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