Sample code for 30+ languages & platforms
Java

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 Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.Cert;
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Rsa;
import com.chilkatsoft.chilkat2.StringBuilder;

public class ChilkatExample {

    public static void main(String[] args) {
        // 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("=", "");
        // }

        DateTime dt = new DateTime();
        dt.setFromCurrentSystemTime();
        String timeCreated = dt.getAsTimestamp(true);

        // Such as 2019-04-01T19:35:44-05:00
        System.out.println(timeCreated);

        StringBuilder sbToken = new StringBuilder();
        sbToken.append("validityTimeMinutes:=10.0;timeCreated:=");
        sbToken.append(timeCreated);

        Cert cert = new Cert();
        try {
            cert.loadPfxFile("Some path to p12/p12file_name.p12", "Password_for_p12");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        Rsa rsa = new Rsa();
        String signature;
        try {
            rsa.setX509Cert(cert, true);

            rsa.setEncodingMode("base64url");

            signature = rsa.signStringENC(sbToken.getAsString(), "sha256");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        sbToken.append(";signature:=");
        sbToken.append(signature);

        // Base64URL encode the result
        sbToken.encode("base64url", "utf-8");
        String token = sbToken.getAsString();

        System.out.println(token);
    }
}