By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,081 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

how to encrypt a string by coding??

P: 4
please somebody show me the way... ^^
Jan 11 '08 #1
Share this Question
Share on Google+
6 Replies


debasisdas
Expert 5K+
P: 8,127
You can try to replace some characters by others and reverse the process to get back the actual string.
Jan 11 '08 #2

Expert 5K+
P: 8,434
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.
Jan 11 '08 #3

jamesd0142
100+
P: 469
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()
Jan 11 '08 #4

P: 41
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
Jan 14 '08 #5

P: 41
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
Jan 14 '08 #6

lotus18
100+
P: 866
Hello All

Check out my code on encryption/decryption: click me

Rey Sean
Jan 15 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.