 |
|
 |
I need the c# code for making key generation , encryption and decryption in RSA algorithm
|
| Sign In·View Thread·PermaLink | 1.00/5 (2 votes) |
|
|
|
 |
|
 |
// original from sf under GNU LESSER GENERAL PUBLIC LICENSE // http://sourceforge.net/projects/rc4dotnet // simplified by me and fixed for files using System; using System.IO; using System.Text;
namespace Encryption.LESR_RC4_Geffe { public interface ICoder { void Encrypt(string password, string file, string targetFile); void Decrypt(string password, string file, string targetFile); } public class RC4: ICoder { private const int EncodingCode = 1251; public void Encrypt(string password, string file, string targetFile) { byte[] content = encrypt(password, File.ReadAllBytes(file)); File.WriteAllBytes(targetFile, content); }
public void Decrypt(string password, string file, string targetFile) { byte[] content = encrypt(password, File.ReadAllBytes(file)); File.WriteAllBytes(targetFile, content); }
private int getAsciiCode(char character) { return (int)(Encoding.GetEncoding(EncodingCode).GetBytes(character + "")[0]); }
private char fromAsciiCode(int asciiCode) { byte[] bytes = new byte[1]; bytes[0] = (byte)asciiCode; return Encoding.GetEncoding(EncodingCode).GetString(bytes)[0]; }
private string hexToBinary(string packtype, string datastring) { int i, j, datalength, packsize; byte[] bytes; char[] hex; string tmp;
datalength = datastring.Length; packsize = (datalength/2) + (datalength % 2); bytes = new byte[packsize]; hex = new char[2];
for (i = j = 0; i < datalength; i+=2) { hex[0] = datastring[i]; if (datalength - i == 1) hex[1] = '0'; else hex[1] = datastring[i + 1]; tmp = new string(hex, 0, 2); try { bytes[j++] = byte.Parse(tmp, System.Globalization.NumberStyles.HexNumber); } catch {} /* grin */ } return Encoding.GetEncoding(EncodingCode).GetString(bytes); }
public string binaryToHex(string bindata) { int i; byte[] bytes = Encoding.GetEncoding(EncodingCode).GetBytes(bindata); string hexString = ""; for (i = 0; i < bytes.Length; i++) { hexString += bytes[i].ToString("x2"); } return hexString; }
private byte[] encrypt(string password, byte[] data) { int a, i, j, k, tmp, pwd_length, data_length; int[] key, box; byte[] cipheredText;
password = hexToBinary("H*", password); // valid input, please! pwd_length = password.Length; data_length = data.Length; key = new int[256]; box = new int[256]; cipheredText = new byte[data.Length];
for (i = 0; i < 256; i++) { key[i] = getAsciiCode(password[i % pwd_length]); box[i] = i; } for (j = i = 0; i < 256; i++) { j = (j + box[i] + key[i]) % 256; tmp = box[i]; box[i] = box[j]; box[j] = tmp; } for (a = j = i = 0; i < data_length; i++) { a = (a + 1) % 256; j = (j + box[a]) % 256; tmp = box[a]; box[a] = box[j]; box[j] = tmp; k = box[((box[a] + box[j]) % 256)]; cipheredText[i] = (byte)(data[i] ^ k); } return cipheredText; } } }
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
And even simpler - with all language symbols
namespace Encryption.LESR_RC4_Geffe { public class RC4: ICoder { private const int EncodingCode = 1251; public void Encrypt(string password, string file, string targetFile) { byte[] passwordToPass = System.Text.Encoding.Default.GetBytes(password); byte[] content = encrypt(passwordToPass, File.ReadAllBytes(file)); File.WriteAllBytes(targetFile, content); }
public void Decrypt(string password, string file, string targetFile) { Encrypt(password, file, targetFile); }
private byte[] encrypt(byte[] password, byte[] data) { int a, i, j, k, tmp, pwd_length, data_length; int[] key, sbox; byte[] cipheredData;
pwd_length = password.Length; data_length = data.Length; key = new int[256]; sbox = new int[256]; cipheredData = new byte[data.Length];
for (i = 0; i < 256; i++) { key[i] = password[i % pwd_length]; sbox[i] = i; } for (j = i = 0; i < 256; i++) { j = (j + sbox[i] + key[i]) % 256; tmp = sbox[i]; sbox[i] = sbox[j]; sbox[j] = tmp; } for (a = j = i = 0; i < data_length; i++) { a = (a + 1) % 256; j = (j + sbox[a]) % 256; //swap tmp = sbox[a]; sbox[a] = sbox[j]; sbox[j] = tmp; k = sbox[((sbox[a] + sbox[j]) % 256)]; cipheredData[i] = (byte)(data[i] ^ k); } return cipheredData; } } }
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
 |
Thanks Simone for this article, but actually I have a comment about the algorithm implementation, you are using 'mod 255', while I've read the algorithm before & it says to use 'mod 256' that differs in the output completely, so do you have any explanation for wt you've done or that was a mistake? Thanks,
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
hello.....i`m now doing my final year project in my university.....and i have to do the encryption and decryption via the PIC microcontroller....could anyone help me how could i do my project or any references or tutorial.....I`m gonna do it in C language programming.......
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Hello,
This code doesn't seem to encrypt/decrypt the unicode (chinese/japanese) characters correctly. Is there anything that needs to be modified in order to make that work ?
Thanks in advance.
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
This algorithm does not produce correct outputs. Should not be used. Instead another .NET library http://rc4dotnet.devhome.org
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Thanks man!
Your example was exactly what I was looking for. A small and simple (and working) encryption class for a small and simple tool that needs to hide a password in its settings file. All the other examples I've found using Microsoft's encryption API were just too complicated for a beginner. You've really made my day! 
Cheers Christian
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I can't encript the following frase
Estos fabio ay dolor que ves ahora campos de soledad mustio collado fueron otrora
When you encrypt it it loses part of the message and when you decipher you got only
Estos fabio ay
By
Orestes
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
AntoninoLuis wrote: When you encrypt it it loses part of the message and when you decipher you got only
It's a bug of the interface: it doesn't display all the crypted phrase. The encryption class works fine : if you want to test it you have to debug the project and set breakpoint to line 314/315 of frmFrontend.cs. Hope it heps.
Simone
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I don't know much about encryption, but I found it really easy to add your class to my Delphi .NET project and encrypt some data. Good work. Thank you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Which is faster RSA, ECC, DES, or RC4? I know symmetric enc. is faster so I think it's RC4, but wanted to double check.
Can RC4 be used in either CBC or ECB mode?
Can RC4 be us as a public key algorithm?
thanks!
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
That's a pretty dumb question. Google the internet for answers before asking something like this.
ROFLOLMFAO
|
| Sign In·View Thread·PermaLink | 1.00/5 (3 votes) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
Can u please tell me how it can be done using this algorithm Please pass me refrence if u have anything.
shekhar
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
The RC4 algorithm works very well, but I need to hold the text in a file and then load it and descrypt it. What kind of file I need for that ?
I tried using a simple StreamFile in C#, but when I load the text again, and try to descrypt it, the result isn't the first text typed by myselft(I think the troubles are due to the encryption characters).
What I can do ?
Thanks. Ruben
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I am having the same problem. I tried using all kinds of methods to do the trick but the ciphertext is not converted back to the original plaintext. I guess the problem lies in picking up the special characters from the text file and then trying to decrypt them using this algo. There are so many packages that do encryption and store it to a file so I guess there must surely be a way via which this could be achieved. I really need an answer pronto!
Seemant  
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi
I've just completed writing a little encryption program that allows you to load text into a text control, encrypt it and save it again. I wrote the original in WINAPI C and found the problem you mentioned when I translated it into CSharp. I found that the 'Line ' methods of the StreamReader, StreamWriter and TextBox classes added carriage return characters ('\r\n') to terminate the lines. The way round this is to read the whole stream from a text file into a single string using the ReadToEnd method. Store the string as a buffer and load it into the the textbox with Text and not Lines even though it is multiline. You can change the text and refresh the buffer with the modified text. Process the buffer with RC4 and store the result again as a string in the buffer. Display and save from the buffer - don't display and then take the text from the display for storing as Windows alters it in mysterious ways. Save the string using the Write method and it should work. Hope that helps.
Ti
ps I used a modified version of a C function for the RC4 engine. You can find it on www.cr0.net:8040/code/crypto/rc4/
Ti
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hey, I was just reading over your post, and I seen that you have found a solution to the saving and loading files problems. I was wondering if you had your source code posted anywhere online. Your help would be greatly appreciated.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
In RC4Engine.cs Line 266 is defined: static public long m_nBoxLen = 255; its not correct! correct is: static public long m_nBoxLen = 256; // DesignFehler! 0x0 ... 0xff entspricht 256 // joerg.drobick@web.de 22.01.2005
|
| Sign In·View Thread·PermaLink | 5.00/5 (1 vote) |
|
|
|
 |
|
 |
We saw your coding(c# version).We are doing our final project(B.E-CSE) in WEP.It will be more helpful to us if u send us the original code(C version) of RC4 algorithm and its explanation for our reference.We assure that we will not misuse the code in anyway. Thank you
|
| Sign In·View Thread·PermaLink | 1.29/5 (6 votes) |
|
|
|
 |