Tokenization Examples
Java Client Example
package com.vormetric.app;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;
import com.jayway.jsonpath.*;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public class CTSClientSample {
public static void main(String[] args) {
new CTSClientSample().DoIt();
}
@SuppressWarnings("null")
private void DoIt(){
String credential = Base64.encodeBase64String("ctsuser1:Panda45".getBytes());
String ccn[] = {
"5471949763376677","5545127221796024","5353800637453171","5139918930790601",
"5267003853942275"
};
try {
// Tokenize request
String https_url = "https://ipaddress/vts/rest/v2.0/tokenize/";
URL myurl = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
String ccNum = "9453677629008564";
String jStr =
"{\"data\":\""+ccNum+"\",\"tokengroup\":\"t1\",\"tokentemplate\":\"Credit
Card\"}";
con.setRequestProperty("Content-length", String.valueOf(jStr.length()));
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Authorization","Basic "+credential);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream output = new DataOutputStream(con.getOutputStream());
output.writeBytes(jStr);
output.close();
BufferedReader rd=new BufferedReader(new InputStreamReader(con.getInputStream()
));
String line = "";
String strResponse = "";
while ((line = rd.readLine()) != null) {
strResponse=strResponse+line;
}
rd.close();
String token = JsonPath.read(strResponse, "$.token").toString();
con.disconnect();
System.out.println("Tokenize server: "+https_url);
System.out.println("Tokenize request: "+jStr);
System.out.println("Tokenize response: "+strResponse);
// Bulk tokenize
String jStrArray ="[";
for (int i=0;i<ccn.length;i++) {
jStrArray = jStrArray +
"{\"data\":\""+ccn[i]+"\",\"tokengroup\":\"t1\",\"tokentemplate\":\"Credit
Card\"}";
if (i<ccn.length-1) {
jStrArray = jStrArray + ",";
}
}
jStrArray=jStrArray+"]";
con = (HttpsURLConnection)myurl.openConnection();
con.setRequestProperty("Content-length", String.valueOf(jStrArray.length()));
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Authorization","Basic "+credential);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
output = new DataOutputStream(con.getOutputStream());
output.writeBytes(jStrArray);
output.close();
rd = new BufferedReader(new InputStreamReader(con.getInputStream() ));
line = "";
strResponse = "";
while ((line = rd.readLine()) != null) {
strResponse=strResponse+line;
}
rd.close();
System.out.println("Bulk Tokenize request: "+jStrArray);
System.out.println("Bulk Tokenize response: "+strResponse);
String[] tokens = new String[5];
for (int i=0;i<5;i++) {
tokens[i]=JsonPath.read(strResponse, "$["+i+"].token").toString();
}
// Detokenize request
https_url ="https://ipaddress/vts/rest/v2.0/detokenize/";
myurl = new URL(https_url);
con = (HttpsURLConnection)myurl.openConnection();
jStr="{\"token\":\""+token+"\",\"tokengroup\" :
\"t1\",\"tokentemplate\":\"Credit Card\"}";
con.setRequestProperty("Content-length", String.valueOf(jStr.length()));
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Authorization","Basic "+credential);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
output = new DataOutputStream(con.getOutputStream());
output.writeBytes(jStr);
output.close();
rd = new BufferedReader(new InputStreamReader(con.getInputStream() ));
line = "";
strResponse = "";
while ((line = rd.readLine()) != null) {
strResponse=strResponse+line;
}
rd.close();
con.disconnect();
System.out.println("Detokenize server: "+https_url);
System.out.println("Detokenize request: "+jStr);
System.out.println("Detokenize response: "+strResponse);
// Bulk DeTokenize
jStrArray ="[";
for (int i=0;i<ccn.length;i++) {
jStrArray = jStrArray + "{\"token\":\""+tokens[i]+"\",\"tokengroup\" :
\"t1\",\"tokentemplate\":\"Credit Card\"}";
if (i<ccn.length-1) {
jStrArray = jStrArray + ",";
}
}
jStrArray=jStrArray+"]";
con = (HttpsURLConnection)myurl.openConnection();
con.setRequestProperty("Content-length", String.valueOf(jStrArray.length()));
con.setRequestProperty("Content-Type","application/json");
con.setRequestProperty("Authorization","Basic "+credential);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
output = new DataOutputStream(con.getOutputStream());
output.writeBytes(jStrArray);
output.close();
rd = new BufferedReader(new InputStreamReader(con.getInputStream() ));
line = "";
strResponse = "";
while ((line = rd.readLine()) != null) {
strResponse=strResponse+line;
}
rd.close();
con.disconnect();
System.out.println("Bulk Detokenize request: "+jStrArray);
System.out.println("Bulk Detokenize response: "+strResponse);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
C# Client Example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Threading.Tasks;
using RestSharp;
using RestSharp.Authenticators;
using System.Windows.Forms;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace CTS2ClientSample
{
class Program
{
static public bool MyRemoteCertificateValidationCallback(System.Object sender,
X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true; // unsafe! Do not do this in production!
}
static void tokenize(RestClient client)
{
var request = new RestRequest("vts/rest/v2.0/tokenize", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new { tokengroup = "t1", tokentemplate = "Credit Card",
data = "1234567812345670" });
// execute the request
IRestResponse response = client.Execute(request);
var responsestatus = response.ResponseStatus;
var content = response.Content; // raw content as string
MessageBox.Show("Tokenize API returns: " + content,
responsestatus.ToString(), MessageBoxButtons.OK);
}
static void detokenize(RestClient client)
{
var request = new RestRequest("vts/rest/v2.0/detokenize", Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddBody(new { tokengroup = "t1", tokentemplate = "Credit Card",
token = "1234567812345670" });
// execute the request
IRestResponse response = client.Execute(request);
var responsestatus = response.ResponseStatus;
var content = response.Content; // raw content as string
MessageBox.Show("Detokenize API returns: " + content, responsestatus.ToString(),
MessageBoxButtons.OK);
}
static void Main(string[] args)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
ServicePointManager.ServerCertificateValidationCallback =
MyRemoteCertificateValidationCallback; // unsafe! Do not do this in production!
var client = new RestClient("https://10.10.10.10");
client.Authenticator = new HttpBasicAuthenticator("username",
"password");
tokenize(client);
detokenize(client);
}
}
}