Click here to Skip to main content
14,872,784 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello there,

i'm C# developer,
i find lot of thing about encryption, but all of them choose what encryption they liked

the thing i'm looking for is 2 thing:
1. what's best encoding for encryption which support all encoding
2.do all encryption algorithm support all encoding? and is this good that i let user choose the encoding he require once he use algorithm? or i have to limit the choice?


also i want to see what's best choice encoding to encrypt a stored file..

because i use both string encryption and file encryption option.

Note: i used all symmetric and asymmetric encryption provided in VS library.
it's good to know about all of them one by one :| sry cause you trouble
Posted
Updated 30-Dec-11 5:09am
v2
Comments
Oshtri Deka 30-Dec-11 11:06am
   
I am a bit confused with first part of your question, can you please rephrase it?

Encryption does not look at encoding - it works on a stream of bytes, and does not assume any encoding at all. If you think about it, that is the only way it can work, unless the encryption algorithm is told "this is a binary executable file, treat it differently" which would be counter productive. Just hand the encrypt method a byte array and let it get on with it - that way you can do any un-encoding and re-encoding without encryption messing things up.
   
Hello Hassan,

Is this what you are looking for?

  • You may exchange alg and the associated aux functions to match your needs.
  • If you have a byte array instead of a C# string, you may leave away the Unicode conversions to byte array.
  • If you do not need a printable representation, leave the base64 parts away.


See also: Simple encrypting and decrypting data in C#.

Cheers

Andi

C#
/// <summary>
/// Encryption utilities for string encryption.
/// This is using know-how obtained from
/// http://www.codeproject.com/KB/security/DotNetCrypto.aspx
/// (no license restrictions apply).
/// </summary>
public class StringEncryption
{
    /// <summary>Salt: some static byte array, e.g. your name</summary>
    private static readonly byte[] salt = new byte[] { ... };

    /// <summary>Encrypt a string and return it as hex encocded.</summary>
    /// <param name="plainText">plain text to encrypt</param>
    /// <param name="password">password</param>
    /// <returns>encrypted text as base64 string (without new lines)</returns>
    public static string Encrypt(string plainText, params byte[] password)
    {
        using (var ms = new MemoryStream())
        {
            using (var alg = Rijndael.Create())
            using (var pwd = new Rfc2898DeriveBytes(password, salt, 1000))
            using (var cs = new CryptoStream(ms, GetEncryptor(alg, pwd),
                                                 CryptoStreamMode.Write))
            {
                byte[] plainBytes = Encoding.Unicode.GetBytes(plainText);
                cs.Write(plainBytes, 0, plainBytes.Length);
            }
            byte[] encodedBytes = ms.ToArray();
            string base64string = Convert.ToBase64String(encodedBytes,
                                            base64FormattingOptions.None);
            return base64string;
        }
    }
    /// <summary>
    /// Decrypt a base64 encocded and encrypted string and return it as plain text.
    /// </summary>
    /// <param name="encryptedText">base64 encoded and encrypted text</param>
    /// <param name="password">password</param>
    /// <returns>decrypted text</returns>
    public static string Decrypt(string encryptedText, params byte[] password)
    {
        using (var ms = new MemoryStream())
        {
            using (var alg = Rijndael.Create())
            using (var pwd = new Rfc2898DeriveBytes(password, salt, 1000))
            using (var cs = new CryptoStream(ms, GetDecryptor(alg, pwd),
                                                 CryptoStreamMode.Write))
            {
                byte[] decodedBytes = Convert.FromBase64String(encryptedText);
                cs.Write(decodedBytes, 0, decodedBytes.Length);
            }
            byte[] decryptedBytes = ms.ToArray();
            string plainText = Encoding.Unicode.GetString(decryptedBytes);
            return plainText;
        }
    }

    private static ICryptoTransform GetEncryptor(SymmetricAlgorithm algorithm,
                                                 DeriveBytes pwd)
    {
        return algorithm.CreateEncryptor(pwd.GetBytes(32), pwd.GetBytes(16));
    }
    private static ICryptoTransform GetDecryptor(SymmetricAlgorithm algorithm,
                                                 DeriveBytes pwd)
    {
        return algorithm.CreateDecryptor(pwd.GetBytes(32), pwd.GetBytes(16));
    }
}
   
Comments
RaviRanjanKr 30-Dec-11 16:48pm
   
5+
1. so if i convert string to utf, i just make bigger array of byte? and still it work same for method?

2. i didn't got you completely for my issue in your language, you say if i want to convert to byte executable file or dll, i have to do specific encoding?
   
Comments
RaviRanjanKr 30-Dec-11 16:48pm
   
A suggestion :- you can use Have a question or Comment button to drop your message and to get Immediate response instead of posting as answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900