?
Solved

Encrypt a tring

Posted on 2000-02-02
17
Medium Priority
?
293 Views
Last Modified: 2010-04-04
Hi,
I have a string of integers ('0000000000') to the billionths place. Any of the zeroes in that tring can be different. I need to pass this string to a procedure (which im hoping you would help me with) that would:

1. Add any zeroes up to the billionths place. Basically turn '2367' into '0000002368'

To explain this easier, i will assign letters to the zeroes. (A-J)
X stands for a random number from 0 to 9. (2x) represents two random numbers one right after eachother and so on.
I need to encrypt whatever stringis passed in the following way.

(2x)(9-E)(30X)(9-A)(10X)(9-D)(20X)(9-H)(9X)(9-J)(16X)(9-B)(6X)(9-G)(9-F)(36X)(9-C)(84X*)(9-I)(X)

* The 84X are not random numbers, they are derived from already previously set numbers. Take the (2X)(30X)(10X)(20X)(16X)(6X) that were alreay randomly picked and then subtract each one of those numbers from 9 to form the 84 new numbers.

In the end, there should be a total of 224 numbers within which, the billion place number is encrypted.


I realize this encryption sucks but i must do it this way, please do not ask for an explanation.

Thanks a lot.
0
Comment
Question by:MAXcom
  • 8
  • 4
  • 3
  • +2
17 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 3600 total points
ID: 2484172
How about this:

