(Solved) – How do I convert private key to PKCS8 format in VBA?

  • by
(solved)-–-how-do-i-convert-private-key-to-pkcs8-format-in-vba?

I am writing some API code in vba to connect to a secure endpoint. Part of the process requires me to send a payload with which is signed using my private key. Is there someone there who can help me convert a private key to PKCS8 format in VBA?

Below is some code that exactly represents what I want to accomplish and I know works although in Java. What I would like is something similar in VBA.

=====

function _genJWS() {

var sHead = "some value";
var sPayload = "Some payload";
var sPemPrvKey = "Some private key";

var sResult = null;

try {
  prv = KEYUTIL.getKey(sPemPrvKey);
  sResult = sign("RS256", sHead, sPayload, prv);
  "some value" = sResult;
 } catch (ex) {
  alert("Error: "   ex);
  }
}

function sign(alg, spHeader, spPayload, key, pass) {

var sHeader, pHeader, sPayload;

sHeader = spHeader;
if (! KJUR.jws.JWS.isSafeJSONString(sHeader))
    throw "JWS Head is not safe JSON string: "   sHeader;
pHeader = KJUR.jws.JWS.readSafeJSONString(sHeader);

sPayload = spPayload;

if (alg !== pHeader.alg)
    throw "alg and sHeader.alg doesn't match: "   alg   "!="   
pHeader.alg;

var sigAlg = KJUR.jws.JWS.jwsalg2sigalg[alg];

var uHeader = utf8tob64u(sHeader);
var uPayload = sPayload;
var uSignatureInput = uHeader   "."   uPayload

var hSig = "";
if (sigAlg != "none") {
  var sig = new KJUR.crypto.Signature({'alg': sigAlg});
    sig.init(key, pass);
    sig.updateString(uSignatureInput);
    hSig = sig.sign();
}

var uSig = hextob64u(hSig);
return uHeader   ".."   uSig;
};

Leave a Reply

Your email address will not be published. Required fields are marked *