- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一段使用 sun.misc.* 包使用 AES 算法加密和解密的代码。
后来我才知道使用这些包是错误的,这让我遵循了使用 Apache 的 Commons Codec 的建议,这是有效的。
之前的代码如下:
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;
}
}
按照建议,我删除了 sun.misc 并进行了以下更改。
在 Apache 的通用编解码器中用 Base64 替换 BASE64Encoder 类后:
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());
byte[] encryptedValue = new Base64().encode(encVal);
return new String(encryptedValue);
}
我找不到合适的解密替代品,因为我被击中了:
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
我没有找到任何方法来完成 decodeBuffer(String encryptedData) 的工作并返回 decodedValue 的字节数组。
最佳答案
好的,那这个呢?
使用 apache Base64 util 类类似于 sun.misc.Base64Encoder。 Apache Base64 类提供了许多重载方法,可以减轻字节和字符串之间的转换。
byte[] encodedBytes = Base64.encodeBase64(testString.getBytes());
String decodedString = new String(Base64.decodeBase64(encodedBytes));
关于java - Commons Codec 中 decodeBuffer 方法的合适替代品,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13599267/
我正在尝试使用 user guide 中的抓取示例运行 geb用于引入依赖项: $ cat my.groovy @Grapes([ @Grab("org.gebish:geb-core:0.9
我阅读了很多关于 opus-codec 的内容,但我不明白如何在我的示例 Java 应用程序中使用它。 是否有任何可用于 opus 的 .so 文件可以使用?如果没有,那么如何? 最佳答案 目前(在撰
我试图构建 Maven 项目, 每当我在命令行上运行“mvn clean install”时,都会出现以下错误: 无法解析项目 com.my_project:jar:0.0.1-SNAPSHOT 的依
我有一个项目需要 Lucene(4.3.0) 并添加以下依赖项:lucene-core,lucene-analyzers-common,lucene-queries,lucene-queryparse
我正在对 Controller 进行单元测试,目前我被服务(由 Controller 调用)中的“encodeAsJSON()”方法调用所困扰。 我得到了 MissingMethodException
无法弄清楚是什么原因导致 ' 名称为“Lucene42”的 org.apache.lucene.codecs.Codec 类型的 SPI 类不存在。您需要将支持此 SPI 的相应 JAR 文件添加到您
我想运行以下命令来使用 MongoDB Java 驱动程序创建用户, client = new MongoClient(mongoClientURI); MongoDatabase d
对于 lucene-core-5.5.2,我在 weblogic 服务器中遇到了问题 a。独立的搜索应用程序可以工作,但是当我部署为 WEB APP 时,它失败并出现以下错误 Exception ty
我的代码: DateTime dateTime = new DateTime(); BasicDBObject oldDoc = new BasicDBObject("email",email); B
我正在尝试在 Hibernate-ogm 中尝试 GridFS。这就是我的课 import org.hibernate.ogm.datastore.mongodb.type.GridFS; @Embe
我正在使用如下聚合: final List aggregations = new ArrayList<>(); Polygon polygon = new Polygon(new Po
我正在处理一个多模块 gradle 项目(12 个模块)。我继承了该项目,需要更新其中使用的一些库的版本。 我无法理解此错误的原因: ... 67 more Caused by: java.l
我正在使用 Java 学习 MongoDB。我正在尝试使用 Java 驱动程序将数据插入 MongoDB。我正在像 MongoDB 教程中一样进行插入,而且一切都很好。但是如果我想插入一个变量,当我运
我正在尝试打开并读取包含大量文本的 .txt 文件。下面是我的代码,我不知道如何解决这个问题。任何帮助将不胜感激。 file = input("Please enter a .txt file: ")
我使用 Arch Linux 和默认的 Python 3。我使用 Konsole 通过命令 pip install django-toolbelt 下载 django-toolbelt。名称: pip
我正在尝试使用 LibAV 解码 mpeg 视频文件。有两个术语我无法正确理解,镜框 和 数据包 . 按照我目前的理解,镜框 是未压缩的视频帧和 数据包是压缩帧。 问题 : 数据包有多个帧,对吗? 一
我正在查看计算机断层扫描 (CT) DICOM 图像。这些最初是未压缩的 DICOM 图像。我有这些 DICOM 图像的无损 J2K 压缩形式:传输语法 = 1.2.840.10008.1.2.4.9
如何安装通用编解码器?我已经下载了,但是我在网上搜索过,找不到这个问题的答案。我想使用 Base64 编码器和解码器。 还有 1 个问题,如果我的代码使用这个编解码器,其他尝试使用我的程序的用户是否也
本文整理了Java中loci.formats.codec.ZlibCodec类的一些代码示例,展示了ZlibCodec类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Ma
本文整理了Java中hivemall.utils.codec.ZigZagLEB128Codec类的一些代码示例,展示了ZigZagLEB128Codec类的具体用法。这些代码示例主要来源于Githu
我是一名优秀的程序员,十分优秀!