How to enter key manually using R's cyphr library

Viewed 38

Suppose I create a key using R's cyphr library

library("cyphr")

k <- sodium::keygen()
# > k
# [1] af ee af 08 5b 75 42 7b 83 05 67 69 82 66 55 67 ea ee 04 76 90 cb b2 19 49 11 15 ab 53 5f e9 aa

key <- cyphr::key_sodium(k)
key
# > key
# <cyphr_key: sodium>

This seems to work OK.

Now suppose I never store this key in a file anywhere, but write it down on a piece of paper. When it comes time to decrypt a file I manually enter the key like so to try to recreate the sodium cyphr key object:

k <- "af ee af 08 5b 75 42 7b 83 05 67 69 82 66 55 67 ea ee 04 76 90 cb b2 19 49 11 15 ab 53 5f e9 aa"
key <- cyphr::key_sodium(noquote(k))

# Error in file(con, "rb") : cannot open the connection
# In addition: Warning message:
#   In file(con, "rb") :
#   cannot open file 'af ee af 08 5b 75 42 7b 83 05 67 69 82 66 55 67 ea ee 04 76 90 cb b2 19 49 11 15 ab 53 5f e9 aa': No such file or directory

Why doesn't this work? Can it work?

1 Answers

One option is split the string at the space with strsplit, loop over the list of values, convert to raw through hexmode

k <- unname(sapply(strsplit(k1, " ")[[1]], function(x) as.raw(as.hexmode(x))))

-checking

k
#[1] af ee af 08 5b 75 42 7b 83 05 67 69 82 66 55 67 ea ee 04 76 90 cb b2 19 49 11 15 ab 53 5f e9 aa
str(k)
#raw [1:32] af ee af 08 ...

-use as input

key <- key_sodium(k)
key    
#<cyphr_key: sodium>

Regarding why noquote didn't work, it is because it is still a character class overrided with noquote and not raw

str(noquote(k1))
# 'noquote' chr "af ee af 08 5b 75 42 7b 83 05 67 69 82 66 55 67 ea ee 04 76 90 cb b2 19 49 11 15 ab 53 5f e9 aa"

class(k)
#[1] "raw"
class(noquote(k1))
#[1] "noquote"

We need the class to be raw

data

k1 <-  "af ee af 08 5b 75 42 7b 83 05 67 69 82 66 55 67 ea ee 04 76 90 cb b2 19 49 11 15 ab 53 5f e9 aa"
Related