当前位置: 首页 > 知识库问答 >
问题:

Jruby OpenSSL RSA Base64私钥无法实例化对象

能翔宇
2023-03-14

在Ruby中,我有简单的旧源代码行来创建RSA公钥或私钥:

OpenSSL::PKey::RSA.new(File.read(key_file))

我的RSA公钥和私钥的格式如下,我对MRI和Jruby使用完全相同的密钥文件:

-----BEGIN RSA PRIVATE KEY-----
MIIEvwIBADANBgkq...
-----END RSA PRIVATE KEY-----

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqh...
-----END PUBLIC KEY-----

当我在MRI v2.4.0上运行它时-任何问题,我都可以实例化公钥和私钥:

$> irb
> k = OpenSSL::PKey::RSA.new(File.read("private.pem"))
=> #<OpenSSL::PKey::RSA:0x007fda1096a6a8> 
> k = OpenSSL::PKey::RSA.new(File.read("public.pem"))
=> #<OpenSSL::PKey::RSA:0x007fda1096a6a8> 

但是,使用Jruby 9.0.4.0和9.1.8.0,我可以成功地启动公钥,但在初始化私钥时失败:

> set JRUBY_OPTS=-J-Djruby.openssl.debug=true
> irb
irb(main):001:0> require "openssl"
using provider: BC version 1.56
irb(main):002:0> k = OpenSSL::PKey::RSA.new(File.read("public.pem"))
=> #<OpenSSL::PKey::RSA:0x1c9e76b>

但创建私钥对象失败:

    irb(main):003:0> k = OpenSSL::PKey::RSA.new(File.read("private.pem"))
