-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;
13
  • Encoding.UTF8.GetBytes("some string"); Commented Aug 20, 2016 at 6:57
  • Yeah, I tried that too with no luck. Commented Aug 20, 2016 at 6:58
  • If you're using `Encoding.WhateverEncoding.GetBytes()/GetString() this exception won't be trhown it just can't. Check: msdn.microsoft.com/en-us/library/744y86tc(v=vs.110).aspx , msdn.microsoft.com/en-us/library/ds4kkd55(v=vs.110).aspx Commented Aug 20, 2016 at 6:58
  • 1
    @JohnSchultz this converts the string to byte array for sure. Show your code Commented Aug 20, 2016 at 7:00
  • 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 minimal reproducible example that reliably reproduces the problem. Commented Aug 20, 2016 at 7:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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");

4 Comments

Tried that as well with no luck.
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.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.