-1

I am having an issue where a method call requires a string (plainText) or byte[] (plainTextAsBytes) and is generating a compile error of Cannot implicitly convert type string to byte[] I have tried to implicitly convert the string to a byte array with no success either.

I have read and tried the examples located at the following SO posts:

Converting string to byte array in C#

Cannot implicitly convert type string to byte[]

The code that I am calling is located at (in the C# section):

How to encrypt and decrypt data with salt

So, what am I doing wrong?

Edit 1

Problem is that when using this:

byte[] array = Encoding.UTF8.GetBytes(SMTPModel.SMTPPassword);
EncryptedPassword = RijndaelEnhanced.Encrypt(plainTextBytes: array);

or:

EncryptedPassword = RijndaelEnhanced.Encrypt(plainText: decryptedPassword);

or even:

EncryptedPassword = RijndaelEnhanced.Encrypt(plainText: "Test");

I get the above stated error.

Edit 2

Forgot to supply the following information:

decryptedPassword is a string defined as:

string decryptedPassword = SMTPModel.SMTPPassword;
Community
  • 1
  • 1
John Schultz
  • 672
  • 1
  • 10
  • 29
  • `Encoding.UTF8.GetBytes("some string");` – Aleksey L. Aug 20 '16 at 06:57
  • Yeah, I tried that too with no luck. – John Schultz Aug 20 '16 at 06:58
  • If you're using `Encoding.WhateverEncoding.GetBytes()/GetString() this exception won't be trhown it just can't. Check: https://msdn.microsoft.com/en-us/library/744y86tc(v=vs.110).aspx , https://msdn.microsoft.com/en-us/library/ds4kkd55(v=vs.110).aspx – Pau C Aug 20 '16 at 06:58
  • 1
    @JohnSchultz this converts the string to byte array for sure. Show your code – Aleksey L. Aug 20 '16 at 07:00
  • @AlekseyL. I am not disputing that, I am just saying it does not work, unfortunately. – John Schultz Aug 20 '16 at 07:01
  • @null, Tried that as well with no luck :( – John Schultz Aug 20 '16 at 07:04
  • @John Schultz but what problem did you have? The output was not expected? What exception was thrown? That code should awlays work. – Pau C Aug 20 '16 at 07:05
  • 1
    Your edit doesn't make any sense. The compiler would never give the error you state if you're passing an actual `byte[]` value, as in your first code snippet. Your second example is useless, because we have no idea what `decryptedPassword` is. And the third example would call the `Encrypt(string)` overload, obviating any need to convert from `string` to `byte[]`. Please provide a good [mcve] that reliably reproduces the problem. – Peter Duniho Aug 20 '16 at 07:12
  • Your second edit doesn't help either. Knowing that `decryptedPassword` is a `string` value just means that your second and third code examples are equivalent to each other; just as in the third example, the `Encrypt(string)` overload would be called, so too would it in the second. (Frankly, how your question got two up-votes, when no one can tell _what_ you're asking, is beyond me.) – Peter Duniho Aug 20 '16 at 07:17
  • All I am saying in **Edit 2** is that I have tried the three different snippets separately with no success. – John Schultz Aug 20 '16 at 07:20
  • What is the declaration of `EncryptedPassword`? Are you trying to assign the `string` _return value_ to a variable that has the type `byte[]`? Again, without a good [mcve] it is impossible to understand your question. Please stop making people guess. Show the code. – Peter Duniho Aug 20 '16 at 07:30
  • Is your implementation of RijndaelEnhanced _exactly_ the same as in the link you provided? The reason I ask is that you are calling it in your sample like a static method, but in the implementation in that link, it is an instance method. If you have not copied it exactly from the implementation in that link, then the issue is more likely to be your particular implementation of RijndaelEnhanced. – Chris Simon Aug 20 '16 at 07:45
  • It is actually. Updated my copy yesterday morning. – John Schultz Aug 20 '16 at 07:47

2 Answers2

0

OK, thank you for your insight and assistance, however the problem is not with my code, but the code I am calling. When I use a different method call from the Rijndael class (EncryptToBytes) it compiles fine with no errors.

Here is the statement that works:

EncryptedPassword = RijndaelEnhanced.EncryptToBytes(plainTextBytes: Encoding.UTF8.GetBytes(SMTPModel.SMTPPassword));

Again, thanks for your time.

John Schultz
  • 672
  • 1
  • 10
  • 29
-2

use the Encoding namespace. Note that UTF8 encoding can be ASCII or other.

byte[] array = Encoding.UTF8.GetBytes("The Quick Brown Fox Jumped over the yellow moon");

Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21
  • Tried that as well with no luck. – John Schultz Aug 20 '16 at 06:59
  • You're saying your method call will accept string or byte input, but your error is that you're sending a string when a byte array is expected. We can't help you any more than this without seeing your code. The ONLY way that error gets thrown is if you send a string to something that is looking for a byte array. – Shannon Holsinger Aug 20 '16 at 07:10
  • Uncool to vote me down for trying to help. Yes - bad question, but we've all been there, and my answer was not incorrect based on the question asked. Vote down the question if you want, but why pick on me? – Shannon Holsinger Aug 20 '16 at 07:12
  • Note that I answered BEFORE The edit, when the OP was asking HOW to get an array of bytes from a string. If you can think of a better way to turn a string into an array of bytes, then please vote my answer down. – Shannon Holsinger Aug 20 '16 at 07:17