Error Decrypting Key in R using rsa_decrypt from openssl - r

I'm trying to create a JWT claim for Box in R by following these directions: https://developer.box.com/docs/construct-jwt-claim-manually
I've loaded the config file containing the publickey, privatekey, and passphrase. The second step requires decrypting the private key with the passphrase.
I'm using openssl::rsa_decrypt(data = charToRaw(privateKey), password = passphrase) to do this, but I'm getting this error: Error: OpenSSL error in rsa_ossl_private_decrypt: data greater than mod len
I'm pretty sure I'm doing something wrong with the data = charToRaw(privateKey) argument, but I'm not sure what?

I found out I was using the wrong function.
read_key(privateKey, password = passphrase) is what I was looking for.

Related

How to validate SHA256 hash with a message + nonce?

In R I can generate a 256hash with the following function.
> sha256 ( 'R is great')
[1] "3df43bfb52a031873872284bec472cdd8e1fe8803a6ba936c2446d4d920fd592"
however I know that if I want to get a hash with 4 leading zeros the nounce is
62137 and the final has is 000099bed1e274d13fd4b9864d52ee6f2a8d1f93941f8283b879ba949250b01c
however I want to show this in R. But when I add the nounce to the previous function it does not produce the same hash.
> sha256 ( '000099bed1e274d13fd4b9864d52ee6f2a8d1f93941f8283b879ba949250b01cR is great')
[1] "e1c828213b2463986fe8a5557c4c7b39ea42b792de1bb2a22c50dc2472f7ac6f"
how do I add the nonce to the message to produce the same hash?
source:
https://andersbrownworth.com/blockchain/blockchain
thank you.
I've dug around in the source code of the example. The function getText generates the string that is hashed. getText adds the following elements:
block number
nonce
data
previous hash
In R, you can reproduce it with:
sha256( '262137R is great000015783b764259d382017d91a36d206d0600e2cbb3567748f46a33fe9297cf')
[1] "000099bed1e274d13fd4b9864d52ee6f2a8d1f93941f8283b879ba949250b01c"

R - NiceHash API - HMAC signing issue?

