java中的AES加密 [英] AES encryption in java

查看:113
本文介绍了java中的AES加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用AES算法加密和解密的程序,但我必须指定加密文件的名称,并指定原始文件的格式作为加密文件名称的一部分。我想知道如何在我的代码中实现以下功能:

I have a program that encrypts and decrypts using the AES algorithm, but I have to specify the name of the encrypted file, and also specify the format of the original file as part of the name of the encrypted file. I will like to know how how to implement the following features into my code:


  1. 我希望加密文件的名称在cipherText中

  2. 我希望计算机能够决定文件类型(扩展名为eg.txt)而无需我指定它,例如在我的代码中如果我正在加密.jpg文件,我必须指定加密文件的名称为encrypt.jpg

这是我试图实现的代码:

Here is my code which i have tried to implement:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.SecretKeySpec;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Scanner;

public class EncryptDecrypt {
    static Cipher cipher;
    static byte[] cipherText;
    static byte[] input;
    static byte k[]="2305ty6345663ty0".getBytes();
    static SecretKeySpec key = new SecretKeySpec(k, "AES");
    static int ctLength;
    static String filePath = "C:/inddexfolder/casie.jpg";
    static String encryptionPath = "C:/indexfolder1/encrypt.jpg";

        public static void main(String[] args) {
            EncryptDecrypt.encrypt();
            EncryptDecrypt.decrypt();
        }

        public static void encrypt() {
            try{
                input = filePath.getBytes();
                FileInputStream file = new FileInputStream(filePath);
                FileOutputStream outStream = new FileOutputStream(encryptionPath);

                cipher  = Cipher.getInstance("AES/ECB/PKCS5Padding", "SunJCE");

                cipher.init(Cipher.ENCRYPT_MODE, key);
                cipherText = new byte[cipher.getOutputSize(input.length)];
                ctLength = cipher.update(input, 0, input.length, cipherText, 0);
                ctLength+= cipher.doFinal(cipherText, ctLength);
                String encrypted = new String (cipherText);
                CipherOutputStream cos = new CipherOutputStream(outStream, cipher);
                byte[] buf = new byte[1024];
                int read;
                while((read=file.read(buf))!=-1){
                    cos.write(buf,0,read);
                }
                file.close();
                outStream.flush();
                cos.close();
            }
            catch(IOException e) {  
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchProviderException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            } catch (ShortBufferException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            }
        }

        public static void decrypt() {
        try {
            FileInputStream file = new FileInputStream(encryptionPath);
            FileOutputStream outStream = new FileOutputStream("casenc1.jpg");
            byte k[]="2305ty6345663ty0".getBytes();
            SecretKeySpec key = new SecretKeySpec(k, "AES");
            cipher  = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            CipherOutputStream cos = new CipherOutputStream(outStream, cipher);
            byte[] buf = new byte[1024];
            int read;
            while((read=file.read(buf))!=-1) {
                cos.write(buf,0,read);
            }
            file.close();
            outStream.flush();
            cos.close();

         Runtime.getRuntime().exec("rundll32 url.dll, FiProtocolHandler 
          "+"casenc1.jpg");

        } catch(IOException e) {
            System.out.println(" not decrypted Successfully");
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
    }
}

对于加密中的FileOutputStream方法,我试过:

For the FileOutputStream in the encrypt method , i tried:

FileOutputStream outStream
    = new FileOutputStream(encryptionPath = new String(cipherText));      

...看看我是否可以将文件名设为密文但我收到了以下错误:

…to see if I could get the file name to be in cipher text but I got these errors:

Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(Unknown Source)
at EncryptDecrypt.encrypt(EncryptDecrypt.java:48)
at EncryptDecrypt.main(EncryptDecrypt.java:37)

谢谢!

推荐答案

您的 cipherText 为空。请检查在加密之前是否尝试分配它。

Your cipherText is null. Please check whether you try assign it before encrypt it.

FileOutputStream outStream
    = new FileOutputStream(encryptionPath = new String(cipherText));   

这篇关于java中的AES加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