登录
首页 >  Golang >  Go问答

转化为 RSA 公钥数据类型

来源:stackoverflow

时间:2024-03-25 12:09:21 169浏览 收藏

为了使用自己的 RSA 公钥进行字符串加密,需要将类型 PublicKey 转换为类型 rsa.PublicKey。这是因为 PublicKey 仅是一个基本接口,而 rsa.PublicKey 是一个派生类型,提供了所需的加密功能。如果不进行转换,密钥类型将不正确,导致代码失败。

问题内容

我正在尝试导入我自己的 rsa 公钥,而不是生成一个来加密字符串。唯一的问题是我在更改为正确的类型时遇到问题。这是我当前的代码:

func main() {
    s := ""
    i := new(big.Int)
    i.SetString(s, 16)

    encrypt(i)
}

func encrypt(i *big.Int) {
    publicKey := PublicKey{N: i, E: 010001}

    secretMessage := "This is super secret message!"

    encryptedMessage := RSA_OAEP_Encrypt(secretMessage, publicKey)

    fmt.Println("Cipher Text:", encryptedMessage)
}

Where it doesn't like publicKey's type

What I'm wondering is if there's a way that I can **convert type PublicKey** to **type rsa.PublicKey** so that I can use it for encryption.

解决方案


密钥似乎有两种类型:一种是基本接口,称为 PublicKey。类型 rsa.PublicKey 派生自该类型,应改为使用。否则密钥类型设置不正确,代码将失败。我认为它创建了一个带有设置值的匿名类型。

到这里,我们也就讲完了《转化为 RSA 公钥数据类型》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>