Java - Encoding message to String
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 at 21:11
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 20:55
J. Doe
31
31
use the serialization interface
– kelalaka
Nov 19 at 21:11
add a comment |
use the serialization interface
– kelalaka
Nov 19 at 21:11
use the serialization interface
– kelalaka
Nov 19 at 21:11
use the serialization interface
– kelalaka
Nov 19 at 21:11
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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 at 21:53
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 at 21:53
add a comment |
up vote
1
down vote
accepted
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 at 21:53
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 at 21:28
slimane
3944
3944
That worked, thank you a lot! :)
– J. Doe
Nov 19 at 21:53
add a comment |
That worked, thank you a lot! :)
– J. Doe
Nov 19 at 21:53
That worked, thank you a lot! :)
– J. Doe
Nov 19 at 21:53
That worked, thank you a lot! :)
– J. Doe
Nov 19 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 at 21:11