gpt4 book ai didi

R RSA 签名原始到字符并返回

转载 作者:行者123 更新时间:2023-12-02 11:17:06 26 4
gpt4 key购买 nike

所以我正在使用 R PKI 包,感谢 Simon Urbanek,并且正在尝试了解签名消息的应用程序。这样我就可以弄清楚如何签署一些东西。

    require(PKI)

# generate 2048-bit RSA key
key <- PKI.genRSAkey(bits = 2048L)

# extract private and public parts as PEM
priv.pem <- PKI.save.key(key)
pub.pem <- PKI.save.key(key, private=FALSE)
# load back the public key separately
pub.k <- PKI.load.key(pub.pem)

# encrypt with the public key
x <- PKI.encrypt(charToRaw("Hello, world!"), pub.k)
# decrypt with private key
rawToChar(PKI.decrypt(x, key))

# So straight from the Package examples I have the public and private keys.

# Additionally, with the same I can sign a message
x <- charToRaw("My message to sign")
sig <- PKI.sign(x, key)
PKI.verify(x, sig, key)

# Now a slight change from the exapmles I will verify that the public key can verify
PKI.verify(x, sig, pub.k)

pub.pem 可以写入文件

PuK<-paste(pub.pem, collapse="")

稍后可以通过

重建
  pub.pem<-substring(PuK, 
c(1, 27, 91, 155, 219, 283, 347, 411, 419),
c(26, 90,154,218,282,346,410,418,442))
pub.k <- PKI.load.key(pub.pem)

然后再次验证为

PKI.verify(x, sig, pub.k)

但是,sig 是原始的

str(sig)

当它被写入一个文件时,你会得到

sig<-paste(sig, collapse=" " )

但您无法再验证签名,因为它现在是一个字符串而不是原始签名,并且 charToRaw 不会重新创建原始签名。我可以得到其中的一部分,但无法获得格式正确的原始向量来验证签名

sigraw<-rawToChar(sig2, multiple = TRUE)
str(sapply(sigraw, FUN=charToRaw))

有没有办法可以将签名写入文件,然后再次返回以验证签名?

最佳答案

不确定这是问题的最直接答案,但它确实允许将文本字符串写入文件,然后重新格式化。

library("BMS")
library("PKI")
library("pack")

# generate 2048-bit RSA key
key <- PKI.genRSAkey(bits = 2048L)
# extract private and public parts as PEM
priv.pem <- PKI.save.key(key)
pub.pem <- PKI.save.key(key, private=FALSE)
# load back the public key separately
pub.k <- PKI.load.key(pub.pem)
x <- charToRaw("My message to sign")
sig <- PKI.sign(x, key)
PKI.verify(x, sig, key)

bits<-rawToBits(sig)

### This part can be skipped, long way to make character vector & back ###
pbitsb<-paste(bits)
back<-sapply(pbitsb, FUN=as.raw)
back==bits
sigbits<-packBits(bits, type="raw")
sigback<-packBits(back, type="raw")
sig==sigbits&sigbits==sigback
PKI.verify(x,sigback,key)


#Can make this more compressed character vector
hexsig<-bin2hex(as.numeric(bits))

#This is the value to be shared in a text file as proof of signature


#The remaineder needs to be executed by the recipient
binsig<-hex2bin(hexsig)

backbin<-sapply(binsig, FUN=as.raw)
sigbin<-packBits(backbin, type="raw")
PKI.verify(x,sigbin,key)


sig==sigbits&sigbits==sigback&sigback==sigbin

关于R RSA 签名原始到字符并返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29908963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com