我是java的新手。我已经有一个用于图像加密的AES算法代码。我的问题是我只想为我的加密图像添加密钥,只有我给密钥的人才能解密图像示例,如对称加密,md5,pgp或其他任何你应该建议我用作密钥的东西。这是我的代码
package crypto;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
/**
*
* @author fIZI/Z47
*/
public class ImageCrypto extends javax.swing.JFrame {
/**
* Creates new form ImageCrypto
*/
public ImageCrypto() {
initComponents();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
file_path.setText(f.getAbsolutePath());
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
FileInputStream file = new FileInputStream(file_path.getText());
FileOutputStream outStream = new FileOutputStream("C:\\Users\\Z47\\Desktop\\Encrypt Image.jpg");
byte k[]="FiZi1701NuLl5252".getBytes();
SecretKeySpec key = new SecretKeySpec(k, "AES");
Cipher enc = Cipher.getInstance("AES");
enc.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(outStream, enc);
byte[] buf = new byte[1024];
int read;
while((read=file.read(buf))!=-1){
cos.write(buf,0,read);
}
file.close();
outStream.flush();
cos.close();
JOptionPane.showMessageDialog(null, "The image was encrypted successfully !");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
FileInputStream file = new FileInputStream(file_path.getText());
FileOutputStream outStream = new FileOutputStream("C:\\Users\\Z47\\Desktop\\Decrypt Image.jpg");
byte k[]="FiZi1701NuLl5252".getBytes();
SecretKeySpec key = new SecretKeySpec(k, "AES");
Cipher enc = Cipher.getInstance("AES");
enc.init(Cipher.DECRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(outStream, enc);
byte[] buf = new byte[1024];
int read;
while((read=file.read(buf))!=-1){
cos.write(buf,0,read);
}
file.close();
outStream.flush();
cos.close();
JOptionPane.showMessageDialog(null, "Success! your image was decrypted. Click OK to preview the image.");
Runtime.getRuntime().exec("rundll32 url.dll, FileProtocolHandler "+"C:\\Users\\Z47\\Desktop\\Decrypt Image.jpg");
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
private void file_pathActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ImageCrypto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ImageCrypto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ImageCrypto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ImageCrypto.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ImageCrypto().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JTextField file_path;
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JPanel jPanel2;
// End of variables declaration
}
顺便说一下,我在JFrame中使用netbeans并运行GUI。任何人都可以显示密钥的完整代码吗?与我的编码相结合。
答案 0 :(得分:0)
我只是想为我的加密图片添加密钥而只添加我的人 给密钥只能解密图像
在您的代码中,您的密钥为byte[] k
。
如果您想将其与加密图像一起安全地发送给收件人,您可能需要考虑使用非对称密钥加密算法来加密您的密钥。
实际上,您将使用收件人的公钥来加密AES算法中使用的密钥。只有他们才能解密该密钥才能解密消息。