Sample code for 30+ languages & platforms
PowerBuilder

Create JCEKS Containing Secret Keys

See more Java KeyStore (JKS) Examples

Demonstrates how to create a JCEKS keystore file containing symmetric secret keys (for AES, Blowfish, HMAC SHA25, ChaCha20, etc.)

This example requires Chilkat v9.5.0.66 or greater.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jceks
oleobject loo_Prng
string ls_AesKey
string ls_BlowfishKey
string ls_HmacKey
string ls_ChachaKey
string ls_Encoding
string ls_Password
string ls_FilePassword
oleobject loo_SbJson
oleobject loo_Json

li_Success = 0

// IMPORTANT: This example requires Chilkat v9.5.0.66 or greater.

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

loo_Jceks = create oleobject
li_rc = loo_Jceks.ConnectToNewObject("Chilkat.JavaKeyStore")
if li_rc < 0 then
    destroy loo_Jceks
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// We'll need a pseudo-random number generator (PRNG) to generate symmetric keys.
loo_Prng = create oleobject
li_rc = loo_Prng.ConnectToNewObject("Chilkat.Prng")

// Generate some keys..

// 128-bit AES key (16 bytes)
ls_AesKey = loo_Prng.GenRandom(16,"base64")

// 256-bit Blowfish key (32 bytes)
ls_BlowfishKey = loo_Prng.GenRandom(32,"base64")

// HMAC SHA256 key
// (An HMAC key can be anything, and any length. We'll use the following string:
ls_HmacKey = "This is my HMAC key"

// ChaCha20 256-bit
ls_ChachaKey = loo_Prng.GenRandom(32,"base64")

// Add each secret key to the JCEKS
ls_Encoding = "base64"
ls_Password = "secret"
loo_Jceks.AddSecretKey(ls_AesKey,ls_Encoding,"AES","my aes key",ls_Password)
loo_Jceks.AddSecretKey(ls_BlowfishKey,ls_Encoding,"BLOWFISH","my blowfish key",ls_Password)
// For HMAC, we're using the us-ascii bytes for the key..
loo_Jceks.AddSecretKey(ls_HmacKey,"ascii","HMAC_SHA256","my hmac key",ls_Password)
loo_Jceks.AddSecretKey(ls_ChachaKey,ls_Encoding,"CHACHA","my chacha20 key",ls_Password)

ls_FilePassword = "password"
// Write the JCEKs to a file.
li_Success = loo_Jceks.ToFile(ls_FilePassword,"qa_output/secretKeys.jceks")
if li_Success <> 1 then
    Write-Debug loo_Jceks.LastErrorText
    destroy loo_Jceks
    destroy loo_Prng
    return
end if

// We can also emit as a JWK Set..
loo_SbJson = create oleobject
li_rc = loo_SbJson.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Jceks.ToJwkSet("secret",loo_SbJson)
if li_Success <> 1 then
    Write-Debug loo_Jceks.LastErrorText
    destroy loo_Jceks
    destroy loo_Prng
    destroy loo_SbJson
    return
end if

// Emit the JSON in pretty-printed (indented) form:
loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.LoadSb(loo_SbJson)
loo_Json.EmitCompact = 0
Write-Debug loo_Json.Emit()

// Output is:

// { 
//   "keys": [
//     { 
//       "kty": "oct",
//       "alg": "AES",
//       "k": "vHekQQB0Gc1NvppapUTW2g",
//       "kid": "my aes key"
//     },
//     { 
//       "kty": "oct",
//       "alg": "BLOWFISH",
//       "k": "qHsdXaJsXicVCZbK8l8hJQpYOa0GkiO9gsRK9WLtht8",
//       "kid": "my blowfish key"
//     },
//     { 
//       "kty": "oct",
//       "alg": "HMAC_SHA256",
//       "k": "VGhpcyBpcyBteSBITUFDIGtleQ",
//       "kid": "my hmac key"
//     },
//     { 
//       "kty": "oct",
//       "alg": "CHACHA",
//       "k": "yNv832U43C9BcWvaQAH2_rG-GwfmpgT5JBRllWGQY1o",
//       "kid": "my chacha20 key"
//     }
//   ]
// }
// 


destroy loo_Jceks
destroy loo_Prng
destroy loo_SbJson
destroy loo_Json