bear0053 has asked for the wisdom of the Perl Monks concerning the following question:
when the code is stored it is stored a 7 bit ascii even parity. so some how perl takes 7 bit ascii and turns it into 7 bit ascii even parity when it writes it to the database. why and how can i stop this? This would be fine if my .net application would read the data from the sql table in as 7 bit ascii even parity and not convert it. But it doesn't. Instead c#.net reads the value from the db as 7 bit ascii even parity and then when it is turned into a byte[] gets converted to 8bit and then cannot be decrypted.my $obj = lockpan(); //interacts with a vb6 dll my $enc_data = obj->encryptval($data); //returns binary val $enc_data = '0x' . unpack('H*', $enc_data); //bin -> hex #$enc_data is then written to a sql db as 7 bit ascii even parity
//enc_data is a hex formatted string generated by the the COM object m +y .net apps interact with. but in order for the .net app to decrypt t +he hex value returned by the same com object to perl the hex string m +ust be transformed into the appropriate parity format by doing the fo +llowing enc_data = Regex.Replace(enc_data, "^0x", ""); enc_data = FromUnicodeByteArray( hex_to_bin(enc_data) ); //now enc_data contains a value that my .net app can send to the com o +bject for decryption and get back the correct value private static byte[] hex_to_bin ( string s ) { int stringLength = s.Length; if ( (stringLength & 0x1) != 0 ) { Console.WriteLine("not even numbers"); } byte[] b = new byte[ stringLength / 2 ]; for ( int i=0 ,j= 0; i< stringLength; i+= 2,j ++ ) { int high= charToNibble( (s.Substring ( i,1 ).ToCharArr +ay())[0]); int low = charToNibble( (s.Substring(i+1,1).ToCharArra +y())[0] ); b[ j ] = (byte ) ( ( high << 4 ) | low ); } return b; } /** * convert a single char to corresponding nibble. * * @param c char to convert. must be 0-9 a-f A-F, no * spaces, plus or minus signs. * * @return corresponding integer */ private static int charToNibble ( char c ) { if ( '0' <= c && c <= '9' ) { return c - '0' ; } else if ( 'a' <= c && c <= 'f' ) { return c - 'a' + 0xa ; } else if ( 'A' <= c && c <= 'F' ) { return c - 'A' + 0xa ; } else { Console.WriteLine( "Invalid hex character: " + c ) ; return 0; } } private static string FromUnicodeByteArray(byte[] characters) { string constructedString = Encoding.Default.GetString(char +acters); return (constructedString); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: convert 7 bit even parity to 7 bit ascii
by iburrell (Chaplain) on Mar 09, 2004 at 19:52 UTC | |
by bear0053 (Hermit) on Mar 09, 2004 at 22:40 UTC | |
by theorbtwo (Prior) on Mar 10, 2004 at 03:34 UTC | |
by iburrell (Chaplain) on Mar 10, 2004 at 22:01 UTC | |
by bear0053 (Hermit) on Apr 06, 2004 at 17:03 UTC |