【发布时间】:2018-02-13 06:50:06
【问题描述】:
我有一个使用 RSA 加密的 Golang 项目,所以现在,我有一个用于加密消息的 Base64 公钥格式,
我使用了这个代码:
publicKeyBase64 = "MIGJAoGBAJJYXgBem1scLKPEjwKrW8+ci3B/YNN3aY2DJ3lc5e2wNc0SmFikDpow1TdYcKl2wdrXX7sMRsyjTk15IECMezyHzaJGQ9TinnkQixJ+YnlNdLC04TNWOg13plyahIXBforYAjYl2wVIA8Yma2bEQFhmAFkEX1A/Q1dIKy6EfQ+xAgMBAAE="
publicKeyBinary, err := base64.StdEncoding.DecodeString(publicKeyBase64)
publicKeyInterface, err := x509.ParsePKIXPublicKey(publicKeyBinary)
if err != nil {
fmt.Println("Could not parse DER encoded public key (encryption key)")
return "","",err
}
publicKey, isRSAPublicKey := publicKeyInterface.(*rsa.PublicKey)
if !isRSAPublicKey {
fmt.Println("Public key parsed is not an RSA public key")
return "","",err
}
encryptedMessage, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, "message")
当我运行这段代码时,我得到了这个错误:
Could not parse DER encoded public key (encryption key)
asn1: structure error: tags don't match (16 vs {class:0 tag:2 length:129 isCompound:false}) {optional:false explicit:false application:false defaultValue:<nil> tag:<nil> stringType:0 timeType:0 set:false omitEmpty:false} AlgorithmIdentifier @3
错误指向publicKeyInterface,它无法从Base64解码格式解析为公钥,我的代码有什么问题?
=======更新=====
我的 publicKeyBase64 是从我的二进制数据类型的模型中检索到的
当我从我的 Rails API 将它存储在我的 mongoDB 中时,我收到了 Base64 格式的 public_key 参数,但我将它解码为二进制,然后我用这个代码存储它
def create
params = device_params
public_key = Base64.decode64 device_params[:public_key]
#device_params[:public_key] value is "MIGJAoGBAJJYXgBem1scLKPEjwKrW8+ci3B/YNN3aY2DJ3lc5e2wNc0SmFikDpow1TdYcKl2wdrXX7sMRsyjTk15IECMezyHzaJGQ9TinnkQixJ+YnlNdLC04TNWOg13plyahIXBforYAjYl2wVIA8Yma2bEQFhmAFkEX1A/Q1dIKy6EfQ+xAgMBAAE="
params[:public_key] = BSON::Binary.new(public_key, :generic)
device = Device.find_or_create_by(id: device_params[:id])
render_success device.update_attributes(params), device
end
当我使用 Rails 代码使用此代码转换我的 Base64 公钥字符串时,它成功了:
rsa_public_key = OpenSSL::PKey::RSA.new(Base64.decode64(public_key))
在我的 iOS 应用程序中,我使用 https://github.com/DigitalLeaves/AsymmetricCrypto 使用此代码生成公钥:
AsymmetricCryptoManager.sharedInstance.createSecureKeyPair({ (success, error) -> Void in
if success {
print("RSA-1024 keypair successfully generated.")
let publicKey = AsymmetricCryptoManager.sharedInstance.getPublicKeyData()?.base64EncodedString()
let url = ENV.BASE_URL + "devices"
let headers = ["Authentication-Token": CurrentUser.getCurrentUser().token] as! HTTPHeaders
let params = ["device[user_id]": CurrentUser.getCurrentUser().id!, "device[id]": instanceID,"device[token]": fcmToken, "device[os]": "ios", "device[public_key]": publicKey!]
Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: headers)
} else { print("An error happened while generating a keypair: \(error)") }
})
【问题讨论】:
-
你能展示生成base64密钥的代码吗?
-
我从我的模型中检索到它实际上是二进制数据数据类型
publicKeyBase64 := base64.StdEncoding.EncodeToString(publicKey.Data) -
什么是
Data?这不是rsa.PublicKey的字段。 -
它来自我的结构
PublicKey bson.Binary -
好吧,这仍然没有帮助。它最初来自哪里?是 pem 块中的
block.Bytes吗?如果不是,您可能存储了错误的东西,或者试图以错误的方式对其进行解码。现在,我们只知道它是 base64。
标签: ios go swift3 rsa public-key-encryption