Sample code for 30+ languages & platforms
PowerBuilder

Duplicate Java Secure Token Creation

See more RSA Examples

Demonstrates how to duplicate some Java code that creates an RSA signature to create a base64 token.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Dt
string ls_TimeCreated
oleobject loo_SbToken
oleobject loo_Cert
oleobject loo_Rsa
string ls_Signature
string ls_Token

li_Success = 0

// This requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// This example duplicates the following Java code:

// public X509Certificate2 cert = new X509Certificate2(@"Some path to p12/p12file_name.p12","Password_for_p12"); 
// 
// public string GenerateSignToken(double timeValidityMin){ 
//   string equalsSign = ":="; 
//   string timeCreated = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffzzz"); 
//   string tokenTimeInfo = "validityTimeMinutes" + equalsSign + timeValidityMin + ";"+"timeCreated" + equalsSign + timeCreated; 
//   string signature = SignData(tokenTimeInfo); 
//   string secureToken = tokenTimeInfo + ";" + "signature" + equalsSign + signature; 
//   return Base64UrlEncode(secureToken); 
// } 
//  
// public string SignData(string stringToSign){ 
//   byte[] dataToSign = Encoding.UTF8.GetBytes(stringToSign); 
//   RSACryptoServiceProvider privKey = (RSACryptoServiceProvider)cert.PrivateKey; 
//   CspKeyContainerInfo containerInfo = new RSACryptoServiceProvider().CspKeyContainerInfo; 
//   CspParameters cspparams = new CspParameters(containerInfo.ProviderType, containerInfo.ProviderName, privKey.CspKeyContainerInfo.KeyContainerName); 
//   privKey = new RSACryptoServiceProvider(cspparams); 
//   string id = CryptoConfig.MapNameToOID("SHA256"); 
//   byte[] sign = privKey.SignData(dataToSign, id); 
//   bool res = privKey.VerifyData(dataToSign, id, sign); 
//   return Convert.ToBase64String(sign).Replace('+', '-').Replace('/', '_').Replace("=", ""); 
// } 
//  
// private static string Base64UrlEncode(string input){ 
//   var inputBytes = Encoding.UTF8.GetBytes(input); 
//   return Convert.ToBase64String(inputBytes).Replace('+', '-').Replace('/', '_').Replace("=", ""); 
// } 

loo_Dt = create oleobject
li_rc = loo_Dt.ConnectToNewObject("Chilkat.CkDateTime")
if li_rc < 0 then
    destroy loo_Dt
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Dt.SetFromCurrentSystemTime()
ls_TimeCreated = loo_Dt.GetAsTimestamp(1)

// Such as 2019-04-01T19:35:44-05:00
Write-Debug ls_TimeCreated

loo_SbToken = create oleobject
li_rc = loo_SbToken.ConnectToNewObject("Chilkat.StringBuilder")

loo_SbToken.Append("validityTimeMinutes:=10.0;timeCreated:=")
loo_SbToken.Append(ls_TimeCreated)

loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("Some path to p12/p12file_name.p12","Password_for_p12")
if li_Success <> 1 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Dt
    destroy loo_SbToken
    destroy loo_Cert
    return
end if

loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

li_Success = loo_Rsa.SetX509Cert(loo_Cert,1)
if li_Success <> 1 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Dt
    destroy loo_SbToken
    destroy loo_Cert
    destroy loo_Rsa
    return
end if

loo_Rsa.EncodingMode = "base64url"

ls_Signature = loo_Rsa.SignStringENC(loo_SbToken.GetAsString(),"sha256")
if loo_Rsa.LastMethodSuccess = 0 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_Dt
    destroy loo_SbToken
    destroy loo_Cert
    destroy loo_Rsa
    return
end if

loo_SbToken.Append(";signature:=")
loo_SbToken.Append(ls_Signature)

// Base64URL encode the result
loo_SbToken.Encode("base64url","utf-8")
ls_Token = loo_SbToken.GetAsString()

Write-Debug ls_Token


destroy loo_Dt
destroy loo_SbToken
destroy loo_Cert
destroy loo_Rsa