Encrypt a tring

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.
MAXcomAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwilson032697Commented:
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
intheCommented:
listening ..
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

rwilson032697Commented:
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
MadshiCommented:
listening...  (I don't understand the encryption logic)
0
AttarSoftwareCommented:
listening... (sat with Madshi) ;O)
0
MAXcomAuthor Commented:
Adjusted points to 900
0
MAXcomAuthor Commented:
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
rwilson032697Commented:
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
rwilson032697Commented:
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
MadshiCommented:
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
rwilson032697Commented:
Hi Madshi:

EncryptString('12345') = '32905634009932754887810741732407198258806730962982965574976507206624257560046060088651698145973700578074755623815890827210070269201614160932056340099327548878107417324071825880673062982965574976507206606008865169814573700551'

Make sense now? :-)

Cheers,

Raymond.
0
MadshiCommented:
Hmmm... Now it is beginning to become clear...   :-)
0
MAXcomAuthor Commented:
Would this be a good time to ask for code to decrypt it? :)
0
rwilson032697Commented:
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
MAXcomAuthor Commented:
works great for me, thanks a ton!
0
rwilson032697Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.