java.io.IOException: problem creating RSA private key: java.lang.ClassCastException: org.bouncycastle.asn1.DLSequence cannot be ca
st to org.bouncycastle.asn1.ASN1Integer
        at org.jruby.ext.openssl.x509store.PEMInputOutput.mapReadException(PEMInputOutput.java:383)
        at org.jruby.ext.openssl.x509store.PEMInputOutput.readPrivateKey(PEMInputOutput.java:328)
        at org.jruby.ext.openssl.PKey.readPrivateKey(PKey.java:395)
        at org.jruby.ext.openssl.PKeyRSA.initialize(PKeyRSA.java:260)
        at org.jruby.ext.openssl.PKeyRSA$INVOKER$i$0$0$initialize.call(PKeyRSA$INVOKER$i$0$0$initialize.gen)
        at org.jruby.internal.runtime.methods.JavaMethod$JavaMethodN.call(JavaMethod.java:720)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:171)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:177)
        at org.jruby.RubyClass.newInstance(RubyClass.java:1001)
        at org.jruby.RubyClass$INVOKER$i$newInstance.call(RubyClass$INVOKER$i$newInstance.gen)
        at org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:200)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:338)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:163)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:314)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.Interpreter.INTERPRET_EVAL(Interpreter.java:122)
        at org.jruby.ir.interpreter.Interpreter.evalCommon(Interpreter.java:176)
        at org.jruby.ir.interpreter.Interpreter.evalWithBinding(Interpreter.java:200)
        at org.jruby.RubyKernel.evalCommon(RubyKernel.java:1033)
        at org.jruby.RubyKernel.eval19(RubyKernel.java:1000)
        at org.jruby.RubyKernel$INVOKER$s$0$3$eval19.call(RubyKernel$INVOKER$s$0$3$eval19.gen)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:77)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:83)
        at org.jruby.ir.instructions.CallBase.interpret(CallBase.java:428)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:355)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:109)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:95)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:77)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:83)
        at org.jruby.ir.instructions.CallBase.interpret(CallBase.java:428)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:355)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.InterpreterEngine.interpret(InterpreterEngine.java:89)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:214)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:200)
        at org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:208)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:193)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:323)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.Interpreter.INTERPRET_BLOCK(Interpreter.java:132)
        at org.jruby.runtime.MixedModeIRBlockBody.commonYieldPath(MixedModeIRBlockBody.java:148)
        at org.jruby.runtime.IRBlockBody.yieldSpecific(IRBlockBody.java:80)
        at org.jruby.runtime.Block.yieldSpecific(Block.java:136)
        at org.jruby.ir.runtime.IRRuntimeHelpers.yieldSpecific(IRRuntimeHelpers.java:450)
        at org.jruby.ir.instructions.YieldInstr.interpret(YieldInstr.java:74)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.processOtherOp(StartupInterpreterEngine.java:187)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:111)
        at org.jruby.ir.interpreter.InterpreterEngine.interpret(InterpreterEngine.java:83)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:179)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:165)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:171)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:177)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:332)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.Interpreter.INTERPRET_BLOCK(Interpreter.java:132)
        at org.jruby.runtime.MixedModeIRBlockBody.commonYieldPath(MixedModeIRBlockBody.java:148)
        at org.jruby.runtime.IRBlockBody.doYield(IRBlockBody.java:189)
        at org.jruby.runtime.BlockBody.yield(BlockBody.java:120)
        at org.jruby.runtime.Block.yieldValues(Block.java:183)
        at org.jruby.ir.instructions.YieldInstr.interpret(YieldInstr.java:80)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.processOtherOp(StartupInterpreterEngine.java:187)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:111)
        at org.jruby.ir.interpreter.Interpreter.INTERPRET_BLOCK(Interpreter.java:132)
        at org.jruby.runtime.MixedModeIRBlockBody.commonYieldPath(MixedModeIRBlockBody.java:148)
        at org.jruby.runtime.IRBlockBody.yieldSpecific(IRBlockBody.java:80)
        at org.jruby.runtime.Block.yieldSpecific(Block.java:136)
        at org.jruby.RubyKernel.loop(RubyKernel.java:1298)
        at org.jruby.RubyKernel$INVOKER$s$0$0$loop.call(RubyKernel$INVOKER$s$0$0$loop.gen)
        at org.jruby.internal.runtime.methods.JavaMethod$JavaMethodZeroBlock.call(JavaMethod.java:497)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:298)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:79)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:83)
        at org.jruby.ir.instructions.CallBase.interpret(CallBase.java:428)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:355)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.Interpreter.INTERPRET_BLOCK(Interpreter.java:132)
        at org.jruby.runtime.MixedModeIRBlockBody.commonYieldPath(MixedModeIRBlockBody.java:148)
        at org.jruby.runtime.IRBlockBody.doYield(IRBlockBody.java:182)
        at org.jruby.runtime.BlockBody.yield(BlockBody.java:111)
        at org.jruby.runtime.Block.yield(Block.java:167)
        at org.jruby.RubyContinuation.enter(RubyContinuation.java:107)
        at org.jruby.RubyKernel.rbCatch19Common(RubyKernel.java:1127)
        at org.jruby.RubyKernel.rbCatch19(RubyKernel.java:1120)
        at org.jruby.RubyKernel$INVOKER$s$rbCatch19.call(RubyKernel$INVOKER$s$rbCatch19.gen)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:348)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:173)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:177)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:332)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:109)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:95)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:298)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:79)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:83)
        at org.jruby.ir.instructions.CallBase.interpret(CallBase.java:428)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:355)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.InterpreterEngine.interpret(InterpreterEngine.java:77)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:144)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:130)
        at org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:192)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:318)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:131)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:339)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.Interpreter.INTERPRET_BLOCK(Interpreter.java:132)
        at org.jruby.runtime.MixedModeIRBlockBody.commonYieldPath(MixedModeIRBlockBody.java:148)
        at org.jruby.runtime.IRBlockBody.doYield(IRBlockBody.java:182)
        at org.jruby.runtime.BlockBody.yield(BlockBody.java:111)
        at org.jruby.runtime.Block.yield(Block.java:167)
        at org.jruby.RubyContinuation.enter(RubyContinuation.java:107)
        at org.jruby.RubyKernel.rbCatch19Common(RubyKernel.java:1127)
        at org.jruby.RubyKernel.rbCatch19(RubyKernel.java:1120)
        at org.jruby.RubyKernel$INVOKER$s$rbCatch19.call(RubyKernel$INVOKER$s$rbCatch19.gen)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:348)
        at org.jruby.runtime.callsite.CachingCallSite.callBlock(CachingCallSite.java:173)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:177)
        at org.jruby.ir.interpreter.InterpreterEngine.processCall(InterpreterEngine.java:332)
        at org.jruby.ir.interpreter.StartupInterpreterEngine.interpret(StartupInterpreterEngine.java:73)
        at org.jruby.ir.interpreter.InterpreterEngine.interpret(InterpreterEngine.java:83)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.INTERPRET_METHOD(MixedModeIRMethod.java:179)
        at org.jruby.internal.runtime.methods.MixedModeIRMethod.call(MixedModeIRMethod.java:165)
        at org.jruby.internal.runtime.methods.DynamicMethod.call(DynamicMethod.java:200)
        at org.jruby.runtime.callsite.CachingCallSite.cacheAndCall(CachingCallSite.java:338)
        at org.jruby.runtime.callsite.CachingCallSite.call(CachingCallSite.java:163)
        at C_3a_.jruby_minus_9_dot_1_dot_8_dot_0.bin.jirb.invokeOther13:start(C:/jruby-9.1.8.0/bin/jirb:13)
        at C_3a_.jruby_minus_9_dot_1_dot_8_dot_0.bin.jirb.RUBY$script(C:/jruby-9.1.8.0/bin/jirb:13)
        at java.lang.invoke.MethodHandle.invokeWithArguments(Unknown Source)
        at org.jruby.ir.Compiler$1.load(Compiler.java:95)
        at org.jruby.Ruby.runScript(Ruby.java:827)
        at org.jruby.Ruby.runNormally(Ruby.java:746)
        at org.jruby.Ruby.runNormally(Ruby.java:764)
        at org.jruby.Ruby.runFromMain(Ruby.java:577)
        at org.jruby.Main.doRunFromMain(Main.java:417)
        at org.jruby.Main.internalRun(Main.java:305)
        at org.jruby.Main.run(Main.java:232)
        at org.jruby.Main.main(Main.java:204)
