How i can apply custom function with tmap, or may be with tsystem. I want to decrypt crypted columns on-the-fly with my custom function. I can write all encrypted values to file, then write from file to tsystem, or tmap, and decrypt values. What is the best way for this?
Use Java routines. you can create java methods and call anywhere. for example
public static String decrypt(String encryptStr){
String decrypted = null;
try {
while(encryptStr != null){
try
{
String key = "Bar12345Bar12345"; // 128 bit key
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
// for decryption
byte[] bb = new byte[encryptStr.length()];
for (int i=0; i<encryptStr.length(); i++) {
bb[i] = (byte) encryptStr.charAt(i);
}
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
decrypted = new String(cipher.doFinal(bb));
}
catch(Exception e)
{
e.printStackTrace();
}
}
} catch (IOException ex) {
Logger.getLogger(Snake_H.class.getName()).log(Level.SEVERE, null, ex);
}
return decrypted;
}
For encrypts follow the same kind of methods. you can call this java method at anywhere like tmap
Reference Talend Routines
If you are speaking about routines, simply call the desired method everywhere you can put some java code.
For example, as an expression for the output flow, you can have something like:yourClass.yourMethod(...)
Hope this helps.
Related
When I try to convert cas to xmi, I'm receiving UIMARuntimeException due to �" (an invalid XML character). Thanks in advance.
Exception:
Caused by: org.xml.sax.SAXParseException; lineNumber: 190920; columnNumber: 36557; Character reference "�" is an invalid XML character.
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.apache.uima.util.XmlCasDeserializer.deserializeR(XmlCasDeserializer.java:111)
at org.apache.uima.util.CasIOUtils.load(CasIOUtils.java:366)
Code:
private static void serialize(CAS cas, File file) throws SAXException, IOException {
Watch casToXmi = new Watch(Path.getFileName() + "Cas to Xmi Convertion - "+file.getName());
casToXmi.start();
OutputStream outputStream = null;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(file));
XmiCasSerializer xmiSerializer = new XmiCasSerializer(cas.getTypeSystem());
XMLSerializer xmlSerializer = new XMLSerializer(outputStream, true);
xmiSerializer.serialize(cas,xmlSerializer.getContentHandler());
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException(fnfe.getMessage());
} catch (SAXException saxe) {
throw new SAXException(saxe.getMessage());
} finally {
try {
outputStream.close();
} catch (IOException ioe) {
throw new IOException(ioe.getMessage());
}
}
casToXmi.stop();
}
Per default, the XMI is serialized as XML 1.0. XML 1.0 has a restricted range of characters that it can represent.
But UIMA has the CasIOUtils which make it really easy to write our data out:
out = new FileOutputStream(this.outputFile);
CasIOUtils.save(cas, out, SerialFormat.XMI_1_1);
Alternatively, you can configure the serializer in your code to produce XML 1.1 instead which might resolve your issue:
XMLSerializer sax2xml = new XMLSerializer(docOS, prettyPrint);
sax2xml.setOutputProperty(OutputKeys.VERSION, "1.1");
These lines were taken from the XmiWriter of DKPro Core.
Note: I see your code includes a Watch. If speed is your concern, then there are other supported formats which save/load considerably faster than XMI, e.g. the binary format SerialFormat.COMPRESSED_FILTERED_TSI. Unlike XMI This format also supports any characters in the text.
Disclaimer: I am part of the Apache UIMA project and the maintainer of DKPro Core.
I used SerialFormat.BINARY which will give plain custom binary serialized CAS without type system, no filtering.
private static void serialize(CAS cas, File file) throws SAXException, IOException {
Watch casToXmi = new Watch(Path.getFileName() + "Cas to Xmi Convertion - "+file.getName());
casToXmi.start();
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
CasIOUtils.save(cas, outputStream, SerialFormat.BINARY);
} catch (FileNotFoundException fnfe) {
throw new FileNotFoundException(fnfe.getMessage());
} finally {
try {
outputStream.close();
} catch (IOException ioe) {
throw new IOException(ioe.getMessage());
}
}
casToXmi.stop();
}
I need to figure out how to generate a random 256 AES CBC key with PKCS5 padding in Dart for my Flutter project. I've been able to accomplish it in Java using this:
public static SecretKey generateSecretKey() {
KeyGenerator generator;
try {
generator = KeyGenerator.getInstance("AES/CBC/PKCS5Padding");
generator.init(256); // The AES key size in number of bits (256)
return generator.generateKey();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
In dart I've gotten as far as this (broken/unfinished code) using PointyCastle
static generateSecretKey() {
AESFastEngine aes = AESFastEngine();
KeyParameter keyParameter = KeyParameter();
aes.init(true, keyParameter);
CBCBlockCipher cbc = CBCBlockCipher(aes);
}
I'm using RSA 4096 keys for exchanging the AES 256 key. In the client side, I only use the public key for encrypting the generated AES key
I think the best option is use this package
#James Reinstate Monica Polk
Apparently dart provides this for you: https://www.scottbrady91.com/Dart/Generating-a-Crypto-Random-String-in-Dart
This seems to be a good solution for generating an AES 256 bit key.
I have the contents of what a feed is sending to the search appliance for indexing, but one XML node is base64compressed. Looking at the GSA docs' custom feed are to be constructed by compressing (zlib) and then encoding them. I tried to reverse the process by decoding and then using 7zip to open it but it did not work.
Rationale: I am looking at this is as GSA is EOL, we are moving to Solr but will continue to use some GSA Connectors for the time being (they are open source). I need to look at the text contents of what gets indexed to the search appliance so I can construct a proper Solr schema.
My experience with GSA is very minimal so I may be thinking about this all wrong, would appreciate any suggestions on how to tackle this.
Thanks!
This code will decode then uncompress the base64compressed item in a GSA feed.
private byte[] decodeUncompress(byte[] data) throws IOException {
// Decode
byte[] decodedBytes = Base64.getDecoder().decode(data);
// Uncompress
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Inflater decompresser = new Inflater(false);
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(stream, decompresser);
try {
inflaterOutputStream.write(decodedBytes);
} catch (IOException e) {
throw e;
} finally {
try {
inflaterOutputStream.close();
} catch (IOException e) {
}
}
return stream.toByteArray();
}
For the life of me I can't find out the answer to what I thought would have been a simple question - how to find the data typeof an OPC Node.
I'm writing a UA OPC library which involves creating a subscription and the monitoring/updating of tags within this subscription.
The request to write to a tag comes from an external application and consists of Subscription and tag ID together with an object value. The issue is that this object is invariably a string (don't ask), which needs to be converted to an appropriate data type for the tag. So, I need to find out what data type is associated with the tag. At the moment, the writing fails with a type mismatch exception.
Here's my update code:
//
// Extension methods
public bool Write<T>(long groupID, long tagID, T item)
{
var sub = FindSubscription(groupID);
try
{
if (sub != null)
{
var node = sub.MonitoredItems.FirstOrDefault(m => (long)m.Handle == tagID);
if (node != null)
{
bool result = false;
var nodesToWrite = BuildWriteValueCollection(node.DisplayName, Attributes.Value, item);
StatusCodeCollection results;
DiagnosticInfoCollection diag;
try
{
_session.Write(
requestHeader: null,
nodesToWrite: nodesToWrite,
results: out results,
diagnosticInfos: out diag);
result = CheckReturnValue(results[0]);
}
catch (Exception ex)
{
}
}
}
}
catch (Exception ex)
{
LogMessage(String.Format("Write GroupID {0}, Tag {1}, Value {2}", groupID, tagID, item.ToString()), ex);
}
return false;
}
I've searched for ages to find out how to determine the data type of the node, but without success. So, for a MonitoredItem, how would one go about finding the data type so that I can convert the string to a compatible type?
Thanks
Steve
Read the DataType attribute of the VariableNode in question.
I can provide more detail if necessary, but my question is basically thus:
If I'm running an openfire server that encrypts traffic using an RSA pub/priv key combo that I created (and have), is there a way (preferably in Java) to sniff packets off the wire and then decrypt them using my private key? Currently I can encrypt/decrypt a string using the following:
public class TLSDecryptTest {
Cipher Ecipher;
Cipher Dcipher;
public TLSDecryptTest(String pubpath, String privpath){
byte[] publicKeyContentsAsByteArray;
RSAPublicKey pubKey;
try {
this.Ecipher = Cipher.getInstance("RSA");
String path1 = new String("C:\\Users\\peter.marino\\Desktop\\javapub.key");
File pubFile = new File(path1);
publicKeyContentsAsByteArray = new byte[(int)pubFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(pubFile));
publicKeyContentsAsByteArray = new byte[(int)pubFile.length()];
bis.read(publicKeyContentsAsByteArray);
bis.close();
CertificateFactory certificateFactory = CertificateFactory.getInstance("X509");
Certificate certificate = certificateFactory.generateCertificate(new ByteArrayInputStream(publicKeyContentsAsByteArray));
pubKey = (RSAPublicKey) certificate.getPublicKey();
this.Ecipher.init(Cipher.ENCRYPT_MODE, pubKey);
} catch(Exception e) {
System.out.println("Exception" + e);
}
try {
this.Dcipher = Cipher.getInstance("RSA");
String path2 = new String("C:\\Users\\peter.marino\\Desktop\\java.key");
File privFile = new File(path2);
byte[] privateKeyContentsAsByteArray = new byte[(int)privFile.length()];
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(privFile));
privateKeyContentsAsByteArray = new byte[(int)privFile.length()];
bis.read(privateKeyContentsAsByteArray);
bis.close();
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
KeySpec ks = new PKCS8EncodedKeySpec(privateKeyContentsAsByteArray);
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(ks);
System.out.println("PRIVATE KEY:::: " + new String(privKey.getEncoded()).equals(new String(privateKeyContentsAsByteArray)));
this.Dcipher.init(Cipher.DECRYPT_MODE, privKey);
} catch(Exception e) {
System.out.println("Exception" + e);
}
}
public byte[] en(byte[] decryptedMessage) throws Exception {
byte[] encryptedMessage = this.Ecipher.doFinal(decryptedMessage);
//byte[] encryptedMessage = this.Ecipher.doFinal(decryptedMessage);
return (encryptedMessage);
}
public byte[] de(byte[] encryptedMessage) throws Exception {
byte[] decryptedMessage = this.Dcipher.doFinal(encryptedMessage);
return (decryptedMessage);
}
public static void main(String args[]) throws Exception{
TLSDecryptTest t = new TLSDecryptTest(null,null);
String s = ("Testing decryption.1Testing decryption.2Testing decryption.3Testing decryption.4");
System.out.println("S: " + s);
byte[] todo = s.getBytes();
byte[] e = t.en(todo);
String es = new String(e);
System.out.println("E: " + es);
byte[] d = t.de(e);
String ds = new String(d);
System.out.println("D: " + ds);
}
}
which works fine. However, if I sniff a few packets off the wire and then try to decrypt it, I get errors. I even tried only decrypting the first 256 bytes of it, seeing as that's the limitation of my RSA key, but it still throws errors. Most notably, a BadPaddingException at the doFinal() line.
Any ideas?
Thanks in advance.
If you are talking about SSL-protected session, then man-in-the-middle attack is possible if you have a legitimate server's private key (and can obtain the certificate which is public anyway). For practical purpose you should be able to use Wireshark to spy on your traffic.
But you can't decrypt the traffic as is. Partially because it's not encrypted using public key cryptography - data is encrypted using symmetric key generated per session.
Wireshark will allow you to decrypt if you have the server's private key. Docs are here.
First, go to Edit/Preferences/Protocols/SSL, click the Edit button next to RSA Keys:
Next, click New. Fill out the form with information that describes when the key should be used. This should be the IP address and port of the server:
Your key file may or may not require a passphrase. Hit OK three times. Capture as usual.
No. With public key encryption, you can only ever decrypt with the opposite key. e.g.
encrypted with private key => decrypt with public key
encryptd with public key => decrypt with private key
consider the chaos that would happen if
encrypted with public key => decrypt with public key
were possible - since the public key is floating around "in the open" for everyone to see, you'd essentially be giftwrapping your data in saran wrap, because everyone would have the key to decrypt it already. This would completely torpedo the entire SSL security model.