Skip to main content

Working with Helper Functions

Creating T-Tags (hidden links), encrypting and decrypting strings and arrays, and hashing data.


Code Samples

See the Java environment settings for details on setting the project environment.


Java_HelperFunctions.java

The following example demonstrates different encryption tasks.

/*****************************************************************************************
*
* Java_HelperFunctions.java
*
******************************************************************************************/
import com.eruces.teagent.TEAgentConnection;
import com.eruces.teagent.TEAgentException;
import com.eruces.teagent.TEAgentConnectionFactory;
import com.eruces.teagent.TEAgentMatrix;
public class Java_HelperFunctions
{
public static String generateHiddenLink(TEAgentConnection conn) throws TEAgentException
{
String strOriginal = "This is a dummy string.";
StringBuffer strbufHL = new StringBuffer();
encryptSingle(conn, strOriginal, strbufHL);
return strbufHL.toString();
}
public static String encryptSingle(TEAgentConnection conn, String originalData, StringBuffer strbufHL) throws TEAgentException
{
String[] strIns = new String[1];
String[] strOuts = new String[1];
strIns[0] = originalData;
encryptArray(conn, strIns, strOuts, strbufHL);
return strOuts[0];
}
public static String decryptSingle(TEAgentConnection conn, String encryptedData, String strHL) throws TEAgentException
{
String[] strIns = new String[1];
String[] strOuts = new String[1];
strIns[0] = encryptedData;
decryptArray(conn, strIns, strOuts, strHL);
return strOuts[0];
}
public static void encryptArray(TEAgentConnection conn, String[] originalData, String[] encryptedData, StringBuffer strbufHL) throws TEAgentException
{
int nRows = 1;
int nCols = originalData.length;
int i, j;
String strHLs [] = new String[nRows];
String strMatrix [][] = new String[nRows][nCols];
TEAgentMatrix matrix = new TEAgentMatrix(); // Create TEAgentMatrix object
// Set data to be encrypted
strHLs[0] = strbufHL.toString(); // Set T-tag
for(j = 0; j < nCols; j++)
strMatrix[0][j] = originalData[j]; // Data to be encrypted
// Set the matrix for encryption
for(i = 0; i < strMatrix.length; i++)
{
// Set T-tag
if (strHLs[i] != null && !strHLs[i].equals(""))
matrix.setHiddenLink(strHLs[i], i+1);
// Set data for encryption. For TEAgentMatrix, both row count and column count starts at 1
for(j = 0; j < strMatrix[i].length; j++)
matrix.setRawElement(strMatrix[i][j].getBytes(), i+1, j+1);
}
// Encrypt data
matrix.attachConnection(conn);
matrix.encrypt();
matrix.detachConnection();
// Get encrypted data
for(i = 1; i <= matrix.getRowCount(); i++)
{
strHLs[i-1] = matrix.getHiddenLink(i);
//strHLs[i-1] = matrix.getElement(i, 0); // Column 0 contains T-tag
for(j = 1; j < matrix.getColCount(); j++)
strMatrix[i-1][j-1] = matrix.getElement(i, j);
}
if (strbufHL.toString() == null || strbufHL.toString().equals("") )
{
strbufHL.append(strHLs[0]); // if T-tag is not provided by caller, returt it.
}
for(j = 0; j < nCols; j++)
encryptedData[j] = strMatrix[0][j]; // Data to be encrypted
}
public static void decryptArray(TEAgentConnection conn, String[] encryptedData, String[] decryptedData, String strHL) throws TEAgentException
{
int nRows = 1;
int nCols = encryptedData.length;
int i, j;
String strHLs [] = new String[nRows];
String strMatrix [][] = new String[nRows][nCols];
strHLs[0] = strHL;
for (j=0; j<nCols; j++)
strMatrix[0][j] = encryptedData[j];
// Set the matrix for decryption
TEAgentMatrix matrix2 = new TEAgentMatrix(); // Create TEAgentMatrix object
for(i = 0; i < strMatrix.length; i++)
{
// Set T-tag
matrix2.setHiddenLink(strHLs[i], i+1);
for(j = 0; j < strMatrix[i].length; j++)
matrix2.setElement(strMatrix[i][j], i+1, j+1);
}
// Decrypt data
matrix2.attachConnection(conn);
matrix2.decrypt();
matrix2.detachConnection();
// Get decrypted data
for(i = 1; i <= matrix2.getRowCount(); i++)
{
strHLs[i-1] = matrix2.getHiddenLink(i);
for(j = 1; j < matrix2.getColCount(); j++)
strMatrix[i-1][j-1] = new String(matrix2.getRawElement(i, j));
}
for(j = 0; j < nCols; j++)
decryptedData[j] = strMatrix[0][j]; // return decrypted data
}
public static String hashSingle(String strData, String strSalt) throws TEAgentException
{
String strHashed = null;
TEAgentMatrix matrix = new TEAgentMatrix();
strHashed = matrix.performHash(strData, strSalt); // SHA-256
return strHashed;
}
public static TEAgentConnection getConnect(String strServerName, int nPort, String strUsername, String strPasswd) throws TEAgentException
{
TEAgentConnection conn = null;
// Get a connection instance
conn = TEAgentConnectionFactory.getInstance(TEAgentConnectionFactory.USER_PASSWORD);
// Make connection
conn.open(strServerName, nPort, strUsername, strPasswd);
System.out.println("Connected to " + strServerName + " at " + nPort + " as " + strUsername);
return conn;
}
public static void main(String [] args)
{
try
{
TEAgentConnection conn = getConnect("sampleserver", 8080, "user", "password");
// Generate HiddenLink
String strHiddenLink = generateHiddenLink(conn);
System.out.println("strHiddenLink = " + strHiddenLink);
// Test encrypt/decrypt single string.
String strOriginal = "This is a test string.";
StringBuffer strbufHL = new StringBuffer();
String strEncrypted = encryptSingle(conn, strOriginal, strbufHL);
String strHL = strbufHL.toString();
System.out.println();
System.out.println();
System.out.println("strOriginal = " + strOriginal);
System.out.println("strEncrypted = " + strEncrypted);
System.out.println("strHL = " + strHL);

String strDecrypted = decryptSingle(conn, strEncrypted, strHL);
System.out.println("strDecrypted = " + strDecrypted);
// Test encrypt/decrypt an array of strings.
String[] originalData = new String[5];
originalData[0] = "Test string 1";
originalData[1] = "Test string 2";
originalData[2] = "Test string 3";
originalData[3] = "Test string 4";
originalData[4] = "Test string 5";

String[] encryptedData = new String[5];
strbufHL = new StringBuffer();
encryptArray(conn, originalData, encryptedData, strbufHL);
strHL = strbufHL.toString();
String[] decryptedData = new String[5];
decryptArray(conn, encryptedData, decryptedData, strHL);
System.out.println();
System.out.println();
System.out.println("originalData:");
for (int i=0; i<originalData.length; i++)
{
System.out.println(originalData[i]);
}
System.out.println("encryptedData:");
for (int i=0; i<encryptedData.length; i++)
{
System.out.println(encryptedData[i]);
}
System.out.println("strHL = " + strHL);
System.out.println("decryptedData:");
for (int i=0; i<decryptedData.length; i++)
{
System.out.println(decryptedData[i]);
}
// Hash single string
String strData = "This is a test string.";
String strSalt = "";
String strHashValue = hashSingle(strData, strSalt);
System.out.println();
System.out.println();
System.out.println("strData = " + strData);
System.out.println("strHashValue = " + strHashValue);
strHashValue = hashSingle(strData, strSalt);
System.out.println("strData = " + strData);
System.out.println("strHashValue = " + strHashValue);
}
catch (TEAgentException teex)
{
System.out.println("Exception: " + teex.getMessage());
}
}
}