Java - Encoding message to String
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
add a comment |
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
use the serialization interface
– kelalaka
Nov 19 '18 at 21:11
add a comment |
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
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
java rest encryption httprequest rsa
asked Nov 19 '18 at 20:55
J. DoeJ. Doe
31
31
use the serialization interface
– kelalaka
Nov 19 '18 at 21:11
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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
}
}
That worked, thank you a lot! :)
– J. Doe
Nov 19 '18 at 21:53
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
}
}
That worked, thank you a lot! :)
– J. Doe
Nov 19 '18 at 21:53
add a comment |
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
}
}
That worked, thank you a lot! :)
– J. Doe
Nov 19 '18 at 21:53
add a comment |
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
}
}
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
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
use the serialization interface
– kelalaka
Nov 19 '18 at 21:11