0

Possible Duplicate:
Convert a string to number and back to string?

I have a string that looks like:

AhgRtlK==

and I need to be able to encrypt and decrypt this string into numbers that might look like this:

1275653444

It's like phone charge credit (some thing like that)

EDIT : i want to create some thing like mobile charge credit that contains value of credit card but encrypted

Community
  • 1
  • 1
Samy Massoud
  • 4,295
  • 2
  • 35
  • 48
  • 1
    Do you mean, you have a string base64 encoded that you want to decode? – Peon Jul 25 '12 at 11:09
  • no i mean i have string and i want to encrypt it but in numbers – Samy Massoud Jul 25 '12 at 11:10
  • 1
    Are you basically looking for a base conversion algorithm from base 64 to base 10? Question is pretty unclear... – deceze Jul 25 '12 at 11:11
  • Why do you want to have it in numbers? And is it a problem if the number is really long and/or has a possibility of dublicates? If not, you can just use something like the first 64bits of md5(). – Sietse Jul 25 '12 at 11:14
  • to be very clear if you have word "HELLO" i need to encrypt it some how to be like "12534" – Samy Massoud Jul 25 '12 at 11:15
  • 2
    And do you really need to [**encrypt**](http://en.wikipedia.org/wiki/Encryption), or is it sufficient merely to [**encode**](http://en.wikipedia.org/wiki/Character_encoding)? Each of the characters in your input already has an ASCII value. Can you just use those? – ghoti Jul 25 '12 at 11:15
  • Also, what does this have to do with credit cards? – ghoti Jul 25 '12 at 11:16
  • i have Updated question again (i need some encryption like credit card numbers) – Samy Massoud Jul 25 '12 at 11:21
  • @SamyMassoud is it like you want to convert character to its equivalent number or the numbers are randomly selected?? – Sibu Jul 25 '12 at 11:22
  • Whatever this is, it sounds like a bad idea that already must have a proper, established solution. If you need to *encrypt* something, use established encryption algorithms (which won't result in an all-numeric string though). If you need to handle credit cards securely, don't transfer them over some untrusted medium that requires "encryption" to begin with. – deceze Jul 25 '12 at 11:23
  • i have created this string and i wan to convert it to numbers you may look at @Sietse answer it's close to what i need – Samy Massoud Jul 25 '12 at 11:31
  • @SamyMassoud "It's close to" Please say what you need then. Put a comment on my answer or something. It is obviously not clear what you want. – Sietse Jul 25 '12 at 11:39
  • check this out http://stackoverflow.com/questions/1391132/two-way-encryption-in-php – TURTLE Sep 11 '13 at 16:37

2 Answers2

3

You can just use the ascii value to convert a string into a number:

$integer = '';
foreach (str_split($string) as $char) {
    $integer .= sprintf("%03s", ord($char));
}
return $integer;

To convert it back you can use this:

$string = '';
foreach (str_split($integer, 3) as $number) {
    $string .= chr($number);
}
return $string;
Sietse
  • 707
  • 4
  • 10
3

I don't think you understand the problem well enough to ask the right question. To the extent I understand what you're saying, it's not well thought out. Suppose some code meant a credit of $500. Well, it would always mean that, today, tomorrow, and forever, even after I spent some of it.

So you don't want codes that decrypt to values. You want codes that identify unique accounts that have balances. (There are great algorithms to do that, and they're generally based on HMACs.)

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • Wow. I would never have been able to guess that the OP wanted a voucher system. If it's right, awesome catch. – J. Steen Jul 25 '12 at 11:46
  • so what is the direction please :D – Samy Massoud Jul 25 '12 at 11:47
  • @SamyMassoud: It depends on your actual requirements, which you never quite say. But the basic idea is this: You *must* have a database somewhere. Otherwise, there is no way to keep track of which codes have been used. So you might as well just use random numbers for the cards and enter their balances in the database. If you have requirements random numbers don't meet, then you might need some other solution. But we can't tell without knowing your actual requirements. (Three-quarters of the work may be figure out what your requirements actually are. Getting that right is important.) – David Schwartz Jul 25 '12 at 12:14
  • ok i need to generate very unique number and i will store it in database but also i want be sure that unique number is hard to be guessed – Samy Massoud Jul 25 '12 at 12:17
  • 1
    Then just make it random. Alternatively, you can make the first, say, 8 digits sequential (000000, 000001, 0000002) and the remaining digits random. (This may make lookup easier and it ensures no repeats.) – David Schwartz Jul 25 '12 at 12:19