We have a existing old portal in asp with Sql server and now trying to get new features in new site developing in grails,java & same sql server.
Sometime ago, I was working on a login page so that the same old portal credentials could be used. One issue I had to address was that the web app captures the password in clear text but sends an encoded password to the database (where the password is also stored in our encoded format).
Now I was trying to implement a simple logic for encryption; add 20 to each character’s int value and convert it to char and store the encrypted password.
For example the java code-
StringBuffer t = new StringBuffer(“”);
String test = “sviadha”;for ( int i = 0; i < test.length(); ++i ) {
char c = test.charAt( i );
int k = (int) c ;
int kk = k +20;
char ss = (char)kk;
t = t.append(ss);
}
System.out.println(“original password =”+ test);
System.out.println(“encrypted password ==”+ t);
Now when I tried to match the stored password using new UI it was failed all the time.
No matter what I tried, in the Java-tier I could not get past the fact that by the time the password was received in the SQL-tier, there was an encoded password mismatch. So I worked around the problem, but passing the clear text password to the database and the stored procedure did the encoding and finally the validation.
After digging it found the real problem; it turns out that this is a MS SQL Server JDBC driver configuration. By default MS JDBC driver is sent to pass all strings as NVARCHAR, not VARCHAR. This forced a Unicode conversion on the way to the database.
That’s why the same encoding logic was working fine for asp-sql server based old site but not on java based new site.
Here’s the magic to change this behavior so VARCHAR are sent and received…
MyDataSource
jdbc:jtds:sqlserver://mac1.temp.test:1434;DatabaseName=MyDatabase;tds=8.0;lastupdatecount=false; sendStringParametersAsUnicode=false
net.sourceforge.jtds.jdbc.Driver
. . .
Read more…