java - GAE 中的 AES-256 实现

标签 java google-app-engine cryptography aes

256 但它似乎在 GAE 中不起作用。我已经下载了“Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files”,并且 local_policy.jar、US_export_policy.jar 存在于 C:\Program Files\Java\jdk1.6.0_29\jre\lib\security 位置。

这是代码:

import java.security.spec.KeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.*;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;

public class AESEncrypter {

private static final byte[] SALT = {
    (byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
    (byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
};
private static final int ITERATION_COUNT = 65536;
private static final int KEY_LENGTH = 256;
private Cipher ecipher;
private Cipher dcipher;

AESEncrypter(String passPhrase) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec spec = new PBEKeySpec(passPhrase.toCharArray(), SALT, ITERATION_COUNT, KEY_LENGTH);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

    ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    ecipher.init(Cipher.ENCRYPT_MODE, secret);

    dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    byte[] iv = ecipher.getParameters().getParameterSpec(IvParameterSpec.class).getIV();
    dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
}

public String encrypt(String encrypt) throws Exception {
    byte[] bytes = encrypt.getBytes("UTF8");
    byte[] encrypted = encrypt(bytes);
    return new Base64().encodeBase64String(encrypted);
}

public byte[] encrypt(byte[] plain) throws Exception {
    return ecipher.doFinal(plain);
}

public String decrypt(String encrypt) throws Exception {
    byte[] bytes = new Base64().decodeBase64(encrypt);
    byte[] decrypted = decrypt(bytes);
    return new String(decrypted, "UTF8");
}

public byte[] decrypt(byte[] encrypt) throws Exception {
    return dcipher.doFinal(encrypt);
}

public static void main(String[] args) throws Exception {

    String message = "MESSAGE";
    String password = "PASSWORD";

    AESEncrypter encrypter = new AESEncrypter(password);
    String encrypted = encrypter.encrypt(message);
    String decrypted = encrypter.decrypt(encrypted);

    System.out.println("Encrypt(\"" + message + "\", \"" + password + "\") = \"" + encrypted + "\"");
    System.out.println("Decrypt(\"" + encrypted + "\", \"" + password + "\") = \"" + decrypted + "\"");
}

}

如有任何帮助,我们将不胜感激 谢谢!!

最佳答案

您的异常肯定是由于缺少无限强度管辖文件引起的。

您已声明这些已下载并存储在您的 Java 6 JDK 中。我确保您也将副本存储在 JRE 安装的 lib/security 文件夹中(如果您有的话)。

确保您确切知道执行应用程序时正在调用哪个 java 命令。

关于java - GAE 中的 AES-256 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12833826/

相关文章:

java - 显式调用 SpringBoot 提供的默认 Spring 缓存管理器

java - 如何使用服务帐户从 App Engine 访问 Google 电子表格?

android - GAE 从数据存储中读取

C# BouncycaSTLe Blowfish CTR解密

perl - Filter::Crypto 模块对 DATA 部分做了什么?

java - Apache POI 是否支持对单词进行两次签名?

java - 通过 UDP 从 Android 向外部传感器发送数据

javascript - 无法使用 crypto.createDecipheriv() 正确解密文件。文件大小为 0 字节

java - Spring 上下文不使用 @ContextConfiguration 初始化,而是使用 new ClassPathXmlApplicationContext 初始化

python - 我还应该使用 App Engine 的 Users Python API 吗?