-3

I am dealing with very large numbers. One of my algorithms requires a number 'x' to be smaller than another number 'n', in order to perform that algorithm. I was wondering is it possible to encrypt the very large number to produce a smaller number and store the key for when I need to decrpyt the number back to a larger number? I have looked at mcrypt but that doesnt output all numbers, from what I have seen it outputs a string.

emartel
  • 7,500
  • 1
  • 28
  • 56
Matt9Atkins
  • 2,758
  • 13
  • 43
  • 73
  • string is not really much different from a very large number (written in base 256) – Vlad Nov 16 '12 at 19:03
  • Read up on the [pigeonhole principle](http://en.wikipedia.org/wiki/Pigeonhole_principle) if you don't know it already. Does it make this impossible, or are there restrictions on those "very large numbers" (e.g. `max(large) - min(large_numbers) < n`) which make this possible? –  Nov 16 '12 at 19:04
  • @Vlad For simply ASCII chars that would be base 128 – TheZ Nov 16 '12 at 19:05
  • Also, what do you mean by smaller than? Do you mean smaller than in the numeric comparison sense, or smaller than in terms of the storage space used? For instance, you could achieve this if your source is an integer and float is an acceptable destination; then you can just divide the number. For instance, 100 can be compressed by dividing by 10 or 100 or 1000 and so on... – RonaldBarzell Nov 16 '12 at 19:06
  • @Vlad - But it contains letters and due to that I wont be able to perform arithmetic functions involving that number – Matt9Atkins Nov 16 '12 at 19:06
  • @user1161318 - 'x' has to be smaller than 'n' in numerical comparison. The value of 'n' is determined by another function so it is never the same value each time. – Matt9Atkins Nov 16 '12 at 19:07
  • @TheZ: `char` is usually 1 byte (8 bits) long, that makes 2^8 possible values. From the other POV, mcrypt's output is perhaps 6 bit/char. – Vlad Nov 16 '12 at 19:08
  • @Matt9Atkins: well, hexadecimal representation of numbers contains letters in their representation as well, nevertheless we can add the numbers expressed in hexadecimal. – Vlad Nov 16 '12 at 19:09
  • Would another possibility be to convert 'x' to base('n')? Would I be able to use the output number in arithmetic functions? – Matt9Atkins Nov 16 '12 at 19:16
  • encrypted number + decryption key is going to be larger than the original unencrypted number. you're chasing a false savings. – Marc B Nov 16 '12 at 19:24

2 Answers2

1

What you're talking about is called "compression", not "encryption".

If anything, encryption is likely to yield a larger string than its input. While you may be able to compress the number, it will be very slight savings.

Depending on the range of possible values, you may be able to simply build an array of every possible large number and work with array indexes instead of the numbers themselves.

meager
  • 209,754
  • 38
  • 307
  • 315
0

You can convert the number to a higher base to represent it with less characters. For example, if your number is in base 10, convert it to base 16. You can use the GMP library to do math with great precision on these numbers not in base 10.

You can read about the GMP (Gnu Multiple Precision) library here: http://www.php.net/manual/en/book.gmp.php

None
  • 4,945
  • 1
  • 36
  • 49