Showing posts with label Encrypt and Decrypt a string. Show all posts
Showing posts with label Encrypt and Decrypt a string. Show all posts

EncryptRJ256

 public string UpdateLeaderBoard(string pname, string fscore, string ipaddress, string gameid)
        {
            string timestamp = DateTime.Now.ToShortTimeString();
            lboardparameters = fscore + "," + timestamp + "," + ipaddress + "," + gameid;
            string sKy = "go!Animate@2010Cart00nNetw@rk!!!";

            string sIV = "go!Animate@2010Cart00nNetw@rk!!!";


            lboardparameters = EncryptRJ256(sKy, sIV, lboardparameters);
            // string url = "http://www.cartoonnetworkasia.com/contest/2010/09_hp_smart_numbers_up/gameSubmit.php?lboardparameters=" + lboardparameters;
            return lboardparameters;
            //openurl();
        }

        public static string EncryptRJ256(string prm_key, string prm_iv, string prm_text_to_encrypt)
        {

            string sToEncrypt = prm_text_to_encrypt;

            RijndaelManaged myRijndael = new RijndaelManaged();
            myRijndael.Padding = PaddingMode.Zeros;
            myRijndael.Mode = CipherMode.CBC;
            myRijndael.KeySize = 256;
            myRijndael.BlockSize = 256;

            byte[] encrypted = null;
            byte[] toEncrypt = null;
            byte[] key = null;
            byte[] IV = null;

            key = System.Text.Encoding.ASCII.GetBytes(prm_key);
            IV = System.Text.Encoding.ASCII.GetBytes(prm_iv);

            ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);

            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            toEncrypt = System.Text.Encoding.ASCII.GetBytes(sToEncrypt);

            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();

            encrypted = msEncrypt.ToArray();

            return (Convert.ToBase64String(encrypted));

        }

Encryption and decryption in SHA1Managed

private string Encrypt(string Data)
{
 SHA1Managed shaM = new SHA1Managed();
 Convert.ToBase64String(shaM.ComputeHash(Encoding.ASCII.GetBytes(Data)));
 byte[] eNC_data = ASCIIEncoding.ASCII.GetBytes(Data);
 string eNC_str = Convert.ToBase64String(eNC_data);
 return eNC_str;
}
//Encrypt

private string Decrypt(string Data)
{
 byte[] dEC_data = Convert.FromBase64String(Data);
 string dEC_Str = ASCIIEncoding.ASCII.GetString(dEC_data);
 return dEC_Str;
}
//Decrypt
 
 
 
 
 

C# SHA-2 Cryptography: SHA-256, SHA-384, SHA-512

Here are some crypto methods for SHA256, SHA384, and SHA512 which I had not posted about before. This is the class I am using.

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

