IllegalBlockSizeException when Decrypt String by RSA 2048 Private Key
I was trying to decrypt the string by RSA 2048 private key, but the error below is occurred.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: javax.crypto.IllegalBlockSizeException: input must be under 256 bytes
at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:245)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
Here is my code:
private fun rsaDecrypt(key: String, plain: String): String {
var privateKey = ""
if (key.contains("-----BEGIN RSA PRIVATE KEY-----") || key.contains("-----END RSA PRIVATE KEY-----")) {
privateKey = key.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "")
}
val base64Key = Base64.decode(privateKey.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
val spec = PKCS8EncodedKeySpec(base64Key)
val keyFactory = KeyFactory.getInstance("RSA", "BC")
val rsaPrivateKey = keyFactory.generatePrivate(spec)
val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey)
val decryptedBytes = cipher.doFinal(plain.toByteArray(Charsets.UTF_8))
return Base64.encodeToString(decryptedBytes, Base64.DEFAULT)
}
Encrypted string:
fhTGWW3Zn/nnhXoQO2Y3eKfYkBe/um7XUu2SlrnXMHT7tqT+KvcEUWXnJvLgN7/jpJp8ZOFvHiB6w8OFjPob7VSzQbyultpkFvBGZR7Z6j8m24IoqAPj2OhZDPQ4W8kza4eu/LP2560MfcbJqblOG0tcVF72fQZ2fXKPVRyWv53pDLfxC38+jxdd+49mSTRcl7sfyYvqvW4HVb4IH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HU0CgYEAmDDs
jgbTkmgLsp7/vzot+lMTv6er4KB3AHTJWY9pDkNv5vvcyd/I04S/vFDar8FvlrXM
qlcAYptnpUBUPWJ43cnE8VBz2pwInNzty490YHH1mQ622801vAOiZ0t7dowypuip
qE3zYTRUc+qqZmeZYw8IWX/uqxizz67yHelrTZsCgYBZZ6ta+Ml+hlGMQgtI951Z
edDLG4HI+3Fd0aJZVn6o8m9nffkqflbxlpHukV8AG13YMmZhBwq1ZM16ezRIMzgk
oX4Tr0w126AwZfWEVkocMD3yX1R2oT2g2YIY8OChuYz/tgfwFtyRXp9HBphoRO94
bhJkF5i1ZQkoLStx2793wQKBgD4827BlSEZ6ODZu4WWhdEYHjW72McrltGTCXck7
ZZoIqA2M+EiyNJ2TjxmC8zum3myColUSyTyKEwO4X/rOd2Ez709EOGyxHLCRhMmX
O99pGyfhpZQTwqLF1bdqXe4n/VrWkPMcFaitzs+sqszAFzE0Z5S6Ra9ApeALykXD
PDd7AoGBAPQEoz1MYCT58LMwZwJJPeX6RbHp0j9rnzOoHcZof2XBCZ1JmTDtQ6F7
P1Exmmt/UH1km0Nq/caJx2M751bAPP+hM5t4ycZAzpQQsiZaXHDtq+OAIAPyC+vf
nnfSk8udDv445chqGZiMGIwg3XavoUJ5yzu8CTKd5dItxPwzG46Z
-----END RSA PRIVATE KEY-----
How can I decrypt the string?
android kotlin rsa
add a comment |
I was trying to decrypt the string by RSA 2048 private key, but the error below is occurred.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: javax.crypto.IllegalBlockSizeException: input must be under 256 bytes
at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:245)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
Here is my code:
private fun rsaDecrypt(key: String, plain: String): String {
var privateKey = ""
if (key.contains("-----BEGIN RSA PRIVATE KEY-----") || key.contains("-----END RSA PRIVATE KEY-----")) {
privateKey = key.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "")
}
val base64Key = Base64.decode(privateKey.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
val spec = PKCS8EncodedKeySpec(base64Key)
val keyFactory = KeyFactory.getInstance("RSA", "BC")
val rsaPrivateKey = keyFactory.generatePrivate(spec)
val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey)
val decryptedBytes = cipher.doFinal(plain.toByteArray(Charsets.UTF_8))
return Base64.encodeToString(decryptedBytes, Base64.DEFAULT)
}
Encrypted string:
fhTGWW3Zn/nnhXoQO2Y3eKfYkBe/um7XUu2SlrnXMHT7tqT+KvcEUWXnJvLgN7/jpJp8ZOFvHiB6w8OFjPob7VSzQbyultpkFvBGZR7Z6j8m24IoqAPj2OhZDPQ4W8kza4eu/LP2560MfcbJqblOG0tcVF72fQZ2fXKPVRyWv53pDLfxC38+jxdd+49mSTRcl7sfyYvqvW4HVb4IHnwwt+m+1Y4zG7GFxnP+FuywgXmELO1skNCdCHxZ2zopZDtZiBA0vplEkWv18V2lF7SNnaN17SVQQc83yfZnwTQjbfKBWd0HU+4DKtO5ADGJmMCMRV0ceq+BnP141Q45CFDxbQ==
Private key:
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAk2ZbtbEaZhgdRzOecjfgsQqvlPf7SizxrAqT1PW3clXf9Lfq
ZE48aScg/S0Ji0JlgFcD3Ycg/+YTnZZTwCxsG/DgLP8Y2Oiq1wBivuMnOzIh9+Ty
oRAKJvvsc9C0ZfI0XMLntM4utlb772/bmaHJPkW0p8KIuIHBqLYHcMKAy1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SEZ6ODZu4WWhdEYHjW72McrltGTCXck7
ZZoIqA2M+EiyNJ2TjxmC8zum3myColUSyTyKEwO4X/rOd2Ez709EOGyxHLCRhMmX
O99pGyfhpZQTwqLF1bdqXe4n/VrWkPMcFaitzs+sqszAFzE0Z5S6Ra9ApeALykXD
PDd7AoGBAPQEoz1MYCT58LMwZwJJPeX6RbHp0j9rnzOoHcZof2XBCZ1JmTDtQ6F7
P1Exmmt/UH1km0Nq/caJx2M751bAPP+hM5t4ycZAzpQQsiZaXHDtq+OAIAPyC+vf
nnfSk8udDv445chqGZiMGIwg3XavoUJ5yzu8CTKd5dItxPwzG46Z
-----END RSA PRIVATE KEY-----
How can I decrypt the string?
android kotlin rsa
refer stackoverflow.com/questions/10007147/…
– sasikumar
Nov 14 '18 at 8:40
With PKCS1Padding adds 11 bytes of padding so a message of the key size will be to large by 11 bytes. See the above comment link.
– zaph
Nov 14 '18 at 8:47
I don't believe that exception is possible with the data you've supplied. First of all, the private is not in PKCS#8 format, so I don't see how your code could've gotten past thekeyFactory.generatePrivate(spec)
without throwing an exception at that point.
– James K Polk
Nov 14 '18 at 14:54
Well, I actually ran your an almost identical Java version of your code on an Android emulator and worked as is, which was very surprising.
– James K Polk
Nov 14 '18 at 17:55
add a comment |
I was trying to decrypt the string by RSA 2048 private key, but the error below is occurred.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: javax.crypto.IllegalBlockSizeException: input must be under 256 bytes
at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:245)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
Here is my code:
private fun rsaDecrypt(key: String, plain: String): String {
var privateKey = ""
if (key.contains("-----BEGIN RSA PRIVATE KEY-----") || key.contains("-----END RSA PRIVATE KEY-----")) {
privateKey = key.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "")
}
val base64Key = Base64.decode(privateKey.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
val spec = PKCS8EncodedKeySpec(base64Key)
val keyFactory = KeyFactory.getInstance("RSA", "BC")
val rsaPrivateKey = keyFactory.generatePrivate(spec)
val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey)
val decryptedBytes = cipher.doFinal(plain.toByteArray(Charsets.UTF_8))
return Base64.encodeToString(decryptedBytes, Base64.DEFAULT)
}
Encrypted string:
fhTGWW3Zn/nnhXoQO2Y3eKfYkBe/um7XUu2SlrnXMHT7tqT+KvcEUWXnJvLgN7/jpJp8ZOFvHiB6w8OFjPob7VSzQbyultpkFvBGZR7Z6j8m24IoqAPj2OhZDPQ4W8kza4eu/LP2560MfcbJqblOG0tcVF72fQZ2fXKPVRyWv53pDLfxC38+jxdd+49mSTRcl7sfyYvqvW4HVb4IHnwwt+m+1Y4zG7GFxnP+FuywgXmELO1skNCdCHxZ2zopZDtZiBA0vplEkWv18V2lF7SNnaN17SVQQc83yfZnwTQjbfKBWd0HU+4DKtO5ADGJmMCMRV0ceq+BnP141Q45CFDxbQ==
Private key:
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAk2ZbtbEaZhgdRzOecjfgsQqvlPf7SizxrAqT1PW3clXf9Lfq
ZE48aScg/S0Ji0JlgFcD3Ycg/+YTnZZTwCxsG/DgLP8Y2Oiq1wBivuMnOzIh9+Ty
oRAKJvvsc9C0ZfI0XMLntM4utlb772/bmaHJPkW0p8KIuIHBqLYHcMKAy1R08Xmm
fQ6Q5JLy0uFy3wWTqbdk7uXDDfbo4C9MiGdVYa0RGlt703aYpSc3d9Du3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EwO4X/rOd2Ez709EOGyxHLCRhMmX
O99pGyfhpZQTwqLF1bdqXe4n/VrWkPMcFaitzs+sqszAFzE0Z5S6Ra9ApeALykXD
PDd7AoGBAPQEoz1MYCT58LMwZwJJPeX6RbHp0j9rnzOoHcZof2XBCZ1JmTDtQ6F7
P1Exmmt/UH1km0Nq/caJx2M751bAPP+hM5t4ycZAzpQQsiZaXHDtq+OAIAPyC+vf
nnfSk8udDv445chqGZiMGIwg3XavoUJ5yzu8CTKd5dItxPwzG46Z
-----END RSA PRIVATE KEY-----
How can I decrypt the string?
android kotlin rsa
I was trying to decrypt the string by RSA 2048 private key, but the error below is occurred.
java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Caused by: javax.crypto.IllegalBlockSizeException: input must be under 256 bytes
at com.android.org.conscrypt.OpenSSLCipherRSA.engineDoFinal(OpenSSLCipherRSA.java:245)
at javax.crypto.Cipher.doFinal(Cipher.java:1340)
Here is my code:
private fun rsaDecrypt(key: String, plain: String): String {
var privateKey = ""
if (key.contains("-----BEGIN RSA PRIVATE KEY-----") || key.contains("-----END RSA PRIVATE KEY-----")) {
privateKey = key.replace("-----BEGIN RSA PRIVATE KEY-----", "").replace("-----END RSA PRIVATE KEY-----", "")
}
val base64Key = Base64.decode(privateKey.toByteArray(Charsets.UTF_8), Base64.DEFAULT)
val spec = PKCS8EncodedKeySpec(base64Key)
val keyFactory = KeyFactory.getInstance("RSA", "BC")
val rsaPrivateKey = keyFactory.generatePrivate(spec)
val cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey)
val decryptedBytes = cipher.doFinal(plain.toByteArray(Charsets.UTF_8))
return Base64.encodeToString(decryptedBytes, Base64.DEFAULT)
}
Encrypted string:
fhTGWW3Zn/nnhXoQO2Y3eKfYkBe/um7XUu2SlrnXMHT7tqT+KvcEUWXnJvLgN7/jpJp8ZOFvHiB6w8OFjPob7VSzQbyultpkFvBGZR7Z6j8m24IoqAPj2OhZDPQ4W8kza4eu/LP2560MfcbJqblOG0tcVF72fQZ2fXKPVRyWv53pDLfxC38+jxdd+49mSTRcl7sfyYvqvW4HVb4IHnwwt+m+1Y4zG7GFxnP+FuywgXmELO1skNCdCHxZ2zopZDtZiBA0vplEkWv18V2lF7SNnaN17SVQQc83yfZnwTQjbfKBWd0HU+4DKtO5ADGJmMCMRV0ceq+BnP141Q45CFDxbQ==
Private key:
-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEAk2ZbtbEaZhgdRzOecjfgsQqvlPf7SizxrAqT1PW3clXf9Lfq
ZE48aScg/S0Ji0JlgFcD3Ycg/+YTnZZTwCxsG/DgLP8Y2Oiq1wBivuMnOzIh9+Ty
oRAKJvvsc9C0ZfI0XMLntM4utlb772/bmaHJPkW0p8KIuIHBqLYHcMKAy1R08Xmm
fQ6Q5JLy0uFy3wWTqbdk7uXDDfbo4C9MiGdVYa0RGlt703aYpSc3d9Du3U2Xm3dt
Q0zgbBQnmzpahuW/J7hWCNdeoAXbekX0YV38vmVWf1DI+OQXHqKvy/nHflJXcQXe
v7hgJA5oFvAvW0MwaC++Pp2E5fRdD5PZ2qHmnwIDAQABAoIBAHkqjj74O9wq/H5Y
R2pa1qCSSS1rAC3EblFfTJCmAEvDndr8CHi1+2DxImQcTpkkVm2+mdXTy+OvzPYg
6e2yVphNVOqINHOyJyWgEowaDFjT6CpwEKcnmUDcnE3EBVc8V6DdMz1ks+9955G2
4M0UumovQMca2erZ90jpgYpGyTrahG7nN2HKaxO2mcJzZPXJzFyc1SrY1IPpEtlP
xsLsnaRjE2DgClf72DBI0Qwa/ZaRjnjnAcvU4tVp2BIZYLqKpXU/1LMiRF39LKQC
coVFwzqLLkb84v6ReGy8J2mWQpSuyWpmGiLacMyBf1OPDkAa/gXpHUOg+jJOzVlX
ZsC9jDkCgYEA9/DMjSF//hdmSgxjQsLii/NJBdpa3t1yfo8q7hHwZQ9Xz0R4YPg3
SifnGNI3TnRzpVqiGGkpYsogk8QwkVO4J8uvddUDshbFX5F7sReOJxwwNmTy5FR0
fx/AEeJ3GhvkMfNBwZcaQtngK2UKu9rRsaF7bitL4intxU/EtmpDHU0CgYEAmDDs
jgbTkmgLsp7/vzot+lMTv6er4KB3AHTJWY9pDkNv5vvcyd/I04S/vFDar8FvlrXM
qlcAYptnpUBUPWJ43cnE8VBz2pwInNzty490YHH1mQ622801vAOiZ0t7dowypuip
qE3zYTRUc+qqZmeZYw8IWX/uqxizz67yHelrTZsCgYBZZ6ta+Ml+hlGMQgtI951Z
edDLG4HI+3Fd0aJZVn6o8m9nffkqflbxlpHukV8AG13YMmZhBwq1ZM16ezRIMzgk
oX4Tr0w126AwZfWEVkocMD3yX1R2oT2g2YIY8OChuYz/tgfwFtyRXp9HBphoRO94
bhJkF5i1ZQkoLStx2793wQKBgD4827BlSEZ6ODZu4WWhdEYHjW72McrltGTCXck7
ZZoIqA2M+EiyNJ2TjxmC8zum3myColUSyTyKEwO4X/rOd2Ez709EOGyxHLCRhMmX
O99pGyfhpZQTwqLF1bdqXe4n/VrWkPMcFaitzs+sqszAFzE0Z5S6Ra9ApeALykXD
PDd7AoGBAPQEoz1MYCT58LMwZwJJPeX6RbHp0j9rnzOoHcZof2XBCZ1JmTDtQ6F7
P1Exmmt/UH1km0Nq/caJx2M751bAPP+hM5t4ycZAzpQQsiZaXHDtq+OAIAPyC+vf
nnfSk8udDv445chqGZiMGIwg3XavoUJ5yzu8CTKd5dItxPwzG46Z
-----END RSA PRIVATE KEY-----
How can I decrypt the string?
android kotlin rsa
android kotlin rsa
edited Nov 14 '18 at 14:46
James K Polk
30k116896
30k116896
asked Nov 14 '18 at 6:28
shisushishisushi
687
687
refer stackoverflow.com/questions/10007147/…
– sasikumar
Nov 14 '18 at 8:40
With PKCS1Padding adds 11 bytes of padding so a message of the key size will be to large by 11 bytes. See the above comment link.
– zaph
Nov 14 '18 at 8:47
I don't believe that exception is possible with the data you've supplied. First of all, the private is not in PKCS#8 format, so I don't see how your code could've gotten past thekeyFactory.generatePrivate(spec)
without throwing an exception at that point.
– James K Polk
Nov 14 '18 at 14:54
Well, I actually ran your an almost identical Java version of your code on an Android emulator and worked as is, which was very surprising.
– James K Polk
Nov 14 '18 at 17:55
add a comment |
refer stackoverflow.com/questions/10007147/…
– sasikumar
Nov 14 '18 at 8:40
With PKCS1Padding adds 11 bytes of padding so a message of the key size will be to large by 11 bytes. See the above comment link.
– zaph
Nov 14 '18 at 8:47
I don't believe that exception is possible with the data you've supplied. First of all, the private is not in PKCS#8 format, so I don't see how your code could've gotten past thekeyFactory.generatePrivate(spec)
without throwing an exception at that point.
– James K Polk
Nov 14 '18 at 14:54
Well, I actually ran your an almost identical Java version of your code on an Android emulator and worked as is, which was very surprising.
– James K Polk
Nov 14 '18 at 17:55
refer stackoverflow.com/questions/10007147/…
– sasikumar
Nov 14 '18 at 8:40
refer stackoverflow.com/questions/10007147/…
– sasikumar
Nov 14 '18 at 8:40
With PKCS1Padding adds 11 bytes of padding so a message of the key size will be to large by 11 bytes. See the above comment link.
– zaph
Nov 14 '18 at 8:47
With PKCS1Padding adds 11 bytes of padding so a message of the key size will be to large by 11 bytes. See the above comment link.
– zaph
Nov 14 '18 at 8:47
I don't believe that exception is possible with the data you've supplied. First of all, the private is not in PKCS#8 format, so I don't see how your code could've gotten past the
keyFactory.generatePrivate(spec)
without throwing an exception at that point.– James K Polk
Nov 14 '18 at 14:54
I don't believe that exception is possible with the data you've supplied. First of all, the private is not in PKCS#8 format, so I don't see how your code could've gotten past the
keyFactory.generatePrivate(spec)
without throwing an exception at that point.– James K Polk
Nov 14 '18 at 14:54
Well, I actually ran your an almost identical Java version of your code on an Android emulator and worked as is, which was very surprising.
– James K Polk
Nov 14 '18 at 17:55
Well, I actually ran your an almost identical Java version of your code on an Android emulator and worked as is, which was very surprising.
– James K Polk
Nov 14 '18 at 17:55
add a comment |
1 Answer
1
active
oldest
votes
The problem was very simple. You failed to base64-decode the plain
argument. As an interesting aside, the Bouncycastle provider is somewhat forgiving in its PKCS8EncodedKeySpec generator. If the data passed is not PKCS8 then it attempts to parse it as a PKCS #1 RSA Private key, and only if that fails does it finally throw an exception. You private key is formatting according to PKCS#1, not PKCS#8.
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%2f53294291%2fillegalblocksizeexception-when-decrypt-string-by-rsa-2048-private-key%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 problem was very simple. You failed to base64-decode the plain
argument. As an interesting aside, the Bouncycastle provider is somewhat forgiving in its PKCS8EncodedKeySpec generator. If the data passed is not PKCS8 then it attempts to parse it as a PKCS #1 RSA Private key, and only if that fails does it finally throw an exception. You private key is formatting according to PKCS#1, not PKCS#8.
add a comment |
The problem was very simple. You failed to base64-decode the plain
argument. As an interesting aside, the Bouncycastle provider is somewhat forgiving in its PKCS8EncodedKeySpec generator. If the data passed is not PKCS8 then it attempts to parse it as a PKCS #1 RSA Private key, and only if that fails does it finally throw an exception. You private key is formatting according to PKCS#1, not PKCS#8.
add a comment |
The problem was very simple. You failed to base64-decode the plain
argument. As an interesting aside, the Bouncycastle provider is somewhat forgiving in its PKCS8EncodedKeySpec generator. If the data passed is not PKCS8 then it attempts to parse it as a PKCS #1 RSA Private key, and only if that fails does it finally throw an exception. You private key is formatting according to PKCS#1, not PKCS#8.
The problem was very simple. You failed to base64-decode the plain
argument. As an interesting aside, the Bouncycastle provider is somewhat forgiving in its PKCS8EncodedKeySpec generator. If the data passed is not PKCS8 then it attempts to parse it as a PKCS #1 RSA Private key, and only if that fails does it finally throw an exception. You private key is formatting according to PKCS#1, not PKCS#8.
answered Nov 14 '18 at 22:22
James K PolkJames K Polk
30k116896
30k116896
add a comment |
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%2f53294291%2fillegalblocksizeexception-when-decrypt-string-by-rsa-2048-private-key%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
refer stackoverflow.com/questions/10007147/…
– sasikumar
Nov 14 '18 at 8:40
With PKCS1Padding adds 11 bytes of padding so a message of the key size will be to large by 11 bytes. See the above comment link.
– zaph
Nov 14 '18 at 8:47
I don't believe that exception is possible with the data you've supplied. First of all, the private is not in PKCS#8 format, so I don't see how your code could've gotten past the
keyFactory.generatePrivate(spec)
without throwing an exception at that point.– James K Polk
Nov 14 '18 at 14:54
Well, I actually ran your an almost identical Java version of your code on an Android emulator and worked as is, which was very surprising.
– James K Polk
Nov 14 '18 at 17:55