1
public string EncryptPwd(String strString)
        {
            int intAscii;
            string strEncryPwd = "";
            for (int intIndex = 0; intIndex < strString.ToString().Length; intIndex++)
            {
                intAscii = (int)char.Parse(strString.ToString().Substring(intIndex, 1));
                intAscii = intAscii + 5;
                strEncryPwd += (char)intAscii;
            }
            return strEncryPwd;
        }

This is my code.Please suggest me it is right way or not.

Manish Singh
  • 934
  • 1
  • 12
  • 27
  • Suggest you that use `MD5`,`AES`or other standard encryption instead of custom encryption. – Divyang Desai Aug 30 '16 at 11:58
  • Use http://securitydriven.net/inferno/#Encryption it has a nuget package. Inferno does all the proper things for securing data properly, most examples show weak and flawed protection configurations. – Chris Marisic Aug 31 '16 at 16:57

3 Answers3

3

you can use this code:

Encrypt string with password:

public static string EncryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

             MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));         
            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();           
            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;        
            byte[] DataToEncrypt = UTF8.GetBytes(Message);     
            try
            {
                ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
                Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
            }
            finally
            {
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }          
            return Convert.ToBase64String(Results);
        }

and Decrypt String with password:

public static string DecryptString(string Message, string Passphrase)
        {
            byte[] Results;
            System.Text.UTF8Encoding UTF8 = new System.Text.UTF8Encoding();

             MD5CryptoServiceProvider HashProvider = new MD5CryptoServiceProvider();
            byte[] TDESKey = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase));

            TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();

            TDESAlgorithm.Key = TDESKey;
            TDESAlgorithm.Mode = CipherMode.ECB;
            TDESAlgorithm.Padding = PaddingMode.PKCS7;

            byte[] DataToDecrypt = Convert.FromBase64String(Message);
            // Step 5. Bat dau giai ma chuoi
            try
            {
                ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
                Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
            }
            catch (Exception) { Results = DataToDecrypt; }
            finally
            {                   
                TDESAlgorithm.Clear();
                HashProvider.Clear();
            }   

            return UTF8.GetString(Results);
        }
share pvv
  • 81
  • 4
2

Do you have a way to decrypt that? Here is another answer on SO

private static readonly UTF8Encoding Encoder = new UTF8Encoding();

public static string Encrypt(string unencrypted)
{
    if (string.IsNullOrEmpty(unencrypted)) 
        return string.Empty;

    try
    {
        var encryptedBytes = MachineKey.Protect(Encoder.GetBytes(unencrypted));

        if (encryptedBytes != null && encryptedBytes.Length > 0)
            return HttpServerUtility.UrlTokenEncode(encryptedBytes);    
    }
    catch (Exception)
    {
        return string.Empty;
    }

    return string.Empty;
}

public static string Decrypt(string encrypted)
{
    if (string.IsNullOrEmpty(encrypted)) 
        return string.Empty;

    try
    {
        var bytes = HttpServerUtility.UrlTokenDecode(encrypted);
        if (bytes != null && bytes.Length > 0)
        {
            var decryptedBytes = MachineKey.Unprotect(bytes);
            if(decryptedBytes != null && decryptedBytes.Length > 0)
                return Encoder.GetString(decryptedBytes);
        }

    }
    catch (Exception)
    {
        return string.Empty;
    }

    return string.Empty;
}
Community
  • 1
  • 1
James Ralston
  • 1,170
  • 8
  • 11
2

I agree with all the previous answers (especially share pvv) but in case you really did want a Caesar Cipher (Caesar Cipher is a very weak encryption and should not be used in production), and if this is a learning exercise, then here is some code

string strString = "abcdefghijklmnopqrstuvwxyz";
string enc = String.Join("", strString.ToArray().Select(x =>(char)( 'a' + (x-'a'+5)%26)));
Console.WriteLine(enc);

Or using a for loop like you did

 string strEncryPwd = "";
 for (int index = 0; index < strString.ToString().Length; index++)
 {
        int plainChar = strString[index];
        int encrypted = 'a' + (plainChar - 'a' + 5) % 26;
        strEncryPwd += (char)encrypted;
  }
        Console.WriteLine(strEncryPwd);
Community
  • 1
  • 1
robor
  • 2,969
  • 2
  • 31
  • 48