byte[] to string
-
sir, Just to encrypt a string, i used this code. code: public string encryptText(string plaintext,string passpharse,string saltValue,string hashAlgorithm,int passwordIterations,string initvector,int keysize) { byte[] InitVectorBytes = Encoding.ASCII.GetBytes(initvector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plaintextbytes = Encoding.ASCII.GetBytes(plaintext); PasswordDeriveBytes password = new PasswordDeriveBytes(passpharse, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes); MemoryStream memorystream = new MemoryStream(); CryptoStream cryptostream = new CryptoStream(memorystream, encryptor, CryptoStreamMode.Write); cryptostream.Write(plaintextbytes, 0, plaintextbytes.Length); cryptostream.FlushFinalBlock(); byte[] ciphertextbytes = memorystream.ToArray(); memorystream.Close(); cryptostream.Close(); string ciphertext = Convert.FromBase64String(ciphertextbytes); return ciphertext; } I've got the following errors: Error 1 The best overloaded method match for 'System.Convert.FromBase64String(string)' has some invalid arguments.54,29 Error 2 Argument '1': cannot convert from 'byte[]' to 'string'.54,54 Please help me to resolve it.
-
sir, Just to encrypt a string, i used this code. code: public string encryptText(string plaintext,string passpharse,string saltValue,string hashAlgorithm,int passwordIterations,string initvector,int keysize) { byte[] InitVectorBytes = Encoding.ASCII.GetBytes(initvector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plaintextbytes = Encoding.ASCII.GetBytes(plaintext); PasswordDeriveBytes password = new PasswordDeriveBytes(passpharse, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes); MemoryStream memorystream = new MemoryStream(); CryptoStream cryptostream = new CryptoStream(memorystream, encryptor, CryptoStreamMode.Write); cryptostream.Write(plaintextbytes, 0, plaintextbytes.Length); cryptostream.FlushFinalBlock(); byte[] ciphertextbytes = memorystream.ToArray(); memorystream.Close(); cryptostream.Close(); string ciphertext = Convert.FromBase64String(ciphertextbytes); return ciphertext; } I've got the following errors: Error 1 The best overloaded method match for 'System.Convert.FromBase64String(string)' has some invalid arguments.54,29 Error 2 Argument '1': cannot convert from 'byte[]' to 'string'.54,54 Please help me to resolve it.
You've declared ciperTextBytes as a byte array, when it needs to be a string. ConvertFromBase64String(string) is telling you that you need to pass the parameter to it as a string data type. I'm not 100% sure with out testing it, but string ciphertext = Convert.FromBase64String(ciphertextbytes.ToString()); !!MIGHT!! work. Cheers Shawty
-
You've declared ciperTextBytes as a byte array, when it needs to be a string. ConvertFromBase64String(string) is telling you that you need to pass the parameter to it as a string data type. I'm not 100% sure with out testing it, but string ciphertext = Convert.FromBase64String(ciphertextbytes.ToString()); !!MIGHT!! work. Cheers Shawty
-
You need to describe it better than that i'm afraid. the .ToString()??? the function??? the routine??? try to convert your byte array to a string before hand. It's fairly easy. All you need to do is iterate through the array collection and convert each byte to a char before adding it to a string. Cheers Shawty
-
sir, Just to encrypt a string, i used this code. code: public string encryptText(string plaintext,string passpharse,string saltValue,string hashAlgorithm,int passwordIterations,string initvector,int keysize) { byte[] InitVectorBytes = Encoding.ASCII.GetBytes(initvector); byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue); byte[] plaintextbytes = Encoding.ASCII.GetBytes(plaintext); PasswordDeriveBytes password = new PasswordDeriveBytes(passpharse, saltValueBytes, hashAlgorithm, passwordIterations); byte[] keyBytes = password.GetBytes(keysize / 8); RijndaelManaged symmetricKey = new RijndaelManaged(); symmetricKey.Mode = CipherMode.CBC; ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, InitVectorBytes); MemoryStream memorystream = new MemoryStream(); CryptoStream cryptostream = new CryptoStream(memorystream, encryptor, CryptoStreamMode.Write); cryptostream.Write(plaintextbytes, 0, plaintextbytes.Length); cryptostream.FlushFinalBlock(); byte[] ciphertextbytes = memorystream.ToArray(); memorystream.Close(); cryptostream.Close(); string ciphertext = Convert.FromBase64String(ciphertextbytes); return ciphertext; } I've got the following errors: Error 1 The best overloaded method match for 'System.Convert.FromBase64String(string)' has some invalid arguments.54,29 Error 2 Argument '1': cannot convert from 'byte[]' to 'string'.54,54 Please help me to resolve it.
If you can write encryption code, I fail to understand why you have a problem with such trivial error. At least you tell me what your code is trying to do, and why you are impementing it this way. :suss:
Yusuf Oh didn't you notice, analogous to square roots, they recently introduced rectangular, circular, and diamond roots to determine the size of the corresponding shapes when given the area. Luc Pattyn[^]
-
You need to describe it better than that i'm afraid. the .ToString()??? the function??? the routine??? try to convert your byte array to a string before hand. It's fairly easy. All you need to do is iterate through the array collection and convert each byte to a char before adding it to a string. Cheers Shawty
-
There's not much to it. You just need to declare a string, make it empty : String [myvariablename] = "" then you just need to loop over your byte array : foreach(byte [mybytevariablename] in [mybytearrayname]) then in the loop just concatenate each element. [myvariablename] = [myvariablename] + [mybytevariablename] When your finished, then the string you defind should be a string of all the chars in your byte array which you can then pass to the function. replace the [..] above as appropriate for your variable names where you've defined them.