× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



I'm working on a project where I'm exchanging encrypted information with
a Java based system. I'm using the CIPHER MI to do 128 bit AES and the
last issue that I'm working on is padding the last block of data
(they're 16 bytes) before it's encrypted to emulate how the Java
implementation is working. I'm using the PKCS5 method (Method 4 at
http://www.di-mgt.com.au/cryptopad.html explains the goal) and what I'd
like to do is eliminate the loop I'm using to add padding characters. 

An example of the result I'm looking for is this:

"inTest" is 5 bytes long. We're using 16 byte blocks (to match up with
Java) so I need to add 11 padding bytes to the end of the string. PKCS5
says to add the hexidecimal value of the number of bytes added to the
end of the string so I need to add 11 "x'0B'"'s after "ABCDE". The hex
value ends up looking like this:

C1C2C3C4C50B0B0B0B0B0B0B0B0B0B0B

When "inTest" is 16 bytes, I need to add 16 "x'10'"'s to the end (I
don't think this is correct per the spec but it's how Java is doing it)
with a result like this:

8182838485868788899192939495969710101010101010101010101010101010

Here's some code I whipped up to test adding the padding (this does work
correctly):

DinTest           S             16A   inz('ABCDE')
DoutTest          S             32A
Dlen              S             10I 0
Dctr              S             10I 0
DpadChar          S              1A
DpadChars         C
const(x'0102030405060708090A0B0C0D0E-
D                                     0F10')
DbigPad           C
const(x'1010101010101010101010101010-
D                                     1010')
 /free
    exsr pad;
    inTest = 'abcdefghijklmnop';
    exsr pad;

    *inlr = *on;

    begsr pad;
       len = %len(%trim(inTest));
       outTest = inTest;
       If (len < 16);
          padChar = %subst(padChars: 16-len: 1);
          for ctr = len+1 to 16;
             %subst(outTest: ctr: 1) = padChar;
          endfor;
       Else;
          %subst(outTest: 17: 16) = bigPad;
       EndIf;
    endsr;
 /end-free

I had considered a bunch of constants but that seems worse than the
loop. Any ideas?

Thanks,

Matt

As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.