go - 接收加密/rsa:解密文件时出现解密错误
问题描述
这是我的解密函数,但它不断返回:“crypto/rsa:解密错误”。任何意见将是有益的!我将加密分成几个部分,因为密钥不断引发“密钥太短错误”。我是 golang 加密工作的新手。
func DecryptFile(file string, privateKey *rsa.PrivateKey)([]byte, error){
var decryptedBytes []byte
// read the file into bytes
data, err := ioutil.ReadFile(file)
if err != nil {
return decryptedBytes,err
}
fmt.Println(len(data))
var decryptedByte []byte
ByteSlice := split(data,200)
rng := rand.Reader
for _,bytes := range ByteSlice{
decryptedBytes,err = rsa.DecryptOAEP(
sha256.New(),
rng,
privateKey,
bytes,
nil)
if err != nil {
return decryptedBytes,err
}
decryptedBytes = append(decryptedBytes,decryptedByte...)
}
return decryptedBytes,nil
}
加密功能:
func EncryptFile(file string, PublicKey *rsa.PublicKey)([]byte, error){
var encryptedBytes []byte
// read the file into bytes
data, err := ioutil.ReadFile(file)
if err != nil {
return encryptedBytes,err
}
fmt.Println(len(data))
// Encrypts the file
//fmt.Println(PublicKey.N.BitLen())
//_,_ = strconv.Atoi((PublicKey.N).String())
ByteSlice := split(data,200)
var encryptedByte []byte
rng := rand.Reader
for _,bytes := range ByteSlice{
encryptedByte, err = rsa.EncryptOAEP(
sha256.New(),
rng,
PublicKey,
bytes,
nil)
if err != nil {
return encryptedBytes,err
}
encryptedBytes = append(encryptedBytes, encryptedByte...)
}
// Returns file encrypted
return encryptedBytes,nil
}
解决方案
的步骤rsa.EncryptOAEP()
不应超过:
publicKey.Size() - 2*hash.Size() - 2
您可以publicKey.Size() - 2*hash.Size() - 2
用作步骤 rsa.EncryptOAEP()
无论步长如何,
rsa.EncryptOAEP()
函数总是产生固定长度的加密数据,固定长度为publicKey.Size()
.
所以,步骤rsa.DecryptOAEP()
是:
publicKey.Size()
推荐阅读
- outlook - Outlook 加载项发送功能不调用功能
- asp.net-core - ASP.NET Core 3.1:Razor 页面:RazorPage.cshtml.cs 中的依赖注入
- c# - 如何在位于不同游戏对象的脚本中使用 OnCollision 访问动画师?
- xaml - UWP 或 XAML 导航视图突出显示
- c# - 嵌套集合匹配另一个集合的实体框架核心查询返回错误结果
- c# - 在 GridView 中设置 DropDownList 的选定值
- python - 如何在 Numpy/Python 中对序列进行切片并获取它们的频率
- python - 线性搜索未返回预期输出
- javascript - discord.js v13 | 无法获取目标用户的信息
- javascript - Redux 子应用之间的通信。反应原生