【问题标题】:How to put the key for image encryption using AES Algorithm如何使用 AES 算法放置图像加密的密钥
【发布时间】:2016-07-12 14:33:25
【问题描述】:

我是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。任何人都可以显示密钥的完整代码吗?通过结合我的编码。

【问题讨论】:

  • AES 是一种对称密码。由于您也使用 [public-key-encryption] 对此进行了标记,因此我假设您知道其中的区别。我想,你现在有答案了。有很多例子。这些都不符合您的需求吗?
  • 我有。公钥源码,但是不知道怎么和我的图片加密编码结合起来

标签: java encryption public-key-encryption encryption-symmetric pgp


【解决方案1】:

我只想为我的加密图像添加密钥,并且只添加我自己的人 给出密钥只能解密图像

在您的代码中,您的密钥是byte[] k

如果您想将其与加密图像一起安全地发送给收件人,您可能需要考虑使用非对称密钥加密算法来加密您的密钥。

实际上,您会使用收件人的公钥来加密 AES 算法中使用的密钥。只有他们才能解密该密钥才能解密消息。

【讨论】:

  • 对不起,先生,我没听懂您说的话。更详细的,你能显示一个代码吗?
  • 恐怕你的问题不是很清楚。您说您想“为我的加密图像添加密钥” - 我只是指出 byte[] k 是您的密钥。如果您想将其发送给某人,那么您可能会考虑使用 PGP。使用收件人的私钥加密您的 AES 密钥。
  • 嗯,谢谢你刚才的解释。我现在完全明白我需要什么来编辑代码。如果您有 PGP 源代码的链接,那就太好了。顺便感谢您的快速响应,非常感谢您的帮助。 :D
  • 没问题@JohnU2。您可能想看看 Bouncy Castle - bouncycastle.org
猜你喜欢
  • 1970-01-01
  • 2015-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
  • 1970-01-01
  • 2020-06-24
相关资源
最近更新 更多