how to encrypt and decrypt image in matlab using aes ppt
#1
Rainbow 

hi. My name is jeevan. I would like to get details on how to encrypt and decrypt image in Matlab using AES algorithm. kindly help me.
Reply
#2

package com.java.blowfish;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

/**
*
* @author dhanoopbhaskar
*/
public class EncryptFile {

KeyGenerator keyGenerator = null;
SecretKey secretKey = null;
Cipher cipher = null;

public EncryptFile() {
try {
/**
* Create a Blowfish key
*/
keyGenerator = KeyGenerator.getInstance("Blowfish");
secretKey = keyGenerator.generateKey();

/**
* Create an instance of cipher mentioning the name of algorithm
* - Blowfish
*/
cipher = Cipher.getInstance("Blowfish");
} catch (NoSuchPaddingException ex) {
System.out.println(ex);
} catch (NoSuchAlgorithmException ex) {
System.out.println(ex);
}
}

public static void main(String[] args) {
String fileToEncrypt = "fileToEncrypt.jpg";
String encryptedFile = "encryptedFile.jpg";
String decryptedFile = "decryptedFile.jpg";
String directoryPath = "C:/Users/dhanoopbhaskar/Desktop/blowfish/";
EncryptFile encryptFile = new EncryptFile();
System.out.println("Starting Encryption...");
encryptFile.encrypt(directoryPath + fileToEncrypt,
directoryPath + encryptedFile);
System.out.println("Encryption completed...");
System.out.println("Starting Decryption...");
encryptFile.decrypt(directoryPath + encryptedFile,
directoryPath + decryptedFile);
System.out.println("Decryption completed...");
}

/**
*
* @param srcPath
* @param destPath
*
* Encrypts the file in srcPath and creates a file in destPath
*/
private void encrypt(String srcPath, String destPath) {
File rawFile = new File(srcPath);
File encryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
try {
/**
* Initialize the cipher for encryption
*/
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
/**
* Initialize input and output streams
*/
inStream = new FileInputStream(rawFile);
outStream = new FileOutputStream(encryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (IllegalBlockSizeException ex) {
System.out.println(ex);
} catch (BadPaddingException ex) {
System.out.println(ex);
} catch (InvalidKeyException ex) {
System.out.println(ex);
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
}

/**
*
* @param srcPath
* @param destPath
*
* Decrypts the file in srcPath and creates a file in destPath
*/
private void decrypt(String srcPath, String destPath) {
File encryptedFile = new File(srcPath);
File decryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
try {
/**
* Initialize the cipher for decryption
*/
cipher.init(Cipher.DECRYPT_MODE, secretKey);
/**
* Initialize input and output streams
*/
inStream = new FileInputStream(encryptedFile);
outStream = new FileOutputStream(decryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (IllegalBlockSizeException ex) {
System.out.println(ex);
} catch (BadPaddingException ex) {
System.out.println(ex);
} catch (InvalidKeyException ex) {
System.out.println(ex);
} catch (FileNotFoundException ex) {
System.out.println(ex);
} catch (IOException ex) {
System.out.println(ex);
}
}
}
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: blowfish, rsa algorithm encrypt the text and decrypt the same, java cryptography encrypt picture for pixels, aes advantages and disadvantages, encrypt pdf content in php, manfaat algoritma blowfish, image encryption and decryption using aes and matlab,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  well-spring some homemade barbecue backchat and moistureless rubs and the actuality 0 1,025 10-09-2019, 05:48 PM
Last Post:
  agent some homemade barbecue cheek and prosaic rubs and suit 0 942 10-09-2019, 07:04 AM
Last Post:
  pass some homemade barbecue coolness and arid rubs and module 0 898 09-09-2019, 06:35 PM
Last Post:
  matlab code 1 3,436 31-01-2019, 02:52 PM
Last Post: [email protected]
  underwater optical communication matlab code 0 3,289 02-11-2018, 07:32 PM
Last Post: Guest
  slide share ppt of artifical hand using embedded system 0 1,286 24-10-2018, 02:26 PM
Last Post: Guest
  source code for blood group detection in matlab 0 6,361 22-10-2018, 10:59 AM
Last Post: Guest
  hackchina matlab code 0 620 27-09-2018, 10:45 PM
Last Post: Guest
  heart disease prediction system source code for matlab 0 763 27-09-2018, 04:40 PM
Last Post: Guest
  matlab code for echo hiding 1 779 17-08-2018, 07:35 PM
Last Post: Guest

Forum Jump: