Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJceks
LOCAL oPrng
LOCAL cAesKey
LOCAL cBlowfishKey
LOCAL cHmacKey
LOCAL cChachaKey
LOCAL cEncoding
LOCAL cPassword
LOCAL cFilePassword
LOCAL oSbJson
LOCAL oJson

nSuccess := 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.

oJceks := CreateObject("Chilkat.JavaKeyStore")

//  We'll need a pseudo-random number generator (PRNG) to generate symmetric keys.
oPrng := CreateObject("Chilkat.Prng")

//  Generate some keys..

//  128-bit AES key (16 bytes)
cAesKey := oPrng:GenRandom(16, "base64")

//  256-bit Blowfish key (32 bytes)
cBlowfishKey := oPrng:GenRandom(32, "base64")

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

//  ChaCha20 256-bit
cChachaKey := oPrng:GenRandom(32, "base64")

//  Add each secret key to the JCEKS
cEncoding := "base64"
cPassword := "secret"
oJceks:AddSecretKey(cAesKey, cEncoding, "AES", "my aes key", cPassword)
oJceks:AddSecretKey(cBlowfishKey, cEncoding, "BLOWFISH", "my blowfish key", cPassword)
//  For HMAC, we're using the us-ascii bytes for the key..
oJceks:AddSecretKey(cHmacKey, "ascii", "HMAC_SHA256", "my hmac key", cPassword)
oJceks:AddSecretKey(cChachaKey, cEncoding, "CHACHA", "my chacha20 key", cPassword)

cFilePassword := "password"
//  Write the JCEKs to a file.
nSuccess := oJceks:ToFile(cFilePassword, "qa_output/secretKeys.jceks")
IF (nSuccess != 1)
    ? oJceks:LastErrorText
    oJceks:destroy()
    oPrng:destroy()
    RETURN
ENDIF

//  We can also emit as a JWK Set..
oSbJson := CreateObject("Chilkat.StringBuilder")
nSuccess := oJceks:ToJwkSet("secret", oSbJson)
IF (nSuccess != 1)
    ? oJceks:LastErrorText
    oJceks:destroy()
    oPrng:destroy()
    oSbJson:destroy()
    RETURN
ENDIF

//  Emit the JSON in pretty-printed (indented) form:
oJson := CreateObject("Chilkat.JsonObject")
oJson:LoadSb(oSbJson)
oJson:EmitCompact := 0
? oJson: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"
//      }
//    ]
//  }
//

oJceks:destroy()
oPrng:destroy()
oSbJson:destroy()
oJson:destroy()