using System;
using System.Security.Cryptography;
using System.Text;

public static class Encryption
{
    public static string Encrypt(string input, string key)
    {
        byte[] inputArray = UTF8Encoding.UTF8.GetBytes(input);
        TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
        tripleDES.Mode = CipherMode.ECB;
        tripleDES.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = tripleDES.CreateEncryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
        tripleDES.Clear();
        return Convert.ToBase64String(resultArray, 0, resultArray.Length);
    }
    public static string Decrypt(string input, string key)
    {
        byte[] inputArray = Convert.FromBase64String(input);
        TripleDESCryptoServiceProvider tripleDES = new TripleDESCryptoServiceProvider();
        tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key);
        tripleDES.Mode = CipherMode.ECB;
        tripleDES.Padding = PaddingMode.PKCS7;
        ICryptoTransform cTransform = tripleDES.CreateDecryptor();
        byte[] resultArray = cTransform.TransformFinalBlock(inputArray, 0, inputArray.Length);
        tripleDES.Clear();
        return UTF8Encoding.UTF8.GetString(resultArray);
    }

I was lucky enough to find this code on the internet, but for some reason im getting this error

Specified key is not a valid size for this algorithm.
tripleDES.Key = UTF8Encoding.UTF8.GetBytes(key); <-here
My key would be "jazerix", i'm not sure why it wont work, any ideas?

This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits. Your key 'jazerix' only has 56 bits.

oh, can u give me an example of a 128 bit key :)?

jazerix is 56 bits, because each character in your string is 1 byte, or 8 bits. 7 * 8 = 56 :)

Just make your key longer or if the user can enter the key themselves, create a function to either pad the key with a value, or truncate the key to meet the length requirements.

Keep in mind though, not ALL characters are equal, UTF-8 encoding can take up to 4 bytes/character depending on the character.

I attached a little project I made that you can run that will calculate the size of your string as you type it. Dunno if it will be useful, but meh.

commented: Awesome Help :) +2

Hi Jazerix
Have you got it?

I hope so, just woke up ;)

First of all, zachattack05 that program you made sure is neat, actually it seems to work now, i tried with the key "asdfewrewqrss323" (totally random) and it worked, however when i tried with 3956439587341234566 as the key, i got the same error, which i didnt really get since it was between the 128-192 bits :P

Actually it only seems to work with a 128 bit key or a 192 bit key, nothing in between :P

Correct.

It's one or the other. The MSDN says:

This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits.

Since it's in 64 bit increments you have 128, or 128 + 64 = 192.

well, that kinda sucks :P

is there any way i could make it in between?

Also, something else...

I'm not really sure what you plan on using your encryption for, but if it's something very important to your user or to you, you really shouldn't hard code the key into the program.

The CSP also has a GenerateKey() method that I would HIGHLY recommend you read up on. :)

I've used it before and it works nicely, that way you get a random key each time the data/object is encrypted.

I can post some example code of one I've used before if you like.

Sure thanks, that would be wonderful :)

the program is private :)

No, you can't make it inbetween, but what you can do is pad the key or truncate the key with data to make it fall into the required length.

That way if a user enters the key: "HelloWorld!" which is only 88 bits, you can have your application pad the data with say "0" until the length is 128 or 192. The user would still type "HelloWorld!" but the key is changed to: "HelloWorld!00000".

Same with a key that is too long, just drop characters from either the front or back of the string until you reach the right length.

Something else you might consider, taking the user's input, and hashing it with a provider that returns 128 bits. MD5 is one. I attached a table from Wiki that shows hash functions and their output size in bits.

Alright, ill look into it, thank you so much for your help :)!

No problem :)

Hi, can you give ex of 64 bit key?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.