Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
public string active(string text, string fileToWrite)
{
   //byte temp;
   byte[] key = GenerateKey();
   BigInteger keyToUser = new BigInteger(key);//saves the key at text file
   System.IO.StreamWriter objWriter;
   objWriter = new System.IO.StreamWriter(fileToWrite);
   objWriter.Write(keyToUser.ToString());
   objWriter.Close();
   byte[] data = ASCIIEncoding.ASCII.GetBytes(text);//puts the text into a byte array of ascii codes
   byte[] encrypteData = new byte[data.Length];
   int i = 0, j = 0;
   for (int l = 0; l < data.Length; l++)
   {
      i = (i + 1) % 256;
      j = (j + key[i]) % 256;
      key[i] ^= key[j];
      key[j] ^= key[i];
      key[i] ^= key[j];
      encrypteData[l] = (byte)(data[l] ^ key[(key[i] + key[j]) % 256]);
   }
   return ASCIIEncoding.ASCII.GetString(encrypteData);
}
 
public string UnActive(string text, string fileToRead)
{
   //byte temp;
   System.IO.StreamReader objReader;
   objReader = new System.IO.StreamReader(fileToRead);//reads the key from text file
   BigInteger numberThatDecrypt = BigInteger.Parse(objReader.ReadLine());
   byte[] key = numberThatDecrypt.ToByteArray();//puts the key into byte array
   byte[] data = ASCIIEncoding.ASCII.GetBytes(text);
   byte[] decrypteData = new byte[data.Length];
   int i = 0, j = 0;
   for (int l = 0; l < data.Length; l++)
   { 
      i = (i + 1) % 256;
      j = (j + key[i]) % 256;
      key[i] ^= key[j];
      key[j] ^= key[i];
      key[i] ^= key[j];
      decrypteData[l] = (byte)(data[l] ^ key[(key[i] + key[j]) % 256]);
   }
   return ASCIIEncoding.ASCII.GetString(decrypteData);
}
 
private byte[] GenerateKey()
{
   int i = 0, j = 0;
   //byte temp;
   Random rnd = new Random();
   int keyLength = rnd.Next(40, 257);
   BigInteger keyFirstStep = GenerateNumber(keyLength);// make a number in a wanted length
   byte[] keySecondStep = keyFirstStep.ToByteArray();
   byte[] s = new byte[256];
   for (i = 0; i < 256; i++)
      s[i] = (byte)i;
   for(i = 0; i < 256; i++)
   {
      j = (j + s[i] + keySecondStep[i % keySecondStep.Length]) % 256;
      s[i] ^= s[j];
      s[j] ^= s[i];
      s[i] ^= s[j];
   }
   return s;
}
when i do the encryption it encrypts the text, but when i try to decrypt it does not work, can u please help me? it very important!
thanks alot.
 
[Edit]Code block added[/Edit]
Posted 9-Nov-12 7:03am
Edited 9-Nov-12 7:24am
v3
Comments
Member 9552449 at 10-Nov-12 5:52am
   
i didnt understand what you edited in my code.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
There're a lot of articles about encryption/decryption.
Here're a few articles about encryption/decrypion in C# (not with RC4 encryption, but the .NET Framework contains classes and methods to encrypt/decrypt data with TripleDES and other algorithms):
Encrypt and Decrypt Data with C#[^]
Simple encrypting and decrypting data in C#[^]
Simple String Encryption and Decryption with Source Code[^]
Public Key RSA Encryption in C# .NET[^]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 277
1 BillWoodruff 205
2 Maciej Los 205
3 DamithSL 165
4 Sergey Alexandrovich Kryukov 145
0 OriginalGriff 5,107
1 DamithSL 4,157
2 Maciej Los 3,595
3 Kornfeld Eliyahu Peter 3,450
4 Sergey Alexandrovich Kryukov 2,741


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 9 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100