0

我真的无法自己解决这个问题,所以请帮帮我。这是一个使用 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();
}
}
} 
4

2 回答 2

0

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

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

于 2009-11-20T08:23:29.553 回答
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 不需要保密,因此您可以将其添加到密文中。

于 2009-11-20T13:11:53.197 回答