【问题标题】:R RSA signature raw to character and backR RSA签名原始到字符并返回
【发布时间】:2015-04-29 04:20:24
【问题描述】:

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

    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))

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

【问题讨论】:

    标签: r rsa type-conversion signature pki


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2012-02-19
      • 1970-01-01
      • 2019-11-02
      • 1970-01-01
      • 2023-04-05
      • 2013-11-29
      • 2011-03-05
      • 2012-01-29
      • 1970-01-01
      相关资源
      最近更新 更多