我需要实现256位AES加密,但我在网上找到的所有示例都使用"KeyGenerator"生成256位密钥,但我想使用自己的密码.如何创建自己的密钥?我已经尝试将其填充为256位,但后来我得到一个错误,说密钥太长了.我确实安装了无限管辖区补丁,所以那不是问题:)
IE浏览器.KeyGenerator看起来像这样......
// Get the KeyGenerator
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128); // 192 and 256 bits may not be available
// Generate the secret key specs.
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
Run Code Online (Sandbox Code Playgroud)
编辑
我实际上是将密码填充到256个字节,而不是位,这太长了.以下是我现在使用的一些代码,我对此有更多的经验.
byte[] key = null; // TODO
byte[] input = null; // TODO
byte[] output = null;
SecretKeySpec keySpec = null;
keySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
cipher.init(Cipher.ENCRYPT_MODE, keySpec);
output = cipher.doFinal(input)
Run Code Online (Sandbox Code Playgroud)
您需要自己做的"TODO"位:-)
我需要使用以下命令在JAVA中解密在UNIX中加密的文件:
openssl aes-256-cbc -a -salt -in password.txt -out password.txt.enc
mypass
mypass
Run Code Online (Sandbox Code Playgroud)
我必须在java中解密,就像我在UNIX中所做的那样
openssl aes-256-cbc -d -a -in password.txt.enc -out password.txt.new
mypass
Run Code Online (Sandbox Code Playgroud)
有人可以给我一个java代码来做这个吗?
我正在尝试使用 Java Cryto 在 Java 中进行简单的 AES 加密,然后可以使用 OpenSSL 在 ObjectiveC 中对其进行解密。
因为我不是在做 ObjectiveC 方面的工作,所以我想确保它可以工作,使用 openSSL 命令行,但我总是得到“坏幻数”
这是我的 Java 代码
public class EncryptionUtils {
private static final String AES_CIPHER_METHOD = "AES";
private static final int AES_KEY_SIZE = 128;
public static byte[] generateAesKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(AES_CIPHER_METHOD);
keyGenerator.init(AES_KEY_SIZE);
SecretKey key = keyGenerator.generateKey();
return key.getEncoded();
}
public static SecretKeySpec createAesKeySpec(byte[] aesKey) {
return new SecretKeySpec(aesKey, AES_CIPHER_METHOD);
}
public static void aesEncryptFile(File in, File out, SecretKeySpec aesKeySpec) throws InvalidKeyException, NoSuchAlgorithmException, …Run Code Online (Sandbox Code Playgroud) 使用以下命令对文件进行加密:
openssl enc -aes-256-cbc -in file.txt -out file_enc.txt -k 1234567812345678
Run Code Online (Sandbox Code Playgroud)
使用以下命令解密该文件:
openssl enc -d -aes-256-cbc -in file_enc.txt -out file.txt -k 1234567812345678
Run Code Online (Sandbox Code Playgroud)
在java中打印盐和密钥后我得到:
密钥=b796fbb416732ce13d39dbb60c0fb234a8f6d70e49df1c7e62e55e81d33a6bff774254ac99268856bf3afe0b95defdad
在 cmd 中我得到:
盐=2D7C7E1C84BD6693密钥=B796FBB416732CE13D39DBB60C0FB234A8F6D70E49DF1C7E62E55E81D33A6BFF
=774254AC99268856BF3AFE0B95DEFDAD
运行后:
openssl enc -aes-256-cbc -in file.txt -out file_enc.txt -pbkdf2 -k 1234567812345678 -p
我正在使用以下代码,但正在打印加密文件:
public static void main(String args[]) throws InvalidKeySpecException,
NoSuchAlgorithmException,
IllegalBlockSizeException,
InvalidKeyException,
BadPaddingException,
InvalidAlgorithmParameterException,
NoSuchPaddingException,
IOException {
String password = "1234567812345678";
String algorithm = "AES/CBC/PKCS5Padding";
IvParameterSpec ivParameterSpec = AESUtil.generateIv();
Resource resource = new ClassPathResource("file_enc.txt");
File inputFile = resource.getFile();
byte[] salt = …Run Code Online (Sandbox Code Playgroud)