Function EncryptString(S : String) : String;
  Function RandomDigit : Char;
  begin
   // Have your function to generate the random digit here
     Result := ...
  end

  Function RandomDigits(n : integer) ; String;
  var
    I : integer;
  begin  
    result := '';
    for I := 1 to n do
      Result := Result := RandomDigit;
  end;

  Function InvertDigit(ch : char) : Char;
  begin
    Result := chr(ord('9') - ord(s[ord(ch) - ord('A')]));
  end;

  Function InvertDigits(digits : String) : string;
  var
    I : integer;
  begin
    Result := digits;
    for I := 1 to length(digits) do
      digits[i] := chr(ord('9') - (ord(digits[i]) - ord('0'));
  end;

  var
    _2x, _30x, _10x, _20x, _16x, _6x : String;

begin
  Result := '';
  While length(s) < 10 do
    s := '0'+s;

  _2x := RandomDigits(2);
  _30x := RandomDigits(30);
  _10x := RandomDigits(10);
  _20x := RandomDigits(20);
  _16x := RandomDigits(16);
  _6x := RandomDigits(6);

  Result := _2x +
            InvertDigit('E') +
            InvertDigits(_30x) +
            InvertDigit('A') +
            InvertDigits(_10x) +
            InvertDigit('D') +
            InvertDigits(_20x) +
            InvertDigit('H') +
            InvertDigits(RandomDigits(9)) +
            InvertDigit('J') +
            InvertDigits(_16x) +
            InvertDigit('B') +
            InvertDigits(_6x) +
            InvertDigit('G') +
            InvertDigit('F') +
            InvertDigits(RandomDigits(36)) +
            InvertDigit('C') +
            InvertDigits(_2x + _30x + _10x + _20x + _16x + _6x) +
            InvertDigit('I') +
            RandomDigit;
end;

Hows that?

Cheers,

Raymond.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2484184
Opps the invert functions need attention:

  Function InvertDigit(ch : char) : Char;
  var
    Idx : integer;
  begin
    Idx := ord(ch) - ord('A') + 1;
    Result := chr(ord('0' + (ord('9') - ord(s[idx]));
  end;

  Function InvertDigits(digits : String) : string;
  var
    I : integer;
  begin
    Result := digits;
    for I := 1 to length(digits) do
      digits[i] := chr(ord('0') + ord('9') - (ord(digits[i]));
  end;

Cheers,

Raymond.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2484581
listening ..
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 2484641
Oops, Just realised you dont want to 10x, 20x etc inverted the first time around...

The Result := line should then look like this:

  Result := _2x +
            InvertDigit('E') +
            _30x +
            InvertDigit('A') +
            _10x +
            InvertDigit('D') +
            _20x +
            InvertDigit('H') +
            RandomDigits(9) +
            InvertDigit('J') +
            _16x +
            InvertDigit('B') +
            _6x +
            InvertDigit('G') +
            InvertDigit('F') +
            RandomDigits(36) +
            InvertDigit('C') +
            InvertDigits(_2x + _30x + _10x + _20x + _16x + _6x) +
            InvertDigit('I') +
            RandomDigit;

Cheers,

Raymond.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2484791
listening...  (I don't understand the encryption logic)
0
 
LVL 1

Expert Comment

by:AttarSoftware
ID: 2485386
listening... (sat with Madshi) ;O)
0
 

Author Comment

by:MAXcom
ID: 2488180
Adjusted points to 900
0
 

Author Comment

by:MAXcom
ID: 2488181
Ok, first of all thanks a ton for all that code.

I dont think that I explained myself very well.
Letters have nothing to do with this encryption. I used letters just to try and explain the task easier. A represents the first integer (billionths place) B represents second integer (hundred millionths place) and so on.

Also, wheres the part that adds leading zeroes to the string passed so that theres a number in all the places up to the billionth place?

I think im completely not getting your code or something.

I want to end up with a string of 224 numbers with the string of numbers that I originally passed encrypted into it.

Also, shouldnt

 Function RandomDigit : Char;
 begin
 // Have your function to generate the random digit here
 Result := ...
end

look like:
Function RandomDigit : Integer;
begin
Randomize;
Result := random(9);
end;

???

I tried using your code in a new project and it gave me an error in the line:
Result := chr(ord('0' + (ord('9') - ord(s[idx]));

it said "incompatible types"
I definitely think that we are on the right track though.

I think im completely not getting this code. An extra 50 for more comments? :)
Thanks a ton.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2488267
OK, here is the source for the unit I put together to polish it off. I have added appropriate comments as you requested.

I realised you meant 'A'..'J' to be indexes into the string to be encrypted. However, using the characters just made the code a bit more readable. Its a simple matter to change them to be constants which I have done in the code below.

I have left it as a routine that manipulates the number to be converted on a character basis as this just simplifies matters.

I have used randseed instead of randomize - this means that if you encrypt '12345' twice you will get the same results, where you would not with randomize. You may like to write your own random number generator (or use one of the many available) so you are not reliant on the implemantation of the random function remaining the same.


Cheers,

Raymond.

unit Unit11;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Function EncryptString(S : String) : String;

// Return a single random digit
  Function RandomDigit : Char;
  begin
// note that the help reccommends not using random
// for encryption purposes as its implementation may
// change between compiler versions

    Result := Chr(Ord('0') + random(10));
  end;

// Return a string of random digits where 'n' is the
// number of digits requested

  Function RandomDigits(n : integer) : String;
  var
    I : integer;
  begin
    result := '';
    for I := 1 to n do
      Result := Result + RandomDigit;
  end;

// Take a digit in the string to be encrypted, identified
// by the index idx (1..10), and invert it, where inverting
// is defined as 9 - (the value of the digit)
  Function InvertDigit(idx : integer) : Char;
  begin
    Result := chr(ord('0') + (ord('9') - ord(s[idx])));
  end;

// Invert all the digits in the string digits. Each digit in the
// inverted string is defined as 9 - (Value of inverted digit)
  Function InvertDigits(digits : String) : string;
  var
    I : integer;
  begin
    Result := digits;
    for I := 1 to length(digits) do
      digits[i] := chr(ord('0') + ord('9') - (ord(digits[i])));
  end;

  var
    _2x, _30x, _10x, _20x, _16x, _6x : String;

begin
  Result := '';
  RandSeed := 12345; // better than randomize as it reproduces
                     // sequences of random numbers

// Make sure the string to be encrypted has the correct
// number of leading zeros to make it 10 digits long

  While length(s) < 10 do
    s := '0'+s;

// Set up the 6 sets of random digits that will later be
// used for the 84X* term

  _2x := RandomDigits(2);
  _30x := RandomDigits(30);
  _10x := RandomDigits(10);
  _20x := RandomDigits(20);
  _16x := RandomDigits(16);
  _6x := RandomDigits(6);

// Construct the encrypted string
  Result := _2x +
            InvertDigit(5) +
            _30x +
            InvertDigit(1) +
            _10x +
            InvertDigit(4) +
            _20x +
            InvertDigit(8) +
            RandomDigits(9) +
            InvertDigit(10) +
            _16x +
            InvertDigit(2) +
            _6x +
            InvertDigit(7) +
            InvertDigit(6) +
            RandomDigits(36) +
            InvertDigit(3) +
            InvertDigits(_2x + _30x + _10x + _20x + _16x + _6x) +
            InvertDigit(9) +
            RandomDigit;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(inttostr(length(EncryptString('12345'))));
  ShowMessage(EncryptString('12345'));
end;

end.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2488277
Oops :-)

InvertDigits Should look like this:

// Invert all the digits in the string digits. Each digit in the
// inverted string is defined as 9 - (Value of inverted digit)
  Function InvertDigits(digits : String) : string;
  var
    I : integer;
  begin
    Result := digits;
    for I := 1 to length(Result) do
      Result[i] := chr(ord('0') + ord('9') - (ord(Result[i])));
  end;

(Every program has at least one bug :-))

Cheers,

Raymond.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2488796
Hmmm... I still don't understand the logic. Well, if Raymond's sources help you: fine!!
If not: please give some examples for a string before and after the encryption.

Regards, Madshi.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2488925
Hi Madshi:

EncryptString('12345') = '32905634009932754887810741732407198258806730962982965574976507206624257560046060088651698145973700578074755623815890827210070269201614160932056340099327548878107417324071825880673062982965574976507206606008865169814573700551'

Make sense now? :-)

Cheers,

Raymond.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 2489000
Hmmm... Now it is beginning to become clear...   :-)
0
 

Author Comment

by:MAXcom
ID: 2491651
Would this be a good time to ask for code to decrypt it? :)
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2491744
Well, you just need to pick out the digits fromthe encrypted string and invert them, like this:

Function DecryptString(S : String) : String;

// Take a digit in the string to be encrypted, identified
// by the index idx (1..10), and invert it, where inverting
// is defined as 9 - (the value of the digit)
  Function InvertDigit(idx : integer) : Char;
  begin
    Result := chr(ord('0') + (ord('9') - ord(s[idx])));
  end;

begin
  Result := InvertDigit(34) +
            InvertDigit(93) +
            InvertDigit(138) +
            InvertDigit(45) +
            InvertDigit(3) +
            InvertDigit(101) +
            InvertDigit(100) +
            InvertDigit(66) +
            InvertDigit(223) +
            InvertDigit(76);
end;

Cheers,

Raymond.
0
 

Author Comment

by:MAXcom
ID: 2497424
works great for me, thanks a ton!
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2497475
Glad I could help.

I have had an extra thought about using Randseed versus randomize... It might be better to use randomize rather than randseed as this will give more variability between encrypted strings (and as you can tell, the decryption does not rely on any of the random digits...)

Cheers,

Raymond.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

601 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