views:

57

answers:

2

Is there a way to encrypt any string or file of any length and return a key with a fixed length.

e.g:

$str = 'Hello World!'; 
encrypt($str);//returns: "abc123"//a fixed length of characters.
decrypt('abc123');//returns: "Hello World!"//the contents of the original string.

above is in php

Can be any computer language:

Can be any fixed number as long as it's fixed

by fixed i mean always the same: 32 character, 64 characters or X characters.

I have researched a little and it looks like it's hard or imposible. but you never know i thought it might be worthed asking

if you dont ask you don't get :) thnx

+3  A: 

No there isn't. Your asking to write a function which takes n bits of input and returns 32 characters of output which can be reversed. Never mind encryption, if I had an algorithm that could do that I'd be making a fortune selling compression technology. It simply isn't possible - 32 characters of output can only losslessly encode 32 characters of input

Stewart
Well i think i have got an idea of how to but i keep getting something wrong and revising it again and again. not goin to give much away but i think ur answer `if I had an algorithm that could do that I'd be making a fortune selling compression technology` is the reason i could not go on details and wanted to know if something like that existed :) thnx mate :)
Val
That would make YouTube so much faster...
Thilo
@Val - I very much look forward to seeing it. Will you be calling it Shergar?
Stewart
why shergar? i did not get that part lol
Val
by the way i was thinking for gamming industry as my PS3 internet is slowwwwww as hell Call of Duty im v good at it but sometimes my connection is slow so if i could figure this one out... which eventually i will. i wont have that problem no more :) but YouTube looks good aswell :)
Val
The broadband internet business would love it too - you'd be able to get much more data down an old copper phone line if you could infinitely compress it.
Stewart
@Val: Don't waste your time chasing Perpetuum Mobile.
caf
+1  A: 

Yes, this is possible if there is an upper bound X for the number of characters you want to encrypt. Just encrypt using any encryption algorithm, then pad the result to X characters.

If there is no upper bound, this is impossible for reasons of information theory (you would have to somehow store an unlimited number of characters as a limited number of [encrypted] characters, which is not possible in general). To understand why, look up the pigeonhole principle.

sleske
The upper bound is interesting - if you had a ciphertext length of 1e80 for example, you should be able to cover any sensible input. It would be quite slow though, and I don't know how you'd store the ciphertext.
Stewart