gpt4 book ai didi

java - Java 中的密码学

转载 作者:搜寻专家 更新时间:2023-10-31 08:17:38 24 4
gpt4 key购买 nike

我正在制作一个加密某些文件的应用程序。我想使用 gnu 的 cryptix 库。它说它自 2005 年以来不再开发,但我想它拥有我需要的一切......我应该使用其他东西吗?

我有一个关于加密单个文件的问题。现在我用这样的循环来做:

for(int i=0; i+block_size < bdata.length; i += block_size)
cipher.encryptBlock(bdata, i, cdata, i);

所以我的问题是如何加密可能与 block_size 大小不同的最后一个 block 。我在想也许应该向最后一个 block 添加一些额外的数据,但我不知道如何解密......

最佳答案

我强烈建议使用 AES 加密,它也随 JAVA SDK 一起提供。看看:Using AES with Java Technology这会给你一些很好的例子。要阅读有关 AES 的更多信息,请参阅:Advanced Encryption Standard - Wikipedia .

切勿使用您自己的加密方案或旧形式的加密方案。 AES 已经过在该领域知识远比我们丰富的人的尝试和测试,所以你知道它会起作用。与您自己的或旧的加密方案一样,我们可能会错过一个致命的漏洞,这将使我们的数据容易受到攻击。

在此处查看此问题以了解加密方案的差异:Comparison of DES, Triple DES, AES, blowfish encryption for data

附录:

Java 中的 AES 可以完美地用于 192 位和 256 位 key ,但您必须安装更新的 JCE 策略文件。参见 herehere .您还应该将这些文件放在您的 JDK 中,否则从您的 IDE 执行时它不会工作。

注意:确保下载正确的 JCE 策略文件,具体取决于您的 Java 版本,即 1.4、1.5、1.6 或 7。

但是,如果您使用 128 位 key ,则无需安装较新的 JCE 文件。

这是在 java 中使用 CBC/AES/PKCS5Padding 和使用 RandomSecure 的随机 IV 的一些安全 AES 用法的模板。

注意您需要 key 和 IV 才能解密:

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
* This program generates a AES key, retrieves its raw bytes, and then
* reinstantiates a AES key from the key bytes. The reinstantiated key is used
* to initialize a AES cipher for encryption and decryption.
*/
public class AES {

/**
* Encrypt a sample message using AES in CBC mode with a random IV genrated
* using SecyreRandom.
*
*/
public static void main(String[] args) {
try {
String message = "This string contains a secret message.";
System.out.println("Plaintext: " + message + "\n");

// generate a key
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128); // To use 256 bit keys, you need the "unlimited strength" encryption policy files from Sun.
byte[] key = keygen.generateKey().getEncoded();
SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");

// build the initialization vector (randomly).
SecureRandom random = new SecureRandom();
byte iv[] = new byte[16];//generate random 16 byte IV AES is always 16bytes
random.nextBytes(iv);
IvParameterSpec ivspec = new IvParameterSpec(iv);

// initialize the cipher for encrypt mode
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivspec);

System.out.println("Key: " + new String(key, "utf-8") + " This is important when decrypting");
System.out.println("IV: " + new String(iv, "utf-8") + " This is important when decrypting");
System.out.println();

// encrypt the message
byte[] encrypted = cipher.doFinal(message.getBytes());
System.out.println("Ciphertext: " + asHex(encrypted) + "\n");

// reinitialize the cipher for decryption
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);

// decrypt the message
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Plaintext: " + new String(decrypted) + "\n");
} catch (IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException | InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
}

/**
* Turns array of bytes into string
*
* @param buf Array of bytes to convert to hex string
* @return Generated hex string
*/
public static String asHex(byte buf[]) {
StringBuilder strbuf = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}
}

关于java - Java 中的密码学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11707976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com