如何在没有文件 I/O 的情况下打印 SealedObject 加密数据?
How to print SealedObject encrypted data without file I/O?
下面是我的代码。
当我尝试打印密封对象时,它只显示
"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。解密:
创建一个新的密码,Inputstreams 并使用 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();
}
}
}
您的 sealed
对象是可序列化的。因此,您可以将其写入 ObjectOutputStream:
try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(sealed);
byte [] bytes = bos.toByteArray();
System.out.println(bytes);
} catch (IOException e) {
e.printStackTrace();
}
为了更友好地打印它,您可以将其编码为 base64:
String base64encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println(base64encoded);
System.out.println
将始终打印 toString()
方法的值。在您的情况下,打印 Class@hex 是对象 class 中的默认实现,它在 java.
中的所有 classes 中继承
您可以创建自定义方法来打印您的对象。
通过从您的对象调用 getter 方法并打印它们来提供遍历所需结果的方法定义。串联和 return 也是一种选择。
下面是我的代码。 当我尝试打印密封对象时,它只显示
"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。解密:
创建一个新的密码,Inputstreams 并使用 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();
}
}
}
您的 sealed
对象是可序列化的。因此,您可以将其写入 ObjectOutputStream:
try(ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(sealed);
byte [] bytes = bos.toByteArray();
System.out.println(bytes);
} catch (IOException e) {
e.printStackTrace();
}
为了更友好地打印它,您可以将其编码为 base64:
String base64encoded = Base64.getEncoder().encodeToString(bytes);
System.out.println(base64encoded);
System.out.println
将始终打印 toString()
方法的值。在您的情况下,打印 Class@hex 是对象 class 中的默认实现,它在 java.
您可以创建自定义方法来打印您的对象。
通过从您的对象调用 getter 方法并打印它们来提供遍历所需结果的方法定义。串联和 return 也是一种选择。