Learn R Programming

PKI (version 0.1-14)

PKI.crypt: PKI encryption/decryption functions

Description

PKI.encrypt encrypts a raw vector

PKI.decrypt decrypts a raw vector

Usage

PKI.encrypt(what, key, cipher = NULL, iv = NULL)
PKI.decrypt(what, key, cipher = NULL, iv = NULL)

Value

Raw vector (encrypted/decrypted)

Arguments

what

raw vector to encrypt/decrypt. It must not exceed the key size minus padding

key

key to use for encryption/decryption

cipher

cipher to use for encryption/decryption

iv

initialization vector for ciphers that use it (e.g., CBC). NULL corresponds to all-zeroes IV, otherwise must be either a string or a raw vector with sufficiently many bytes to match the IV length for the cipher.

Author

Simon Urbanek

See Also

PKI.genRSAkey, PKI.pubkey

Examples

Run this code
  key <- PKI.genRSAkey(2048)
  x <- charToRaw("Hello, world!")
  e <- PKI.encrypt(x, key)
  y <- PKI.decrypt(e, key)
  stopifnot(identical(x, y))
  print(rawToChar(y))

  ## AES symmetric - use SHA256 to support arbitrarily long key strings
  key <- PKI.digest(charToRaw("hello"), "SHA256")
  ae <- PKI.encrypt(x, key, "aes256")
  ae
  ad <- PKI.decrypt(ae, key, "aes256")
  stopifnot(identical(x, ad))

Run the code above in your browser using DataLab