Click here to Skip to main content
15,900,110 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i have written below code for encryption where i am using string [,]key to call in the main program from encryption class . please suggest how can we convert that string to byte[]

What I have tried:

public int   EncryptFile(string inputFile, string  outputfile, byte[]  rijnKey, byte[] rijnIV)
		{
			try {
				
				FileStream fin = new FileStream(inputFile, FileMode.Open, FileAccess.Read);
				FileStream fout = new FileStream(outputfile, FileMode.OpenOrCreate, FileAccess.Write);
				fout.SetLength(0);
				
				long rdlen = 0;              
				long totlen = fin.Length;    
				int len;       			
				SymmetricAlgorithm rijn = SymmetricAlgorithm.Create(); 
				CryptoStream encStream = new CryptoStream(fout, rijn.CreateEncryptor(rijnKey,rijnIV), CryptoStreamMode.Write);
				int[] digits = terdigits(Convert.ToInt32(totlen));
				int length = 1;
				for (int i = 0; i < digits.Length; i++) {
					switch (i) {
						case 0:
							length = digits[0] * 1000;
							break;
						case 1:
							length = digits[1] * 100;
							break;
						case 2:
							blength = digits[2] * 10;
							break;
						
					}
					if (length != 0) {
						byte[] bin = new byte[length];
						len = fin.Read(bin, 0, length);
						encStream.Write(bin, 0, len);
						rdlen = rdlen + len;
						len = 0;
					}
				}
				encStream.Close();
				fout.Close();
				fin.Close();
				return 1;
			} catch (Exception e) {
				return 0;
			}
		}

result = a.EncryptFile(SelectedPath + @"\" + FileName, TargetFilePath, key[0, 1].ToString(),iv);
Posted
Updated 3-Jun-18 22:59pm

1 solution

C#
byte[] bytes = Encoding.ASCII.GetBytes(someString);

You will need to turn it back into a string like this:
C#
string someString = Encoding.ASCII.GetString(bytes);
 
Share this answer
 
Comments
Member 13846964 4-Jun-18 5:11am    
need to convert while calling the encrypt function or need to convert byte[]rijnkey in encryption function. i am getting belowerros..
Argument 3: cannot convert from 'string' to 'byte[]' (CS1503) -
The best overloaded method match for 'Encryption.AES.EncryptFile(string, string, byte[], byte[])' has some invalid arguments (CS1502) -

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900