如何使用此AES程序在稍后阶段进行解密

时间:2009-11-20 08:18:41

标签: java encryption aes

我真的无法解决这个问题,所以请帮助我。 这是一个使用128位AES加密现有文件keeper.txt内容的程序,将加密文本放入名为Encrypted.txt的新创建文件中,然后将Encrypted.txt内容解密为新创建的文件Decrypted.txt

每次运行此程序时,它都会为加密生成一个随机密钥。

我只想弄清楚我是否必须向某人提供Encrypted.txt文件,以及如何使用此代码或稍微修改此代码来解密该文件。

我认为不可能向他发送这个程序生成的密钥..是吗? 因为当我尝试使用system.out打印密钥时,它没有给出密钥。

请帮帮我

package org.temp2.cod1;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;

import java.security.spec.AlgorithmParameterSpec;

public class AESEncrypter
{
Cipher ecipher;
Cipher dcipher;

public AESEncrypter(SecretKey key)
{
// Create an 8-byte initialization vector
byte[] iv = new byte[]
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};

AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
try
{
ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

// CBC requires an initialization vector
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (Exception e)
{
e.printStackTrace();
}
}

// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out)
{
try
{
// Bytes written to out will be encrypted
out = new CipherOutputStream(out, ecipher);

// Read in the cleartext bytes and write to out to encrypt
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
}
catch (java.io.IOException e)
{
}
}

public void decrypt(InputStream in, OutputStream out)
{
try
{
// Bytes read from in will be decrypted
in = new CipherInputStream(in, dcipher);

// Read in the decrypted bytes and write the cleartext to out
int numRead = 0;
while ((numRead = in.read(buf)) >= 0)
{
out.write(buf, 0, numRead);
}
out.close();
}
catch (java.io.IOException e)
{
}
}

public static void main(String args[])
{
try
{
// Generate a temporary key. In practice, you would save this key.
// See also e464 Encrypting with DES Using a Pass Phrase.

KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key = kgen.generateKey();

// Create encrypter/decrypter class
AESEncrypter encrypter = new AESEncrypter(key);

// Encrypt
encrypter.encrypt(new FileInputStream("C:\\keeper.txt"),new FileOutputStream("C:\\Encrypted.txt"));
// Decrypt
encrypter.decrypt(new FileInputStream("C:\\Encrypted.txt"),new FileOutputStream("C:\\Decrypted.txt"));
}
catch (Exception e)
{
e.printStackTrace();
}
}
} 

3 个答案:

答案 0 :(得分:0)

尝试通过调用key.getEncoded()来获取密钥。这将返回一个字节数组。然后,您可以使用Base64实用程序(如apache-commons codec)将字节数组转换为String。

但请注意,从安全角度来看,向某人发送加密文件以及解密密钥是一种妥协行为。

答案 1 :(得分:0)

您可以根据密码短语修改要加密的代码。

https://stackoverflow.com/questions/372268/java-passphrase-encryption

与您的朋友商定一个交换生成密钥的密码短语可能更容易,特别是如果您只想保护机会主义者阅读您的电子邮件。

答案 2 :(得分:0)

您可以通过调用

获取密钥的编码版本
   byte[] keyBytes = secretKey.getEncoded();

对于AES,实际上没有涉及任何编码,因此您将获得原始字节(128位为16字节)。 keyBytes是二进制文件,因此您无法打印它。如果需要以文本格式发送,可以对hex或base64进行编码。另一方可以重建这样的密钥,

  SecretKey key = new SecretKeySpec(keyBytes, "AES");

我昨天回答了关于Sun的AES示例的另一个问题。这是作者感到困惑的地方。他同时做了两个步骤。

通常,如果您使用密码作为密码并通过密码生成密钥,则人们更容易记住。这称为PBE(基于密码的加密)。以下是我用来生成密钥的代码,

   public static SecretKey getAesKey(char[] password, int keyLength)
   throws GeneralSecurityException {

  int count = 128; // Iteration count
  byte[] salt;
  try {
   salt = "This is a fixed salt string".getBytes("UTF-8");
  } catch (UnsupportedEncodingException e) {
   throw new IllegalStateException("No UTF-8");
  }
  PBEKeySpec keySpec = new PBEKeySpec(password, salt, count, keyLength);
  SecretKeyFactory skf = SecretKeyFactory
    .getInstance("PBKDF2WithHmacSHA1");
  SecretKey pbeKey = skf.generateSecret(keySpec);
  byte[] raw = pbeKey.getEncoded();
  return new SecretKeySpec(raw, "AES");
 }

对您的代码的另一个建议是,如果您每次使用不同的随机IV,则会更加安全。 IV不需要保密,因此您可以将其添加到密文中。