We have a program on our AS400 that takes in a number, encrypts it and then passes the encrypted characters back. Using the CWBX stringConverter.FromBytes method… I am able to convert the numberic byte / ascii code value, into its appropriate character. This has been working fine for a while, until we noticed some values getting cut off.
The byte array returned looks like this:
(70, 126, 135, 27, 0, 201, 222, 229, 52, 75, 61, 121, 34, 65, 113, 215)
Where 240 = “0”, 241 = “1”, 64 = [Blank Space], etc… The problem is with the 5th byte being equal to zero. This is [Null].
Therefore instead of seeing something like: #^!@ 7′<“&$#T~*(
I am instead seeing: #^!@
Because the 5th character element is [Null],it escentially cuts off that character as well as the rest of my string!
Normally, i would just check for the byte element = 0, and then do something else. The only problem is, i have to insert this data into a database so that it can be decrypted later on, so i cannot alter the character and change it to lets say an “#” character.
I have tried looping through each value, and if the byte equals zero then appending vbNull onto the string, but then when its decrypted it does not come back as the same byte ascii code “0”, it comes back as “241” which is vbNull.
Here is my current code:
Dim as400 As cwbx.AS400System
as400 = New cwbx.AS400SystemClass()
Dim program As cwbx.Program = New cwbx.Program()
Dim strResult(1) As String
as400.Define(RPGServer)
program.system = as400
program.system.UserID = RPGUsername
program.system.Password = RPGPassword
program.LibraryName = CurrentLibrary
program.ProgramName = EncryptionProgram
If as400.IsConnected(cwbx.cwbcoServiceEnum.cwbcoServiceRemoteCmd) = 0 Then
as400.Disconnect(cwbx.cwbcoServiceEnum.cwbcoServiceAll)
as400.Connect(cwbx.cwbcoServiceEnum.cwbcoServiceRemoteCmd)
End If
Dim stringConverter As cwbx.StringConverter = New cwbx.StringConverterClass()
Dim parameters As cwbx.ProgramParameters = New cwbx.ProgramParametersClass()
stringConverter.Length = 16
parameters.Append("&INNUM", cwbx.cwbrcParameterTypeEnum.cwbrcInput, 16)
parameters("&INNUM").Value = stringConverter.ToBytes(CardNo.PadRight(16, " "))
parameters.Append("&OUTNUM", cwbx.cwbrcParameterTypeEnum.cwbrcOutput, 16)
program.Call(parameters)
strResult(0) = stringConverter.FromBytes(parameters("&OUTNUM").Value)
And here is what I tried with changing the character:
stringConverter.Length = 16
Dim byteArr() As Byte = {22, 11, 57, 27, 0, 221, 252, 143, 52, 54, 66, 72, 31, 65, 24, 77}
Dim strValue As String = ""
For Each b As Byte In byteArr
stringConverter.Length = 1
If b = 0 Then
strValue &= vbNull
Else
Dim tbb2() As Byte = {b}
strValue &= stringConverter.FromBytes(tbb2)
End If
Next
stringConverter.Length = 16
Dim tttt As Byte() = stringConverter.ToBytes(strValue)
Do not convert a byte[] to a string with an EBCDIC string converter. While 0 is a perfectly reasonable byte value, it often acts as a string terminator. It does so for the debugger (note the missing trailing double quote), it isn’t unlikely to do so as well for your dbase provider. If you don’t want to store the value as a binary blob in the dbase then encode it with Convert.ToBase64String(). Read it back with Convert.FromBase64String().