Java - Encoding message to String












0














I'm trying to implement my own version of authorization for HTTP requests. Now I'm facing a problem that I don't know how to resolve.



As shown in code below, I'm encrypting String message using RSA algorithm. But the problem is that as a result I'm getting object of class SealedObject. I need to have the possibility to use this encrypted string as header - for now using REST client like Postman. So, my question is: How can I parse SealedObject to String? Or what should I do to encrypt the message to String? Is this even possible?



KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = kpg.generateKeyPair();

String message = "Secret message";

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

SealedObject encryptedMessage = new SealedObject(message, cipher);


Thank you in advance :)










share|improve this question






















  • use the serialization interface
    – kelalaka
    Nov 19 '18 at 21:11
















0














I'm trying to implement my own version of authorization for HTTP requests. Now I'm facing a problem that I don't know how to resolve.



As shown in code below, I'm encrypting String message using RSA algorithm. But the problem is that as a result I'm getting object of class SealedObject. I need to have the possibility to use this encrypted string as header - for now using REST client like Postman. So, my question is: How can I parse SealedObject to String? Or what should I do to encrypt the message to String? Is this even possible?



KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = kpg.generateKeyPair();

String message = "Secret message";

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

SealedObject encryptedMessage = new SealedObject(message, cipher);


Thank you in advance :)










share|improve this question






















  • use the serialization interface
    – kelalaka
    Nov 19 '18 at 21:11














0












0








0







I'm trying to implement my own version of authorization for HTTP requests. Now I'm facing a problem that I don't know how to resolve.



As shown in code below, I'm encrypting String message using RSA algorithm. But the problem is that as a result I'm getting object of class SealedObject. I need to have the possibility to use this encrypted string as header - for now using REST client like Postman. So, my question is: How can I parse SealedObject to String? Or what should I do to encrypt the message to String? Is this even possible?



KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = kpg.generateKeyPair();

String message = "Secret message";

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

SealedObject encryptedMessage = new SealedObject(message, cipher);


Thank you in advance :)










share|improve this question













I'm trying to implement my own version of authorization for HTTP requests. Now I'm facing a problem that I don't know how to resolve.



As shown in code below, I'm encrypting String message using RSA algorithm. But the problem is that as a result I'm getting object of class SealedObject. I need to have the possibility to use this encrypted string as header - for now using REST client like Postman. So, my question is: How can I parse SealedObject to String? Or what should I do to encrypt the message to String? Is this even possible?



KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = kpg.generateKeyPair();

String message = "Secret message";

Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

SealedObject encryptedMessage = new SealedObject(message, cipher);


Thank you in advance :)







java rest encryption httprequest rsa






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 20:55









J. DoeJ. Doe

31




31












  • use the serialization interface
    – kelalaka
    Nov 19 '18 at 21:11


















  • use the serialization interface
    – kelalaka
    Nov 19 '18 at 21:11
















use the serialization interface
– kelalaka
Nov 19 '18 at 21:11




use the serialization interface
– kelalaka
Nov 19 '18 at 21:11












1 Answer
1






active

oldest

votes


















1














the first thing that comes to mind is:



SealedObject is a Serializable Object which means you can convert it to bytes and then transform it to String using Base64:
something like this:



ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(sealedObject);
out.flush();
byte yourBytes = bos.toByteArray();
String base64StringHeader = Base64.encodeBase64String(yourBytes);
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}


and then when you receive your request do something like this:



byte backToBytes = Base64.decodeBase64(base64StringHeader);
ByteArrayInputStream bis = new ByteArrayInputStream(backToBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
SealedObject = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}


}






share|improve this answer





















  • That worked, thank you a lot! :)
    – J. Doe
    Nov 19 '18 at 21:53











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382498%2fjava-encoding-message-to-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














the first thing that comes to mind is:



SealedObject is a Serializable Object which means you can convert it to bytes and then transform it to String using Base64:
something like this:



ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(sealedObject);
out.flush();
byte yourBytes = bos.toByteArray();
String base64StringHeader = Base64.encodeBase64String(yourBytes);
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}


and then when you receive your request do something like this:



byte backToBytes = Base64.decodeBase64(base64StringHeader);
ByteArrayInputStream bis = new ByteArrayInputStream(backToBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
SealedObject = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}


}






share|improve this answer





















  • That worked, thank you a lot! :)
    – J. Doe
    Nov 19 '18 at 21:53
















1














the first thing that comes to mind is:



SealedObject is a Serializable Object which means you can convert it to bytes and then transform it to String using Base64:
something like this:



ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(sealedObject);
out.flush();
byte yourBytes = bos.toByteArray();
String base64StringHeader = Base64.encodeBase64String(yourBytes);
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}


and then when you receive your request do something like this:



byte backToBytes = Base64.decodeBase64(base64StringHeader);
ByteArrayInputStream bis = new ByteArrayInputStream(backToBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
SealedObject = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}


}






share|improve this answer





















  • That worked, thank you a lot! :)
    – J. Doe
    Nov 19 '18 at 21:53














1












1








1






the first thing that comes to mind is:



SealedObject is a Serializable Object which means you can convert it to bytes and then transform it to String using Base64:
something like this:



ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(sealedObject);
out.flush();
byte yourBytes = bos.toByteArray();
String base64StringHeader = Base64.encodeBase64String(yourBytes);
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}


and then when you receive your request do something like this:



byte backToBytes = Base64.decodeBase64(base64StringHeader);
ByteArrayInputStream bis = new ByteArrayInputStream(backToBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
SealedObject = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}


}






share|improve this answer












the first thing that comes to mind is:



SealedObject is a Serializable Object which means you can convert it to bytes and then transform it to String using Base64:
something like this:



ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(sealedObject);
out.flush();
byte yourBytes = bos.toByteArray();
String base64StringHeader = Base64.encodeBase64String(yourBytes);
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}


and then when you receive your request do something like this:



byte backToBytes = Base64.decodeBase64(base64StringHeader);
ByteArrayInputStream bis = new ByteArrayInputStream(backToBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
SealedObject = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}


}







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 21:28









stackerstacker

1,09925




1,09925












  • That worked, thank you a lot! :)
    – J. Doe
    Nov 19 '18 at 21:53


















  • That worked, thank you a lot! :)
    – J. Doe
    Nov 19 '18 at 21:53
















That worked, thank you a lot! :)
– J. Doe
Nov 19 '18 at 21:53




That worked, thank you a lot! :)
– J. Doe
Nov 19 '18 at 21:53


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53382498%2fjava-encoding-message-to-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$