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 |
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
1694
1694
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 |
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