- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
下面是我的代码。当我尝试打印密封对象时,它只显示
"javax.crypto.SealedObject@34dac684"
private void encryptUserCodes(List<UserCode> userCodes) {
try {
// generate a secret key using the DES algorithm
key = KeyGenerator.getInstance("DES").generateKey();
ecipher = Cipher.getInstance("DES");
dcipher = Cipher.getInstance("DES");
// initialize the ciphers with the given key
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
// create a sealed object
SealedObject sealed = new SealedObject((Serializable) userCodes, ecipher);
//PRINT SEALED OBJECT HERE
}
catch(Exception e){
e.printStackTrace();
}
}
最佳答案
<强>1。加密:
创建输出流并使用 Base64 编码器获取字符串。
<强>2。解密:
创建一个新的密码、输入流并使用 Base 64 解码器取回原始字符串。
完整的工作示例(只需复制和粘贴):
import javax.crypto.SecretKey;
import javax.crypto.KeyGenerator;
import javax.crypto.Cipher;
import javax.crypto.SealedObject;
import java.io.Serializable;
import java.io.ByteArrayOutputStream;
import javax.crypto.CipherOutputStream;
import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
import javax.crypto.CipherInputStream;
import java.io.ObjectInputStream;
import java.util.Base64;
public class MyClass {
public static void main(String args[]) {
OtherClass myObject = new OtherClass();
myObject.print();
}
}
// you can add other public classes to this editor in any order
class OtherClass
{
public void print() {
try {
String userCodes = "Test123";
// generate a secret key using the DES algorithm
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
Cipher ecipher = Cipher.getInstance("DES");
// initialize the ciphers with the given key
ecipher.init(Cipher.ENCRYPT_MODE, key);
// create a sealed object
SealedObject sealed = new SealedObject((Serializable) userCodes, ecipher);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CipherOutputStream cipherOutputStream = new CipherOutputStream(
outputStream, ecipher);
ObjectOutputStream oos = new ObjectOutputStream(cipherOutputStream);
oos.writeObject( sealed );
cipherOutputStream.close();
byte[] values = outputStream.toByteArray();
String base64encoded = Base64.getEncoder().encodeToString(values);
System.out.println(base64encoded);
// decrypt
Cipher fcipher = Cipher.getInstance("DES");
fcipher.init(Cipher.DECRYPT_MODE, key);
ByteArrayInputStream istream = new ByteArrayInputStream(Base64.getDecoder().decode(base64encoded));
CipherInputStream cipherInputStream = new CipherInputStream(istream, fcipher);
ObjectInputStream inputStream = new ObjectInputStream(cipherInputStream);
SealedObject sealdedObject = (SealedObject) inputStream.readObject();
System.out.println(sealdedObject.getObject(key));
}
catch(Exception e){
e.printStackTrace();
}
}
}
关于java - 如何在没有文件 I/O 的情况下打印 SealedObject 加密数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53881635/
下面是我的代码。当我尝试打印密封对象时,它只显示 "javax.crypto.SealedObject@34dac684" private void encryptUserCodes(List use
我以rsa加密和解密为例。如果消息大于 110 字节,我无法解密。但错误说;数据不得超过 117 字节。为什么7字节不能用? 我的类(class): public class RSAEx {
我有两种加密和解密 java 对象的方法: a) 使用 SealedObject 并使用普通的 FileOutputStream 和 FileInputStream 保存和加载它 b) 使用Ciphe
我正在尝试使用 SealedObjects 通过 RMI 连接发送数据 target 是一个 SealedObject,密码是预先初始化的(据我所知是正确的),但是当我运行以下代码时,我得到一个 IO
我尝试使用 ECIES 密码来实例化 SealedObject,但失败并出现 NullPointerException。我正在使用 Java JDK1.8.0_72 和在 Windows 10 上运行
运行时错误如下: 线程“main”java.lang.ClassCastException中出现异常:类消息无法转换为类javax.crypto.SealedObject(消息位于加载程序“app”的
我是一名优秀的程序员,十分优秀!