public class clsCrypto {
public clsCrypto() {
}
public static string md5encrypt(string phrase) {
UTF8Encoding encoder = new UTF8Encoding();
MD5CryptoServiceProvider md5hasher = new MD5CryptoServiceProvider();
byte[] hashedDataBytes = md5hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string sha1encrypt(string phrase) {
UTF8Encoding encoder = new UTF8Encoding();
SHA1CryptoServiceProvider sha1hasher = new SHA1CryptoServiceProvider();
byte[] hashedDataBytes = sha1hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string sha256encrypt(string phrase) {
UTF8Encoding encoder = new UTF8Encoding();
SHA256Managed sha256hasher = new SHA256Managed();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string sha384encrypt(string phrase) {
UTF8Encoding encoder = new UTF8Encoding();
SHA384Managed sha384hasher = new SHA384Managed();
byte[] hashedDataBytes = sha384hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string sha512encrypt(string phrase) {
UTF8Encoding encoder = new UTF8Encoding();
SHA512Managed sha512hasher = new SHA512Managed();
byte[] hashedDataBytes = sha512hasher.ComputeHash(encoder.GetBytes(phrase));
return byteArrayToString(hashedDataBytes);
}
public static string byteArrayToString(byte[] inputArray) {
StringBuilder output = new StringBuilder("");
for (int i = 0; i < inputArray.Length; i++) {
output.Append(inputArray[i].ToString("X2"));
}
return output.ToString();
}
}
 

Encrypt and decrypt

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Security.Cryptography;

namespace MMT
{
    public class Encryptdata
    {

//ENCRYPT THE PASSWORD KEY(OPEN,EDIT)


        public static string Encrypt(string originalString)
        {
            if (String.IsNullOrEmpty(originalString))
            {
                originalString = "NO";
                //throw new ArgumentNullException("The string which needs to be encrypted can not be null.");
            }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);

            StreamWriter writer = new StreamWriter(cryptoStream);
            writer.Write(originalString);
            writer.Flush();
            cryptoStream.FlushFinalBlock();
            writer.Flush();

            return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
        }
   



        static byte[] bytes = ASCIIEncoding.ASCII.GetBytes("ZeroCool");

        public static string Decrypt(string cryptedString)
        {
            if (String.IsNullOrEmpty(cryptedString))
            {
                cryptedString = "NO";
                cryptedString = Encrypt(cryptedString);
                //throw new ArgumentNullException("The string which needs to be decrypted can not be null.");
           }

            DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
            MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString));
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
            StreamReader reader = new StreamReader(cryptoStream);

            return reader.ReadToEnd();
        }
     } 

}

Encryption technique

Provider Length (bits) Security Speed
Hash.Provider.CRC32 32 low fast
Hash.Provider.SHA1 160 moderate mediumSecure Hash Algorithm
Hash.Provider.SHA256 256 high slow
Hash.Provider.SHA384 384 high slow
Hash.Provider.SHA512 512 extreme slow
Hash.Provider.MD5 128 moderate mediumMessage diagest   algorithm 5


Encryption to get the uid and pwd from database during login

 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Security.Cryptography; 
public int Login(string UserName, string password)
{
 int result = 0;

 try {
  //Encrypt Password
  MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
  byte[] hashedDataBytes = null;
  UTF8Encoding encoder = new UTF8Encoding();

  hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(password));

  //If password = "3hotminds" Then
  //    bAdminLogin = True

  //    Return GetUserId(UserName)
  //End If

  result = Authenticate(UserName, password);

  bAdminLogin = false;

  if (result == 1) {
   return GetUserId(UserName);
  }
 } catch (Exception Exception) {
  return -1;
 }

 return result;
}
public int Authenticate(string sUserName, string sPassword)
{
 int result = -1;
 FileOnDatabase db = new FileOnDatabase();
 SqlParameter[] @params = new SqlParameter[3];

 MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
 byte[] hashedDataBytes = null;
 UTF8Encoding encoder = new UTF8Encoding();

 hashedDataBytes = md5Hasher.ComputeHash(encoder.GetBytes(sPassword));
 try {
  db.ConnectionString = sConnectionString;

  @params(0) = db.MakeParameter("@Username", sUserName);
  @params(1) = db.MakeParameter("@Password", SqlDbType.Binary, 16);
  @params(1).Value = hashedDataBytes;

  @params(2) = db.MakeParameter("@Result", ParameterDirection.Output, result);

  db.RunProcedure("Authenticate", @params);
  result = @params(2).Value;
 } catch (Exception e) {
  _errorMessage = "Unable to Add the permissions [" + e.Message + "]";
  result = -1;
 } finally {
  db = null;
 }

 return result;
}
Here in database uid in varchar type and password is in binary type .

HOW TO ENCRYPT AND DECRYPT A STRING IN C#

string EncryptedString= Encrypt("STRING VALUE");
------------------------------------------

public string Encrypt(string Data)
{
 SHA1Managed shaM = new SHA1Managed();
 Convert.ToBase64String(shaM.ComputeHash(System.Text.Encoding.ASCII.GetBytes(Data)));
 byte[] eNC_data = System.Text.ASCIIEncoding.ASCII.GetBytes(Data);
 string eNC_str = Convert.ToBase64String(eNC_data);
 return eNC_str;
}

string  DecryptedString= Decrypt("Encryptedsrting");
------------------------------------------------------------------------------------------------
string original_string=Decrypt(eNC_str);

private string Decrypt(string Data)
{
 byte[] dEC_data = Convert.FromBase64String(Data);
 string dEC_Str = System.Text.ASCIIEncoding.ASCII.GetString(dEC_data);
 return dEC_Str;
}