R RSA signature raw to character and back -


so using r pki package, simon urbanek, , trying understand application of signing message. can figure out how sign something.

    require(pki)  # generate 2048-bit rsa key key <- pki.genrsakey(bits = 2048l)  # extract private , public parts pem priv.pem <- pki.save.key(key) pub.pem <- pki.save.key(key, private=false) # load public key separately pub.k <- pki.load.key(pub.pem)  # encrypt public key x <- pki.encrypt(chartoraw("hello, world!"), pub.k) # decrypt private key rawtochar(pki.decrypt(x, key))  # straight package examples have public , private keys.  # additionally, same can sign message x <- chartoraw("my message sign") sig <- pki.sign(x, key) pki.verify(x, sig, key)  # slight change exapmles verify public key can verify pki.verify(x, sig, pub.k) 

pub.pem can written file as

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

and can later reconstructed via

  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) 

and verified again

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

however, sig raw

str(sig) 

and when written file get

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

but can no longer verify signature string , not raw , chartoraw not recreate original signature. can part of way there not correctly formatted raw vector verify signature

sigraw<-rawtochar(sig2, multiple = true) str(sapply(sigraw, fun=chartoraw)) 

so there way can write signature file , again verify signature?

not sure direct answer question allow text sting can written file , reformatted.

library("bms") library("pki") library("pack")  # generate 2048-bit rsa key key <- pki.genrsakey(bits = 2048l) # extract private , public parts pem priv.pem <- pki.save.key(key) pub.pem <- pki.save.key(key, private=false) # load public key separately pub.k <- pki.load.key(pub.pem) x <- chartoraw("my message sign") sig <- pki.sign(x, key) pki.verify(x, sig, key)  bits<-rawtobits(sig)  ###  part can skipped, long way make character vector &  ### 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 more compressed character vector hexsig<-bin2hex(as.numeric(bits))  #this value shared in text file proof of signature   #the remaineder needs executed 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