why can't convert Symbian CRSAPublicKey to des

2019-09-18 03:07发布

问题:

code as following:

const CRSAPublicKey& iRSAPublicKey = mRSAKeyPair->PublicKey();
const TInteger& e = iRSAPublicKey.E();
HBufC8* exponent8 = e.BufferLC(); //failed to get the right result,it's garbled
TInt ei = e.ConvertToLongL();    //converted successfully,ei=65537

can anyone tell me why BufferLC() doesn't work?is something important i just have missed?,and how to convert a TInterger to descriptor? thanks in advance.

回答1:

It't not garbled, it just the integer's binary representation as stated in the documentation. The same way as the byte 0x33 is the binary representation for ASCII 3.

I assume you want something printable of the TInteger. For small TIntegers for which IsConvertableToLong() is true, you can use the ConvertToLong() approach with one of the TDes::AppendNum() overloads. For larger integers, I think you need to roll your own conversion function. The most straightforward way is probably just to output the binary representation bytes as hex.


Edited to add: Here's an untested snippet for doing the hex conversion.

HBufC8 *bin = theTInteger.BufferLC();

// One binary byte yields two hex bytes
HBufC8 *output = HBufC8::NewL(bin->Length()*2);
TPtr8 outP = output->Des();

// Loop through the binary bytes  
for (int i = 0; i < bin->Length(); ++i) {
  TUint8 const KHex[] = "01234567890abcdef";
  TUint8 binByte = (*bin)[i];

  // Output high nibble (4 bits)
  outP.Append(KHex[(binByte & 0xf0) >> 4]);

  // Output low nibble
  outP.Append(KHex[binByte & 0x0f]);
}

CleanupStack::PopAndDestroy(bin);


标签: symbian