Connecting Tech Pros Worldwide Forums | Help | Site Map

how to encrypt a string by coding??

Newbie
 
Join Date: Jan 2008
Posts: 4
#1: Jan 11 '08
please somebody show me the way... ^^

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#2: Jan 11 '08

re: how to encrypt a string by coding??


You can try to replace some characters by others and reverse the process to get back the actual string.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#3: Jan 11 '08

re: how to encrypt a string by coding??


Quote:

Originally Posted by idalreadyuse

please somebody show me the way... ^^

I don't think there is any one standard way of encrypting text. You should research the subject and determine what your requirements are. Here is a link to help get you started.
jamesd0142's Avatar
Needs Regular Fix
 
Join Date: Sep 2007
Location: Wales
Posts: 467
#4: Jan 11 '08

re: how to encrypt a string by coding??


One way i have used...

Pick out each character in a string and return the asci code of the character. have some mathematical fun and then turn it back into a character...

basically some asci swapping.

Works well for personal use.

functions used are

mid()
asc()
chr()
Member
 
Join Date: Dec 2007
Posts: 41
#5: Jan 14 '08

re: how to encrypt a string by coding??


Killer42 is correct when he says that there is no standard way to encrypt and that you should do some research to find out what would best fit your needs. However, I used to use the below functions (I don’t any more). They are only going to keep the honest people honest. With that said here you go…

Expand|Select|Wrap|Line Numbers
  1.  
  2.      Function Decrypt(ByVal TextVal As String) As String
  3.         Dim x As Integer
  4.         For x = 1 To Len(TextVal)
  5.             Mid$(TextVal, x, 1) = Chr(Asc(Mid(TextVal, x, 1)) - 100)
  6.         Next
  7.         Decrypt = TextVal
  8.     End Function
  9.  
  10.     Function Encrypt(ByVal TextVal As String) As String
  11.         Dim x As Integer
  12.         For x = 1 To Len(TextVal)
  13.             Mid$(TextVal, x, 1) = Chr(Asc(Mid(TextVal, x, 1)) + 100)
  14.         Next
  15.         Encrypt = TextVal
  16.     End Function
I hope this helps
Torgg
Member
 
Join Date: Dec 2007
Posts: 41
#6: Jan 14 '08

re: how to encrypt a string by coding??


Even better encryption/decryptions. I use this class in my projects and it works great. Symmetric Key Encryption using Rijndael and C#

Expand|Select|Wrap|Line Numbers
  1.     public class Cryptography
  2.     {
  3.         //**********************************************************************
  4.         //******  Key generated from https://www.grc.com/passwords.htm    ******
  5.         //**********************************************************************
  6.         private static string sKey = "D41B11724DDD5276F9793EBA4F2DE9F8"; //Can only be 32 characters long
  7.  
  8.         public static string EncryptString(string InputText)
  9.         //http://dotnet.org.za/deon/articles/2998.aspx
  10.         {
  11.             // We are now going to create an instance of the Rihndael class.
  12.             System.Security.Cryptography.RijndaelManaged RijndaelCipher = new System.Security.Cryptography.RijndaelManaged();
  13.  
  14.             // First we need to turn the input strings into a byte array.
  15.             byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(InputText);
  16.  
  17.             // We are using salt to make it harder to guess our key using a dictionary attack.
  18.             byte[] Salt = Encoding.ASCII.GetBytes(sKey.Length.ToString());
  19.  
  20.             // The (Secret Key) will be generated from the specified password and salt.
  21.             System.Security.Cryptography.PasswordDeriveBytes SecretKey = new System.Security.Cryptography.PasswordDeriveBytes(sKey, Salt);
  22.  
  23.             // Create a encryptor from the existing SecretKey bytes.
  24.             // We use 32 bytes for the secret key
  25.             // (the default Rijndael key length is 256 bit = 32 bytes) and
  26.             // then 16 bytes for the IV (initialization vector),
  27.             // (the default Rijndael IV length is 128 bit = 16 bytes)
  28.             System.Security.Cryptography.ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
  29.  
  30.             // Create a MemoryStream that is going to hold the encrypted bytes
  31.             System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
  32.  
  33.             // Create a CryptoStream through which we are going to be processing our data.
  34.             // CryptoStreamMode.Write means that we are going to be writing data
  35.             // to the stream and the output will be written in the MemoryStream
  36.             // we have provided. (always use write mode for encryption)
  37.             System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, Encryptor, System.Security.Cryptography.CryptoStreamMode.Write);
  38.  
  39.             // Start the encryption process.
  40.             cryptoStream.Write(PlainText, 0, PlainText.Length);
  41.  
  42.             // Finish encrypting.
  43.             cryptoStream.FlushFinalBlock();
  44.  
  45.             // Convert our encrypted data from a memoryStream into a byte array.
  46.             byte[] CipherBytes = memoryStream.ToArray();
  47.  
  48.             // Close both streams.
  49.             memoryStream.Close();
  50.             cryptoStream.Close();
  51.  
  52.             // Convert encrypted data into a base64-encoded string.
  53.             // A common mistake would be to use an Encoding class for that.
  54.             // It does not work, because not all byte values can be
  55.             // represented by characters. We are going to be using Base64 encoding
  56.             // That is designed exactly for what we are trying to do.
  57.             string EncryptedData = Convert.ToBase64String(CipherBytes);
  58.  
  59.             // Return encrypted string.
  60.             return EncryptedData;
  61.         }
  62.  
  63.         public static string DecryptString(string InputText)
  64.         {
  65.             System.Security.Cryptography.RijndaelManaged RijndaelCipher = new System.Security.Cryptography.RijndaelManaged();
  66.  
  67.             byte[] EncryptedData = Convert.FromBase64String(InputText);
  68.             byte[] Salt = Encoding.ASCII.GetBytes(sKey.Length.ToString());
  69.  
  70.             System.Security.Cryptography.PasswordDeriveBytes SecretKey = new System.Security.Cryptography.PasswordDeriveBytes(sKey, Salt);
  71.  
  72.             // Create a decryptor from the existing SecretKey bytes.
  73.             System.Security.Cryptography.ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));
  74.             System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(EncryptedData);
  75.  
  76.             // Create a CryptoStream. (always use Read mode for decryption).
  77.             System.Security.Cryptography.CryptoStream cryptoStream = new System.Security.Cryptography.CryptoStream(memoryStream, Decryptor, System.Security.Cryptography.CryptoStreamMode.Read);
  78.  
  79.             // Since at this point we don't know what the size of decrypted data
  80.             // will be, allocate the buffer long enough to hold EncryptedData;
  81.             // DecryptedData is never longer than EncryptedData.
  82.             byte[] PlainText = new byte[EncryptedData.Length];
  83.  
  84.             // Start decrypting.
  85.             int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
  86.  
  87.             memoryStream.Close();
  88.             cryptoStream.Close();
  89.  
  90.             // Convert decrypted data into a string.
  91.             string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
  92.  
  93.             // Return decrypted string.  
  94.             return DecryptedData;
  95.         }
  96.     }
I hope this is even more helpfull,
Torgg
lotus18's Avatar
Site Addict
 
Join Date: Nov 2007
Location: Zamboanga City, Philippines
Posts: 861
#7: Jan 15 '08

re: how to encrypt a string by coding??


Hello All

Check out my code on encryption/decryption: click me

Rey Sean
Reply