Hi Guys,
I have encrypted images saved in Oracle database, and through web interface developed using Java I can download the images.
I have a decryption key and I would like to do the same thing direct from oracle database but using uipath.
===============================
Below is the table used.
===============================
The java code that is currently working.
import javax.servlet.ServletContext;
import com.sun.media.jai.codec.ImageEncoder;
import javax.servlet.ServletOutputStream;
import java.awt.image.RenderedImage;
import java.io.InputStream;
import tz.co.infowise.registration.model.document.DocumentVO;
import java.io.IOException;
import com.sun.media.jai.codec.ImageEncodeParam;
import java.io.OutputStream;
import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.JPEGEncodeParam;
import javax.media.jai.JAI;
import com.sun.media.jai.codec.SeekableStream;
import java.io.ByteArrayInputStream;
import tz.co.infowise.utils.encrypt.AESEncryptor;
import tz.co.infowise.registration.model.utils.ConfigurationWrapper;
import tz.co.infowise.registration.ServiceLocator;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServlet;
public class DisplayImageServlet extends HttpServlet
{
protected void service(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
try {
final String docId = request.getParameter("d");
final String action = request.getParameter("ac");
final Long imageId = Long.parseLong(docId);
String fileName = null;
byte[] df = null;
final byte[] decryptedData = ServiceLocator.instance().getDocumentService().loadImage(imageId);
try {
AESEncryptor.setKey(ConfigurationWrapper.getKeyString(), ConfigurationWrapper.getKeyIteration());
df = AESEncryptor.decrypt(decryptedData);
}
catch (Exception e) {
e.printStackTrace();
}
final DocumentVO vo = ServiceLocator.instance().getDocumentService().load(imageId);
fileName = vo.getMsisdn() + "." + vo.getFormat().toLowerCase();
if ("Preview".equals(action)) {
if (fileName.contains(".tif")) {
response.setContentType("image/jpg");
final InputStream ins = new ByteArrayInputStream(df);
final SeekableStream s = SeekableStream.wrapInputStream(ins, true);
final RenderedImage objImage = (RenderedImage)JAI.create("stream", (Object)s);
final JPEGEncodeParam encodeParam = new JPEGEncodeParam();
final ServletOutputStream out = response.getOutputStream();
final ImageEncoder encoderImage = ImageCodec.createImageEncoder("JPEG", (OutputStream)out, (ImageEncodeParam)encodeParam);
encoderImage.encode(objImage);
}
else {
final ServletContext context = this.getServletContext();
String mimeType = context.getMimeType(fileName);
if (mimeType == null) {
mimeType = "application/octet-stream";
}
response.setContentType(mimeType);
if (df != null) {
response.setContentLength(df.length);
response.getOutputStream().write(df);
}
}
}
else {
final String headerKey = "Content-Disposition";
final String headerValue = String.format("attachment; filename=\"%s\"", fileName);
response.setHeader(headerKey, headerValue);
final OutputStream outStream = (OutputStream)response.getOutputStream();
final byte[] buffer = new byte[4096];
int bytesRead = -1;
final InputStream inStream = new ByteArrayInputStream(df);
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
}
}
catch (IOException ex) {
ex.printStackTrace();
throw ex;
}
catch (Exception e2) {
e2.printStackTrace();
}
}
}