Can you help with 64 bit encoding please

RodtmcleanRodtmclean Member Posts: 87
Hi,

I am working on some integration with the irish revenue service which makes use of digital certificates. I have to create a certificate by opening a p12 file. In order to open the P12 file, the following steps are followed to create a password. It is step 3 that is causing the problem: -

To calculate the hashed password, follow these steps:
1. First get the bytes of the original password, assuming a "Latin-1" encoding. For the password "Baltimore1," these bytes are: 66 97 108 116 105 109 111 114 101 49 44 (i.e. the value of "B" is 66, "a" is 97, etc).

2. Then get the MD5 hash of these bytes. MD5 is a standard, public algorithm. Once again, for the password "Baltimore1," these bytes work out as: 223 238 161 24 62 121 39 143 115 167 51 163 245 231 226 94

>>>> This is all good :), I get the Bytes in 2 above, the MD5 hashing works out fine, the next part causes me problems <<<<<<

3. Finally, create the new password by Base64-encoding the bytes from the previous step. For example, the password, "Baltimore1," this is “3+6hGD55J49zpzOj9efiXg==”.
I am getting 4YytGD55J8Rz9TOc5IeDXg== instead


The code follows, it has not been fully cleaned up yet - apologies.

Many thanks & kind regards
Roddy
LOCAL GetCertificatePassword(PassWordIn : Text[30]) HashedPassWordOut : Text[30]
TempBlob.INIT;
TempBlob.CALCFIELDS(Blob);
TempBlob.Blob.CREATEOUTSTREAM(OStream);

BigTextVar.ADDTEXT(PassWordIn);
BigTextVar.WRITE(OStream);
TempBlob.INSERT;

TempBlob.CALCFIELDS(Blob);
TempBlob.Blob.CREATEINSTREAM(IStream);


MD5 := MD5.Create;
MyText:= BitCon.ToString(MD5.ComputeHash(IStream));
TempBlob.MODIFY;

TempBlob.CALCFIELDS(Blob);


//I get the answer back in My Text correct values as per step 2 above but in hexadecimal form &quot;AA-FE-23&quot; etc.
//Loop below converts back to dec from hex.
ICounter := 0;
WHILE MyText &lt;&gt; '' DO BEGIN
  IPosition := STRPOS(MyText,'-');
  ICounter += 1;

  IF IPosition = 0 THEN BEGIN
    MyText2[ICounter] := HexToInt(MyText);
    MyText := '';
  END ELSE BEGIN
    MyText2[ICounter] := HexToInt(COPYSTR(MyText,1,IPosition-1));
    MyText := COPYSTR(MyText,IPosition+1);
  END;
END;

CLEAR(TempBlob.Blob);
TempBlob.MODIFY;

TempBlob.CALCFIELDS(Blob);
TempBlob.Blob.CREATEOUTSTREAM(OStream);
//OStream.WRITE(MyText2);
OStream.WRITETEXT(MyText2);
TempBlob.MODIFY;

TempBlob.CALCFIELDS(Blob);
TempBlob.Blob.CREATEINSTREAM(IStream);
HashedPassWordOut := ToBase64String(IStream); 

EXIT(HashedPassWordOut);


//Function to convert base 16 to 10
LOCAL HexToInt(HexText : Text[50]) Result : Integer
HexDigits := '0123456789ABCDEF';

Result := 0;

WHILE HexText &lt;&gt; '' DO BEGIN
  Result := Result * 16;
  intVal := STRPOS(HexDigits,COPYSTR(HexText,1,1)) - 1;
  Result := Result + intVal;
  HexText := COPYSTR(HexText,2);
END;
EXIT(Result);

//Function to perform 64 bit encoding, this is just the function from TempBlob
ToBase64String(IStream : InStream) : Text
MemoryStream := MemoryStream.MemoryStream;
COPYSTREAM(MemoryStream,IStream);
Base64String := Convert.ToBase64String(MemoryStream.ToArray);
MemoryStream.Close;
EXIT(Base64String);

Best Answer

  • RodtmcleanRodtmclean Member Posts: 87
    Answer ✓
    I fixed this one. Basically, I was using a text variable to accept the characters from the MD5 hashing. Changing to an array of Bytes as below solved the issue :).

    TempBlob.Blob.CREATEOUTSTREAM(OStream);
    FOR ICounter := 1 TO EndCount DO
    OStream.WRITE(MyByte[ICounter]);

    //OStream.WRITETEXT(MyText2); //old code
    TempBlob.MODIFY;

    TempBlob.CALCFIELDS(Blob);
    TempBlob.Blob.CREATEINSTREAM(IStream);
    HashedPassWordOut := ToBase64String(IStream);

Answers

  • RodtmcleanRodtmclean Member Posts: 87
    Answer ✓
    I fixed this one. Basically, I was using a text variable to accept the characters from the MD5 hashing. Changing to an array of Bytes as below solved the issue :).

    TempBlob.Blob.CREATEOUTSTREAM(OStream);
    FOR ICounter := 1 TO EndCount DO
    OStream.WRITE(MyByte[ICounter]);

    //OStream.WRITETEXT(MyText2); //old code
    TempBlob.MODIFY;

    TempBlob.CALCFIELDS(Blob);
    TempBlob.Blob.CREATEINSTREAM(IStream);
    HashedPassWordOut := ToBase64String(IStream);
  • hrishihrishi Member Posts: 21
    Hi Rodtmclean,
    Just wondering if you would know how to convert base64-encoded byte array into a PDF please.
    Thanks
Sign In or Register to comment.