Caused by: java.lang.ClassCastException: org.bouncycastle.asn1.DLSequence cannot be cast to org.bouncycastle.asn1.ASN1Integer
        at org.jruby.ext.openssl.impl.PKey.readPrivateKey(PKey.java:88)
        at org.jruby.ext.openssl.x509store.PEMInputOutput.readKeyPair(PEMInputOutput.java:1242)
        at org.jruby.ext.openssl.x509store.PEMInputOutput.readPrivateKey(PEMInputOutput.java:325)
        ... 136 more
PKeyRSA could not read private key java.io.IOException: unknown tag 13 encountered
PKeyRSA could not read public key java.io.IOException: unknown tag 13 encountered
PKeyRSA could not generate (PKCS8) private key org.bouncycastle.jcajce.provider.asymmetric.util.ExtendedInvalidKeySpecException: u
nable to process key spec: java.lang.IllegalArgumentException: failed to construct sequence from byte[]: unknown tag 13 encountere
d
PKeyRSA could not generate (X509) public key java.security.spec.InvalidKeySpecException: encoded key spec not recognized: failed t
o construct sequence from byte[]: unknown tag 13 encountered
OpenSSL::PKey::RSAError: Neither PUB key nor PRIV key:
from org/jruby/ext/openssl/PKeyRSA.java:309:in `initialize'
from (irb):3:in `<eval>'
from org/jruby/RubyKernel.java:1000:in `eval'
from org/jruby/RubyKernel.java:1298:in `loop'
from org/jruby/RubyKernel.java:1120:in `catch'
from org/jruby/RubyKernel.java:1120:in `catch'

然后我尝试使用私钥,我已经从PEM文件中删除了-----BEGIN RSA PRIVATE KEY-----和-----END RSA PRIVATE KEY-----,之后我可以在Jruby上执行此操作:

irb(main):005:0> require "base64"
=> true
irb(main):006:0> k = OpenSSL::PKey::RSA.new(Base64.decode64(File.read("private.pem")))
=> #<OpenSSL::PKey::RSA:0x1cff11c>
irb(main):007:0> k.private?
=> true

有没有办法让Jruby OpenSSL自动检测PEM文件格式并加载Base64私钥?这让Base64格式的公钥无法成功打开,而私钥无法成功打开。

共有1个答案

卜昂熙
2023-03-14

好吧,我决定做这个解决方法,不是最好看的代码,但它有效:

class RSA_Base64_Key
  def self.rsa_new key_text
    if key_text.is_a?(String) && key_text.include?('-----BEGIN RSA PRIVATE KEY-----')
      key_text.gsub!('-----BEGIN RSA PRIVATE KEY-----', '')
      key_text.gsub!('-----END RSA PRIVATE KEY-----', '')
      key_text = Base64.decode64(key_text)
    end
    OpenSSL::PKey::RSA.new(key_text)
  end
end

if RUBY_ENGINE == 'jruby'
  RSA_Base64_Key.rsa_new(File.read(key_file))
else
  OpenSSL::PKey::RSA.new(File.read(key_file))
end
 类似资料:
  • 我试图使用Postgis 2.2和Postgreql 9.5与JPA,Postgis 9.5方言。我已经在pom.xml的要求,按这里http://www.hibernatespatial.org/documentation/documentation/和类型导入正确,但是当我试图运行程序使用几何类型我得到这个错误: 我显然遗漏了一些配置,有人能指出是什么吗?

  • 我在MapFragment的布局文件中出现了这个错误 我试过了 > 安装Google Play服务,但仍有错误 - com.google.android.gms.maps.MapFragment(开放类,显示异常,清除缓存) 提示:在自定义视图中使用view.isinEditMode()跳过代码或在IDE中显示示例数据。 如果这是一个意外错误,您也可以尝试构建项目,然后手动刷新布局。 异常详细信息

  • 我正在将一个Java EE应用程序部署到Bluemix,当第一个请求到达时,我得到了这个错误: 2015-05-20T23:11:58.51+0200[app/0]OUT[INFO]FFDC1015I:已创建FFDC事件:“java.util.ServiceConfigurationError:javax.servlet.ServletContainerInitializer:Provider o

  • 我正在尝试将H2设置为内存数据库。我已将其配置为: 然而,当我尝试运行它时,我会遇到这个错误 我不确定它为什么不接受jdbc url,我的confg格式有什么问题吗?

  • 我想使用我的本地代码通过spark-sql连接到远程配置单元。这是我的代码: 请帮帮我.