gpt4 book ai didi

java - 如何在没有文件 I/O 的情况下打印 SealedObject 加密数据?

转载 作者:行者123 更新时间:2023-11-30 06:42:13 25 4
gpt4 key购买 nike

下面是我的代码。当我尝试打印密封对象时,它只显示

"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/

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