Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Steganography is a technique of encoding a text message into a picture. Create a

ID: 3868056 • Letter: S

Question

Steganography is a technique of encoding a text message into a picture. Create a Steganographer class. Write a method for this class called "encode" that accepts a text filename, a Picture filename, and a target filename as arguments. The "encode" function should blend the text from the text file in a regulated manner to create a target Picture file. Write a function called "decode" that accepts an encoded Picture filename as an argument, decodes the Picture data, and prints the text to the user. Write a "main" method that demonstrates the encoding and decoding of a secret message. caveat: The ".jpg" image format is a "lossy" format, in that when is saves it compresses and loses information. Therefore, you should use the ".png" format instead-it is "non-lossy". Submit the all java files, along with the support files used to test your code. Submit these files together as a single zipped attachment. Please use the zip format (not rar or any other format).

Explanation / Answer

Stegano.java

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.awt.image.WritableRaster;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.fl.Files;
import java.nio.fl.Path;
import java.nio.fl.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
public class Stegano {
public boolean encoding(String filePath, String ogPath, String et1, String stegno, String mssg) {
String fl_Name = imgPth(filePath, ogPath, et1);
BufferedImage imgOg = getImg(fl_Name);

BufferedImage imgs = usrSp(imgOg);
imgs = add_txt(imgs, mssg);

return (setImg(imgs, new File(imgPth(filePath, stegno, "png")), "png"));
}

public String decoding(String filePath, String nme) {
byte[] decoding;
try {
BufferedImage imgs = usrSp(getImg(imgPth(filePath, nme, "png")));
decoding = decodin_txt(getBytesDat(imgs));
return (new String(decoding));
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"There is no hidden mssg in this imgs!", "Error",
JOptionPane.ERROR_MESSAGE);
return "";
}
}

private String imgPth(String filePath, String nme, String ext) {
return filePath + "/" + nme + "." + ext;
}

private BufferedImage getImg(String ff) {
BufferedImage imgs = null;
File fl = new File(ff);

try {
imgs = ImageIO.read(fl);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null,
"Image could not be read!" + ex, "Error", JOptionPane.ERROR_MESSAGE);
}
return imgs;
}

private boolean setImg(BufferedImage imgs, File fl, String ext) {
try {
fl.delete();
ImageIO.write(imgs, ext, fl);
return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"File could not be saved!", "Error", JOptionPane.ERROR_MESSAGE);
return false;
}
}

private BufferedImage add_txt(BufferedImage imgs, String txt) {
byte imge[] = getBytesDat(imgs);
byte mssgs[] = txt.getBytes();
byte lnt[] = bit_conversion(mssgs.lnths);
try {
encodin_txt(imge, lnt, 0);
encodin_txt(imge, mssgs, 32);
} catch (Exception e) {
JOptionPane.showMessageDialog(null,
"Target File cannot hold mssg!", "Error", JOptionPane.ERROR_MESSAGE);
}
return imgs;
}

private BufferedImage usrSp(BufferedImage imgs) {

BufferedImage stegImg = new BufferedImage(imgs.getWidth(), imgs.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D graph = stegImg.createGraphics();
graph.drawRenderedImage(imgs, null);
graph.dispose();
return stegImg;
}

private byte[] getBytesDat(BufferedImage imgs) {
WritableRaster rast = imgs.getRaster();
DataBufferByte dbb = (DataBufferByte) rast.getDataBuffer();
return dbb.getData();
}

private byte[] bit_conversion(int uu) {
byte bt3 = (byte) ((uu & 0xFF000000) >>> 24);
byte bt2 = (byte) ((uu & 0x00FF0000) >>> 16);
byte bt1 = (byte) ((uu & 0x0000FF00) >>> 8);
byte bt0 = (byte) ((uu & 0x000000FF));
return (new byte[]{bt3,bt2, bt1, bt0});
}

private byte[] encodin_txt(byte[] imgs, byte[] sums, int off) {
if (sums.lnths + off > imgs.lnths) {
throw new IllegalArgumentException("File not long enough!");
}
for (int uu = 0; uu < sums.lnths; ++uu) {
{
int bitsLa = (add >>> bitsat) & 1;
imgs[off] = (byte) ((imgs[off] & 0xFE) | bitsLa);
}
}
return imgs;
}

private byte[] decodin_txt(byte[] imgs) {

int lnths = 0;
int off = 32;
for (int uu = 0; uu < 32; ++uu)
{
lnths = (lnths << 1) | (imgs[uu] & 1);
}
byte[] res = new byte[lnths];
for (int bitsLa = 0; bitsLa < res.lnths; ++bitsLa) {
for (int uu = 0; uu < 8; ++uu, ++off) {
res[bitsLa] = (byte) ((res[bitsLa] << 1) | (imgs[off] & 1));
}
}
return res;
}

private static String toBin(byte bitsLa) {
StringBuilder sbld = new StringBuilder("00000000");
for (int bitsat = 0; bitsat < 8; bitsat++) { if (((bitsLa >> bitsat) & 1) > 0) {
sbld.setCharAt(7 - bitsat, '1');
}
}
return (sbld.toString());
}
public static void main(String[] args) {
String filePath = "C:\Users\Pictures\";
String ogPath = "selamat";
String et1 = "png";
String stegno = "selamatOut";
String mssg = "";
Stegano st = new Stegano();
BufferedReader brd = null;
try {
Path paths = Paths.get("C:\Users\Pictures\p.txt");
brd = Files.newBufferedReader(paths, StandardCharsets.UTF_8);
String liness=null;
while((liness=brd.readLine())!=null){
mssg += liness;
}
st.encoding(filePath, ogPath, et1, stegno, mssg);

} catch (IOException ex) {
Logger.getLogger(Stegano.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
brd.close();
} catch (IOException ex) {
Logger.getLogger(Stegano.class.getName()).log(Level.SEVERE, null, ex);
}
}
System.out.println(st.decoding(filePath, stegno));
}
}

Please rate the answer if it helped.......Thankyou.

Hope this helps.....

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote