Syntax violation error-C#-Sybase-PasswordEncryption

K

Karunakararao

Hi All

I'm using sybase database, I have a binary datatype column in a table, when
I'm trying to store the char(128) value into that, I'm getting syntax
violation error.

If I take "w" as the password its ascii value is 119, when you add +9 to
that, it will be 128. How to insert this value into a binary datatype
column. [ Generally when you input the ascii chars to this it will
automatically convert it to hexadecimal values and in the database you can
see only hexadecimal chars].

In a loop I'm writing this logic.
Byte[] tempBytes = Encoding.ASCII.GetBytes(UserPassword.Substring(intIndex,
1));

tempBytes = new Byte[]{Convert.ToByte((Convert.ToInt32(tempBytes[0]) + 9))};

strPasswd = strPasswd + Encoding.ASCII.GetString(tempBytes);

Thanks in Advance

Karunakarraop
 
P

Philippe Bertrand

Your problem has nothing to do with the database you are using. You should
post to the appropriate Sybase newsgroup for a better way to encrypt the
password (forums.sybase.com).

As for your code, try:

byte[] tempBytes = Encoding.ASCII.GetBytes(UserPassword);
for( int i = 0; i < tempBytes.Length; i++ ) {
// Add 9 and handle overflow
tempBytes[ i ] = (byte)((tempBytes[ i ] + 9) & 255);
}
// you now have an encoded array - don't know why you want to convert it
back to a string
// but if you do, just
string newPassword = Encoding.ASCII.GetString(tempBytes);

Again, this is a very weak way of hiding a password. Post the group
appropriate for your database to find out hold the database can do it
(encrypt) all automatically for you.

Philippe
 
J

Jon Skeet [C# MVP]

Philippe Bertrand said:
Your problem has nothing to do with the database you are using. You should
post to the appropriate Sybase newsgroup for a better way to encrypt the
password (forums.sybase.com).

As for your code, try:

byte[] tempBytes = Encoding.ASCII.GetBytes(UserPassword);
for( int i = 0; i < tempBytes.Length; i++ ) {
// Add 9 and handle overflow
tempBytes[ i ] = (byte)((tempBytes[ i ] + 9) & 255);
}

Just a couple of points:

1) Dealing with overflow is implicit in the cast to byte, unless your
code is in an unchecked context.

2) There can't be any overflow in this case, as every byte returned by
Encoding.ASCII.GetBytes is going to be in the range 0-127.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top