【问题标题】:sun.misc.BASE64Encoder is not supported by Google App Engine's Java runtime environmentGoogle App Engine 的 Java 运行时环境不支持 sun.misc.BASE64Encoder
【发布时间】:2012-10-08 05:01:49
【问题描述】:

在尝试加密时,我在 GAE 中遇到了这个问题 “Google App Engine 的 Java 运行时环境不支持 sun.misc.BASE64Encoder”

任何人都可以帮助我应该使用哪个包? 我正在尝试进行 AES 加密。 这是我正在使用的代码。

import java.security.*;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;


import sun.misc.*;

public class AESencrp {
private static final String ALGO = "AES";
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };

public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encVal);
    return encryptedValue;
}

public static String decrypt(String encryptedData) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;
}



}

【问题讨论】:

    标签: java google-app-engine base64


    【解决方案1】:

    你可以试试Commons Codec。有一个类Base64提供Base64编码和解码。

    sun.* 包不是受支持的公共接口的一部分。查看此article 了解更多详情。

    【讨论】:

    • 这是我得到的--import org.apache.commons 无法解析
    • 我找不到合适的替代品 yte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);您能否为方法 decodeBuffer 推荐一些公共编解码器中的方法??
    • @bali208 你试过decodeBase64() 吗?
    • Guava 现在也有 Base64(从 v14 开始)。
    最近更新 更多