Java中的简单AES加密器

时间:2016-04-19 14:46:01

标签: java encryption aes

这是我在Java中的第一个项目,我决定使用AES制作一个简单的文本加密器。

我得到的错误是:Cipher类型中的方法init(int, Key)不适用于参数(int, byte[])

代码:

import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.Key;

import javax.crypto.*;
import java.util.*;

public class Encryptor {

    public static void main(String[] args) throws Exception {
        String FileName = "encryptedtext.txt";
        String FileName2 = "decryptedtext.txt";

        Scanner input = new Scanner(System.in);

       System.out.println("Enter your 16 character key here:");
       String EncryptionKey = input.next();



        KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
        KeyGen.init(128);

        Cipher AesCipher = Cipher.getInstance("AES");

        System.out.println("Enter text to encrypt or decrypt:");
        String Text = input.next();

        System.out.println("Do you want to encrypt or decrypt (e/d)");
        String answer = input.next();
        if (answer.equalsIgnoreCase("e")){

            byte[] byteKey = (EncryptionKey.getBytes());
            byte[] byteText = (Text).getBytes();

            AesCipher.init(Cipher.ENCRYPT_MODE, byteKey); // ERROR LINE
            byte[] byteCipherText = AesCipher.doFinal(byteText);
            Files.write(Paths.get(FileName), byteCipherText);


              }
        else if (answer.equalsIgnoreCase("d")){

            byte[] byteKey = (EncryptionKey.getBytes());
            byte[] byteText = (Text).getBytes();
            byte[] cipherText = Files.readAllBytes(Paths.get(FileName));

            AesCipher.init(Cipher.DECRYPT_MODE, byteKey); // ERROR LINE
            byte[] bytePlainText = AesCipher.doFinal(cipherText);
            Files.write(Paths.get(FileName2), bytePlainText);
        }
}

}

提前致谢! :)

3 个答案:

答案 0 :(得分:1)

你不应该直接将字节数组传递给Cipher对象,而是需要创建SecretKeySpecs的对象。

这是完整的代码

  import java.nio.file.Files;
  import java.nio.file.Paths;
  import javax.crypto.*;
  import javax.crypto.spec.IvParameterSpec;
  import javax.crypto.spec.SecretKeySpec;

  import java.util.*;

  public class Encrypter {

  public static void main(String[] args) throws Exception {
  String FileName = "encryptedtext.txt";
  String FileName2 = "decryptedtext.txt";

  Scanner input = new Scanner(System.in);

  System.out.println("Enter your 16 character key here:");
  String EncryptionKey = input.next();
  byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  IvParameterSpec ivspec = new IvParameterSpec(iv);


  KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
  KeyGen.init(128);


  Cipher AesCipher = Cipher.getInstance("AES/CFB/NoPadding");
  System.out.println("Enter text to encrypt or decrypt:");
  String Text = input.next();

  System.out.println("Do you want to encrypt or decrypt (e/d)");
  String answer = input.next();
  if (answer.equalsIgnoreCase("e")) {

   byte[] byteKey = (EncryptionKey.getBytes());
   byte[] byteText = (Text).getBytes();
   SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
   AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec);
   AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivspec); // ERROR LINE
   byte[] byteCipherText = AesCipher.doFinal(byteText);
   Files.write(Paths.get(FileName), byteCipherText);


  } else if (answer.equalsIgnoreCase("d")) {

   byte[] byteKey = (EncryptionKey.getBytes());
   byte[] byteText = (Text).getBytes();
   byte[] cipherText = Files.readAllBytes(Paths.get(FileName));

   SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
   AesCipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivspec); // ERROR LINE
   byte[] bytePlainText = AesCipher.doFinal(cipherText);
   Files.write(Paths.get(FileName2), bytePlainText);
  }
 }

}

答案 1 :(得分:1)

这是完整代码

完整代码:

import java.nio.file.Files;
import java.nio.file.Paths;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.util.*;

public class Encrypter {

public static void main(String[] args) throws Exception {
    String FileName = "encryptedtext.txt";
    String FileName2 = "decryptedtext.txt";

    Scanner input = new Scanner(System.in);

   System.out.println("Enter your 16 character key here:");
   String EncryptionKey = input.next();
   byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
   IvParameterSpec ivspec = new IvParameterSpec(iv);


    KeyGenerator KeyGen = KeyGenerator.getInstance("AES");
    KeyGen.init(128);


    Cipher AesCipher = Cipher.getInstance("AES/CFB/NoPadding");
    System.out.println("Enter text to encrypt or decrypt:");
    String Text = input.next();

    System.out.println("Do you want to encrypt or decrypt (e/d)");
    String answer = input.next();
    if (answer.equalsIgnoreCase("e")){

        byte[] byteKey = (EncryptionKey.getBytes());
        byte[] byteText = (Text).getBytes();
        SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
        AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec,ivspec );
        AesCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec,ivspec); // ERROR LINE
        byte[] byteCipherText = AesCipher.doFinal(byteText);
        Files.write(Paths.get(FileName), byteCipherText);


          }
    else if (answer.equalsIgnoreCase("d")){

        byte[] byteKey = (EncryptionKey.getBytes());
        byte[] byteText = (Text).getBytes();
        byte[] cipherText = Files.readAllBytes(Paths.get(FileName));

        SecretKeySpec secretKeySpec = new SecretKeySpec(byteKey, "AES");
        AesCipher.init(Cipher.DECRYPT_MODE, secretKeySpec,ivspec); // ERROR LINE
        byte[] bytePlainText = AesCipher.doFinal(cipherText);
        Files.write(Paths.get(FileName2), bytePlainText);
    }
}

}

答案 2 :(得分:0)

我必须加密在外部链接中公开的URL,并且我已经创建了一个在AES上加密的类。也许这可以帮助别人。 我添加了一个方法来创建我的随机初始向量,它也在这个类上。

package br.com.tokiomarine.captcha.encryption;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.text.CharacterPredicates;
import org.apache.commons.text.RandomStringGenerator;

public class Encryptor 
{
    public static String encrypt(String key, String initVector, String value) throws Exception
    {
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

        byte[] encrypted = cipher.doFinal(value.getBytes());

        return Base64.encodeBase64URLSafeString(encrypted);
    }

    public static String decrypt(String key, String initVector, String encrypted) throws Exception 
    {
        IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));

        return new String(original);
    }

    public static String generateInitVector() {
    RandomStringGenerator randomStringGenerator =
        new RandomStringGenerator.Builder()
            .withinRange('0', 'z')
            .filteredBy(CharacterPredicates.LETTERS, CharacterPredicates.DIGITS)
            .build();
    return randomStringGenerator.generate(16);
    }

}

这是我的测试类:

public class EcryptionTest {

    @Test
    public void encryptionTest() {
        try 
        {
            String key = "Key@TokioMarineC";
            for(int i = 0; i < 1000; i++) 
            {
                String initVector = Encryptor.generateInitVector();
                String encrypted = Encryptor.encrypt(key, initVector, "StringTeste");
                assertTrue("StringTeste".equals(Encryptor.decrypt(key, initVector, encrypted)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}