Android RSA Encryption throws InvalidKeySpecException
up vote
-1
down vote
favorite
I'm trying to encrypt data using a given public key.
public static final String public_key = "MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB";
String encryptData(String txt)
String encoded = null;
try
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(Base64.decode(public_key, Base64.DEFAULT)));
Cipher cph = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cph.init(Cipher.ENCRYPT_MODE, key);
encoded = Base64.encodeToString(cph.doFinal(txt.getBytes()),
Base64.DEFAULT);
catch (Exception e)
e.printStackTrace();
return encoded;
And get the error
W/System.err: java.security.spec.InvalidKeySpecException:
java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:143)
at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
I have used the same public key in Python3, with the 'BEGIN PUBLIC KEY...END PUBLIC KEY' header/footer and it works fine:
public_key = """-----BEGIN PUBLIC KEY-----
MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB
-----END PUBLIC KEY-----
"""
def encode(msg):
rsa_key = RSA.importKey(public_key)
pks1_v1_5 = PKCS1_v1_5.new(rsa_key)
encrypted = pks1_v1_5.encrypt(msg.encode('utf-8'))
encrypted = base64.b64encode(encrypted)
return encrypted
Can someone help me out plz?
--- EDIT ---
I did some debugging on the Python code: stepping into 'RSA.importKey(public_key)' I see it recognizes the key as PEM encoded key, removes the header/footer and converts it to binary (binascii.a2b_base64). The binary is passed to RSA._importKeyDER which discovers that it follows the PKCS#1 standard and, in comment, 'The DER object is an RSAPublicKey SEQUENCE with two elements'.
android rsa public-key-encryption
|
show 1 more comment
up vote
-1
down vote
favorite
I'm trying to encrypt data using a given public key.
public static final String public_key = "MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB";
String encryptData(String txt)
String encoded = null;
try
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(Base64.decode(public_key, Base64.DEFAULT)));
Cipher cph = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cph.init(Cipher.ENCRYPT_MODE, key);
encoded = Base64.encodeToString(cph.doFinal(txt.getBytes()),
Base64.DEFAULT);
catch (Exception e)
e.printStackTrace();
return encoded;
And get the error
W/System.err: java.security.spec.InvalidKeySpecException:
java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:143)
at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
I have used the same public key in Python3, with the 'BEGIN PUBLIC KEY...END PUBLIC KEY' header/footer and it works fine:
public_key = """-----BEGIN PUBLIC KEY-----
MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB
-----END PUBLIC KEY-----
"""
def encode(msg):
rsa_key = RSA.importKey(public_key)
pks1_v1_5 = PKCS1_v1_5.new(rsa_key)
encrypted = pks1_v1_5.encrypt(msg.encode('utf-8'))
encrypted = base64.b64encode(encrypted)
return encrypted
Can someone help me out plz?
--- EDIT ---
I did some debugging on the Python code: stepping into 'RSA.importKey(public_key)' I see it recognizes the key as PEM encoded key, removes the header/footer and converts it to binary (binascii.a2b_base64). The binary is passed to RSA._importKeyDER which discovers that it follows the PKCS#1 standard and, in comment, 'The DER object is an RSAPublicKey SEQUENCE with two elements'.
android rsa public-key-encryption
Your key is not in the correct format for X509EncodedKeySpec. The simplest fix is to put it into the correct format.
– James K Polk
Nov 11 at 16:38
@JamesKPolk I get the public key from another system via HTTP request and I have tested and succeeded to encrypt using this key with the Python3 code in my post.
– yann1s
Nov 11 at 21:18
Excellent, than you can dorsa_key.export_key(format='DER')
from your python code and the result will be in the correct format for Java's X509EncodedKeySpec.
– James K Polk
Nov 11 at 21:56
@JamesKPolk problem is that the public_key is not static, as in the code above, I receive it from an HTTP request and it is not the same every time, so I need to find the Java way to correctly get the RSA key from the public key I receive
– yann1s
Nov 11 at 22:41
Well, then you are going to have to write some code to convert it. The Bouncycastle Java library can do most of the work for you, if you can use it in your project.
– James K Polk
Nov 11 at 23:00
|
show 1 more comment
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm trying to encrypt data using a given public key.
public static final String public_key = "MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB";
String encryptData(String txt)
String encoded = null;
try
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(Base64.decode(public_key, Base64.DEFAULT)));
Cipher cph = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cph.init(Cipher.ENCRYPT_MODE, key);
encoded = Base64.encodeToString(cph.doFinal(txt.getBytes()),
Base64.DEFAULT);
catch (Exception e)
e.printStackTrace();
return encoded;
And get the error
W/System.err: java.security.spec.InvalidKeySpecException:
java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:143)
at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
I have used the same public key in Python3, with the 'BEGIN PUBLIC KEY...END PUBLIC KEY' header/footer and it works fine:
public_key = """-----BEGIN PUBLIC KEY-----
MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB
-----END PUBLIC KEY-----
"""
def encode(msg):
rsa_key = RSA.importKey(public_key)
pks1_v1_5 = PKCS1_v1_5.new(rsa_key)
encrypted = pks1_v1_5.encrypt(msg.encode('utf-8'))
encrypted = base64.b64encode(encrypted)
return encrypted
Can someone help me out plz?
--- EDIT ---
I did some debugging on the Python code: stepping into 'RSA.importKey(public_key)' I see it recognizes the key as PEM encoded key, removes the header/footer and converts it to binary (binascii.a2b_base64). The binary is passed to RSA._importKeyDER which discovers that it follows the PKCS#1 standard and, in comment, 'The DER object is an RSAPublicKey SEQUENCE with two elements'.
android rsa public-key-encryption
I'm trying to encrypt data using a given public key.
public static final String public_key = "MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB";
String encryptData(String txt)
String encoded = null;
try
PublicKey key = KeyFactory.getInstance("RSA").generatePublic(
new X509EncodedKeySpec(Base64.decode(public_key, Base64.DEFAULT)));
Cipher cph = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cph.init(Cipher.ENCRYPT_MODE, key);
encoded = Base64.encodeToString(cph.doFinal(txt.getBytes()),
Base64.DEFAULT);
catch (Exception e)
e.printStackTrace();
return encoded;
And get the error
W/System.err: java.security.spec.InvalidKeySpecException:
java.lang.RuntimeException: error:0D0680A8:asn1 encoding routines:ASN1_CHECK_TLEN:wrong tag
at com.android.org.conscrypt.OpenSSLKey.getPublicKey(OpenSSLKey.java:143)
at com.android.org.conscrypt.OpenSSLRSAKeyFactory.engineGeneratePublic(OpenSSLRSAKeyFactory.java:47)
at java.security.KeyFactory.generatePublic(KeyFactory.java:172)
I have used the same public key in Python3, with the 'BEGIN PUBLIC KEY...END PUBLIC KEY' header/footer and it works fine:
public_key = """-----BEGIN PUBLIC KEY-----
MIIBCgKCAQEAr/oYAoxIcXnLzVDNN6TPJVjkwOJZnDcSEeoRntqhOvgjiycfswMWZZ5+UClJ4CMgMCVAs71BzAJzPv902Jt763SPkAO/vh6CwfLq2S3YcqDoRQJYZuSKQHW40R6sN7eFvQdxYhJnF45ketCdLdPFuF5o/ieChwLcCEDKzkWD7xio2TQlZ8jfzB4jNGr6bmW/aqF5ihe0pbhtfvlyM+jNF2vWeB1SCJ4v5zHLNKKYNy4cMsmIGHKB+0BaGVz87eYp65FFc2K9LawBBbWtVCxykYBzEnXRuU+0YzcTi4LThXg1cUsf++LK9qL/G7PZdN6HMGP7DYzgstFLfp8VRpKhqQIDAQAB
-----END PUBLIC KEY-----
"""
def encode(msg):
rsa_key = RSA.importKey(public_key)
pks1_v1_5 = PKCS1_v1_5.new(rsa_key)
encrypted = pks1_v1_5.encrypt(msg.encode('utf-8'))
encrypted = base64.b64encode(encrypted)
return encrypted
Can someone help me out plz?
--- EDIT ---
I did some debugging on the Python code: stepping into 'RSA.importKey(public_key)' I see it recognizes the key as PEM encoded key, removes the header/footer and converts it to binary (binascii.a2b_base64). The binary is passed to RSA._importKeyDER which discovers that it follows the PKCS#1 standard and, in comment, 'The DER object is an RSAPublicKey SEQUENCE with two elements'.
android rsa public-key-encryption
android rsa public-key-encryption
edited Nov 11 at 23:10
asked Nov 11 at 3:25
yann1s
296
296
Your key is not in the correct format for X509EncodedKeySpec. The simplest fix is to put it into the correct format.
– James K Polk
Nov 11 at 16:38
@JamesKPolk I get the public key from another system via HTTP request and I have tested and succeeded to encrypt using this key with the Python3 code in my post.
– yann1s
Nov 11 at 21:18
Excellent, than you can dorsa_key.export_key(format='DER')
from your python code and the result will be in the correct format for Java's X509EncodedKeySpec.
– James K Polk
Nov 11 at 21:56
@JamesKPolk problem is that the public_key is not static, as in the code above, I receive it from an HTTP request and it is not the same every time, so I need to find the Java way to correctly get the RSA key from the public key I receive
– yann1s
Nov 11 at 22:41
Well, then you are going to have to write some code to convert it. The Bouncycastle Java library can do most of the work for you, if you can use it in your project.
– James K Polk
Nov 11 at 23:00
|
show 1 more comment
Your key is not in the correct format for X509EncodedKeySpec. The simplest fix is to put it into the correct format.
– James K Polk
Nov 11 at 16:38
@JamesKPolk I get the public key from another system via HTTP request and I have tested and succeeded to encrypt using this key with the Python3 code in my post.
– yann1s
Nov 11 at 21:18
Excellent, than you can dorsa_key.export_key(format='DER')
from your python code and the result will be in the correct format for Java's X509EncodedKeySpec.
– James K Polk
Nov 11 at 21:56
@JamesKPolk problem is that the public_key is not static, as in the code above, I receive it from an HTTP request and it is not the same every time, so I need to find the Java way to correctly get the RSA key from the public key I receive
– yann1s
Nov 11 at 22:41
Well, then you are going to have to write some code to convert it. The Bouncycastle Java library can do most of the work for you, if you can use it in your project.
– James K Polk
Nov 11 at 23:00
Your key is not in the correct format for X509EncodedKeySpec. The simplest fix is to put it into the correct format.
– James K Polk
Nov 11 at 16:38
Your key is not in the correct format for X509EncodedKeySpec. The simplest fix is to put it into the correct format.
– James K Polk
Nov 11 at 16:38
@JamesKPolk I get the public key from another system via HTTP request and I have tested and succeeded to encrypt using this key with the Python3 code in my post.
– yann1s
Nov 11 at 21:18
@JamesKPolk I get the public key from another system via HTTP request and I have tested and succeeded to encrypt using this key with the Python3 code in my post.
– yann1s
Nov 11 at 21:18
Excellent, than you can do
rsa_key.export_key(format='DER')
from your python code and the result will be in the correct format for Java's X509EncodedKeySpec.– James K Polk
Nov 11 at 21:56
Excellent, than you can do
rsa_key.export_key(format='DER')
from your python code and the result will be in the correct format for Java's X509EncodedKeySpec.– James K Polk
Nov 11 at 21:56
@JamesKPolk problem is that the public_key is not static, as in the code above, I receive it from an HTTP request and it is not the same every time, so I need to find the Java way to correctly get the RSA key from the public key I receive
– yann1s
Nov 11 at 22:41
@JamesKPolk problem is that the public_key is not static, as in the code above, I receive it from an HTTP request and it is not the same every time, so I need to find the Java way to correctly get the RSA key from the public key I receive
– yann1s
Nov 11 at 22:41
Well, then you are going to have to write some code to convert it. The Bouncycastle Java library can do most of the work for you, if you can use it in your project.
– James K Polk
Nov 11 at 23:00
Well, then you are going to have to write some code to convert it. The Bouncycastle Java library can do most of the work for you, if you can use it in your project.
– James K Polk
Nov 11 at 23:00
|
show 1 more comment
1 Answer
1
active
oldest
votes
up vote
0
down vote
Are you generate your public key from openssl, since your public key is too long:
for example i generated from openssl and replace your public key, and everything fine:
openssl genrsa -out key.pem 1024
openssl rsa -in key.pem -pubout > key.pub
and paste key.pub string in your code.
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
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%2f53245576%2fandroid-rsa-encryption-throws-invalidkeyspecexception%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
up vote
0
down vote
Are you generate your public key from openssl, since your public key is too long:
for example i generated from openssl and replace your public key, and everything fine:
openssl genrsa -out key.pem 1024
openssl rsa -in key.pem -pubout > key.pub
and paste key.pub string in your code.
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
add a comment |
up vote
0
down vote
Are you generate your public key from openssl, since your public key is too long:
for example i generated from openssl and replace your public key, and everything fine:
openssl genrsa -out key.pem 1024
openssl rsa -in key.pem -pubout > key.pub
and paste key.pub string in your code.
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
add a comment |
up vote
0
down vote
up vote
0
down vote
Are you generate your public key from openssl, since your public key is too long:
for example i generated from openssl and replace your public key, and everything fine:
openssl genrsa -out key.pem 1024
openssl rsa -in key.pem -pubout > key.pub
and paste key.pub string in your code.
Are you generate your public key from openssl, since your public key is too long:
for example i generated from openssl and replace your public key, and everything fine:
openssl genrsa -out key.pem 1024
openssl rsa -in key.pem -pubout > key.pub
and paste key.pub string in your code.
answered Nov 11 at 7:15
slee
20914
20914
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
add a comment |
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
I receive the public_key from another system, I need to encode data using their public key, so I don't know how they generate it.
– yann1s
Nov 11 at 10:29
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%2f53245576%2fandroid-rsa-encryption-throws-invalidkeyspecexception%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
Your key is not in the correct format for X509EncodedKeySpec. The simplest fix is to put it into the correct format.
– James K Polk
Nov 11 at 16:38
@JamesKPolk I get the public key from another system via HTTP request and I have tested and succeeded to encrypt using this key with the Python3 code in my post.
– yann1s
Nov 11 at 21:18
Excellent, than you can do
rsa_key.export_key(format='DER')
from your python code and the result will be in the correct format for Java's X509EncodedKeySpec.– James K Polk
Nov 11 at 21:56
@JamesKPolk problem is that the public_key is not static, as in the code above, I receive it from an HTTP request and it is not the same every time, so I need to find the Java way to correctly get the RSA key from the public key I receive
– yann1s
Nov 11 at 22:41
Well, then you are going to have to write some code to convert it. The Bouncycastle Java library can do most of the work for you, if you can use it in your project.
– James K Polk
Nov 11 at 23:00