[This is a duplicate post from NiceHash's GitHub in an attempt to broaden the audience. If I find a solution, at least others will also be able to see it in more places too]
Hi all,
I'm a newbie and the only language I know a little of is R.
I've managed to get a number of other APIs working under R using syntax like this:
# Account Stats
response <- httr::GET(
url = "https://API_URL_GOES_HERE",
)
I think I've got the gist of the syntax for R, but I'm having problems with hashing the Secret Key.
My API key / secret works with the "Try it out" link here.
But I get the usual invalid / 2000 error when passing my hashed secret key via the API.
Here's how I'm trying to generate the hashed secret key:
input <- paste0(APIKey," ", XTime," ", XNonce," "," ", OrgID," ", " ", "GET"," ", "/main/api/v2/accounting/accounts2", " ")
# Needs hashed?
APISecret <- hmac(input, "My_Secret_Key", "sha256")
XAuth <- paste0(APIKey, ":", APISecret)
And here's my API call:
# Account Stats
response<- httr::GET(
url = "https://api2.nicehash.com/main/api/v2/accounting/accounts2",
httr::add_headers(
`X-Time` = XTime,
`X-Nonce` = XNonce,
`X-Organization-Id` = OrgID,
#`X-Request-Id` = XNonce,
`X-Auth`= XAuth,
`Content-Type` = "application/json; charset=utf-8"
)
)
My thoughts are:
input is not correctly formatted
HMAC() isn't generating what NiceHash expects
My GET() isn't structured quite right
Any ideas?
EDIT - Link here explains how the input is supposed to be structured.
EDIT - Link here is examples in other languages, which I can't fully translate to R. The code I've shown above is my attempt.
UPDATE - OK, I'm pretty sure it's something to do with the hashing of the input string.
Using this as input:
4ebd366d-76f4-4400-a3b6-e51515d054d6 ⊠ 1543597115712 ⊠ 9675d0f8-1325-484b-9594-c9d6d3268890 ⊠ ⊠ da41b3bc-3d0b-4226-b7ea-aee73f94a518 ⊠ ⊠ GET ⊠ /main/api/v2/hashpower/orderBook ⊠ algorithm=X16R&page=0&size=100
The following string:
fd8a1652-728b-42fe-82b8-f623e56da8850750f5bf-ce66-4ca7-8b84-93651abc723b
when hashed should lead to:
21e6a16f6eb34ac476d59f969f548b47fffe3fea318d9c99e77fc710d2fed798
But I cannot get that as output. I've no idea what to use in place of ⊠ when forming the input. Other language examples use "\x00", but R just errors with:
Error: nul character not allowed (line 1)

Setting the text factory for SQLite in Twisted

I am trying to store binary data in a sqlite database using the Twisted adbapi. However, when I run a query to store the data, I get an error:
sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
After googling a bit, I found the answer for a normal sqlite connection:
con = sqlite3.connect(...)
con.text_factory = str
However, I can't find an equivalent setting to use with a twisted adbapi sqlite connection:
dbpool = adbapi.ConnectionPool("sqlite3", "data.db", check_same_thread=False)
I would appreciate any help!
I figured it out. In order to make changes to the connection after it opens, you have to use the cp_openfun parameter for the ConnectionPool. The following code worked:
def set_text_factory(conn):
conn.text_factory = str
dbpool = adbapi.ConnectionPool("sqlite3", "data.db", check_same_thread=False,
cp_openfun=set_text_factory)

Getting mac from HMAC value and key

I am looking at this page on how to validate HMAC implementation on a platform: http://csrc.nist.gov/groups/STM/cavp/
Test Vectors:
HMAC Test Vectors - These files provide an electronic version of the test vectors
that can be used to informally verify the correctness of an HMAC algorithm
implementation using the HMACVS. However, use of these vectors does not
take the place of validation obtained through the Cryptographic Algorithm
Validation Program (CAVP).
So I open up the file and view the test values:
http://pastebin.com/phJ4C0Fx
it is thousands of lines long but this is the start.
I focus on the first values:
[L=20]
Count = 0
Klen = 10
Tlen = 10
Key = 82f3b69a1bff4de15c33
Msg = fcd6d98bef45ed6850806e96f255fa0c8114b72873abe8f43c10bea7c1df706f10458e6d4e1c9201f057b8492fa10fe4b541d0fc9d41ef839acff1bc76e3fdfebf2235b5bd0347a9a6303e83152f9f8db941b1b94a8a1ce5c273b55dc94d99a171377969234134e7dad1ab4c8e46d18df4dc016764cf95a11ac4b491a2646be1
Mac = 1ba0e66cf72efc349207
My understanding is that with a key and value that openssl would get the mac, however I am not getting the same mac as that above?
echo -n "<Msg here>" | openssl sha1 -hmac "82f3b69a1bff4de15c33"
(stdin)= 981c64f70b07634e01b3800447e6431dddb42530
Any ideas on what I am doing wrong? i am also just guessing sha1, other values don't match either, I don't know how to take from the file what way I should be doing this. The various lengths, and the count. How do I use this information?

CF DESEDE encrypt() Key Length Issue

I am trying to encrypt a string using ColdFusion encrypt() with a 3rd party provided key like this:
encrypteded = encrypt('theString', 'FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF', 'DESEDE/CBC/NoPadding', 'BASE64', ToBase64('0'));
I get:
"The key specified is not a valid key for this encryption: Wrong key algorithm, expected DESede."
What do I have to do to this key in terms of encoding/decoding to get it into the right format?
Generally, when using provided keys from other languages, you have to do a little gymnastics on it to get it into Base64.
Try this for the key argument:
ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex'))
But, to make this work for me, the input string needed to be a multiple of 8 bytes (because you're specifying NoPadding), and the IV needed to also be a multiple of 8 bytes.
So, this ended up working for me - not sure if you'll be able to decrypt it on the other end, tho, if the IV they're specifying is really what you've got listed there.
encrypteded = encrypt('theStrin', ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex')), 'DESEDE/CBC/NoPadding', 'BASE64', ToBase64('0000'));
No IV also worked as well (with different output, obviously):
encrypteded = encrypt('theStrin', ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex')), 'DESEDE/CBC/NoPadding', 'BASE64');
If you've been given a Hex IV, then you can use it as such:
encrypteded = encrypt('theStrin', ToBase64(BinaryDecode('FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF','hex')), 'DESEDE/CBC/NoPadding', 'BASE64', BinaryDecode("7fe8585328e9ac7b","hex"));
Hopefully this is enough info to get you on your way!
The only thing that seems off to me is the algorithm value you're using. Maybe try this?
encrypteded = encrypt('theString', 'FD52250E230D1CDFD5C2DF0D57E3E0FEFD52250E230D1CDF', 'DESEDE', 'BASE64', ToBase64('0'));
I don't know if the /CBC/NoPadding settings will be what you want, but I don't think they will be allowed in the algorithm argument.

Resources