user3557236
user3557236

Reputation: 299

Simple short Encrypt string to fixed length n matter what the input string length is

I would like to encrypt a string and it has to be decrypted back. The input string could be of varying length but the encrypted string must be a max of 15 characters and alphanumeric. This is for an intranet application, so security is not of a big concern. I should be able to decrypt it back to match in another page. I am using vs2012, c#, asp.net. Please advice. I tried rijndael, but it gives a long string. The encrypted string must be user friendly since the user will need to remember and enter it.

Thanks, DotNet

Upvotes: 2

Views: 5078

Answers (5)

user3557236
user3557236

Reputation: 299

Thanks for your suggestions! I had to generate the password using the same technique that my peer had used which is SHA.

Convert.ToBase64String(new SHA1CryptoServiceProvider().ComputeHash(Encoding.UTF8.GetBytes(username)));

Upvotes: 0

deme72
deme72

Reputation: 1153

John skeet is almost right.

If you convert to an encryption of base 10 then convert back adding characters:

!@#$%^&*()_+-=;:'"<>,.?/\ to the 0-9, A-Z, a-z

then you will sometimes reduce the size and all the times it won't be recognizable.

basically convert to base 10 from a large base then convert that to a larger base

Upvotes: 0

David Crowell
David Crowell

Reputation: 3813

As Jon Skeet brings up, what you probably need is a token. You'll need to store you data on the server (database?). Use a token value as a key to retrieve the data.

If you don't need any security at all, you can use an Identity field (autoincrementing). If you want to make it harder to guess, you'll need something like this:

    public static string GetRandomToken()
    {
        // create a guid and convert to a byte array
        var guid = Guid.NewGuid();
        var bytes = guid.ToByteArray();

        // xor the first 8 bytes with the last
        for (int i = 0; i < 8; i++)
        {
            bytes[i] = (byte)(bytes[i] ^ bytes[i + 8]);
        }

        // resize the array down to eight bytes
        Array.Resize<byte>(ref bytes, 8);

        // return the hexidecimal representation, with the last character lopped off
        return BitConverter.ToString(bytes).Replace("-", string.Empty).Substring(0, 15);

    }

This only handles generating a token with length of 15. You still have to handle the database access yourself.

Upvotes: -1

Guanxi
Guanxi

Reputation: 3131

I think you are not looking for encryption but Hash algorithm. Hash algorithm generates Hash of specific length (not encrypted text).

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

The input string could be of varying length but the encrypted string must be a max of 15 characters and alphanumeric.

This is clearly impossible. If you solved this, you would solve all possible storage concerns - after all, you'd be able to store the whole internet (viewed as one long string) in 15 alphanumeric characters (after decryption).

You haven't told us what the input string might consist of, either - assuming that by "alphanumeric" you mean A-Z, a-z, 0-9 that's only 62 possible characters. So there are 6215 possible encrypted strings. If your input is in UTF-16 code units, then just 6 code units has more possibilities (655366 is greater than 6215).

Basically, you're onto a losing proposition here - you should rethink your design.

Perhaps you should store the original value in a server, return some token, and then be able to fetch the value when you want? That isn't encryption, but it may satisfy your real requirements.

Further reading: the pigeonhole principle

Upvotes: 12

Related Questions