I'm trying to generate a private key and public key pair. I want to use the private key to sign my JWT and send the public key to a 3rd party to decode my JWT.
On my mac os terminal, I generated my keys like this:
ssh-keygen -m PEM -t rsa -b 2048
Do now I've pkey & pkey.pub as private and public keys respectively. Now in my rails console I tried to get the private key like this, which works fine:
rsa_private = OpenSSL::PKey::RSA.new(File.read("/path/to/private/key/pkey"))
rsa_private.to_s
"-----BEGIN RSA PRIVATE KEY-----\nCONTENTS_OF_PKEY_FILE\n-----END RSA PRIVATE KEY-----\n"
Now in rails, I can get the public key from generated private key like this :
pub_key = rsa_private.public_key
But when I try to print it's contents, it differs from what's there in pkey.pub generated when I ran ssh-keygen command.
pub_key looks something like this:
"-----BEGIN PUBLIC KEY-----\nSOME_CONTENT\n-----END PUBLIC KEY-----\n"
But my pkey.pub file looks different, something like this:
ssh-rsa SOME_OTHER_CONTENT user#user.local
So, my question, is how can I get 2 different public keys for same private key? Which one do I use to decode my JWT?
Keys should be the same, but are encoded differently.
An RSA key pair consists of several numbers, private key is all these data, public key is the same with private part deleted.
Numbers can be written in different order, encoded in different formats. Also keys can be encrypted for storage with a password(not this case). Thus files with the same keys can look absolutely different.
PEM-format (the one with BEGIN PUBLIC KEY and Base64 data) is more common for generic keys, so better use it.
Update:
PEM format is base64 encoded DER with header and footer. DER in its turn is binary representation of ASN.1. But SSH uses a dirrerent key encoding format (RFC4716)
Example:
% ssh-keygen -m PEM -t rsa -N '' -b 1024 -f ./rsa
% cat rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDRNFYxsULk6x90T0EE8iS3skfJJ407ef3WJJClre0k2sLJUJX6/Xbc3ObxNjixXcgIXp2H4oVOnNpujqFF/XM81zlpLjGT/4igtK1FjIHIaFyRheGuwplgwCkXlxAe/oH1Bb4nFXlD/kORmGgSfSE9BpH+HQU3IzyU1i0X9K828Q== vasfed#Vasiliys-MacBook-Pro.local
% ssh-keygen -e -m PEM -f ./rsa.pub
-----BEGIN RSA PUBLIC KEY-----
MIGJAoGBANE0VjGxQuTrH3RPQQTyJLeyR8knjTt5/dYkkKWt7STawslQlfr9dtzc
5vE2OLFdyAhenYfihU6c2m6OoUX9czzXOWkuMZP/iKC0rUWMgchoXJGF4a7CmWDA
KReXEB7+gfUFvicVeUP+Q5GYaBJ9IT0Gkf4dBTcjPJTWLRf0rzbxAgMBAAE=
-----END RSA PUBLIC KEY-----
% ssh-keygen -e -m PKCS8 -f ./rsa.pub
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDRNFYxsULk6x90T0EE8iS3skfJ
J407ef3WJJClre0k2sLJUJX6/Xbc3ObxNjixXcgIXp2H4oVOnNpujqFF/XM81zlp
LjGT/4igtK1FjIHIaFyRheGuwplgwCkXlxAe/oH1Bb4nFXlD/kORmGgSfSE9BpH+
HQU3IzyU1i0X9K828QIDAQAB
-----END PUBLIC KEY-----
Above is 3 different encodings of the same key, here are ASN-decoded versions of the latter two (via https://lapo.it/asn1js):
PEM, the bare minimum (modulus and exponent, no metadata):
SEQUENCE (2 elem)
INTEGER (1024 bit) 146908353891476107599563957703741990254320034409224509383359005248419…
INTEGER 65537
PKCS8, here we see the exact same numbers, but this time with some metadata:
SEQUENCE (2 elem)
SEQUENCE (2 elem)
OBJECT IDENTIFIER 1.2.840.113549.1.1.1 rsaEncryption (PKCS #1)
NULL
BIT STRING (1 elem)
SEQUENCE (2 elem)
INTEGER (1024 bit) 146908353891476107599563957703741990254320034409224509383359005248419…
INTEGER 65537
Related
I am trying to import a backup of a Firebase Real-Time Database from Google Cloud Storage into BigQuery and getting the following error:
Invalid field name "Name ". Fields must contain only letters, numbers, and underscores, start with a letter or underscore, and be at most 128 characters long. Table: tabletest
I have tried a second dataset that returns the following error:
Invalid field name "-Kq4_0dsRwKfOGGxGoQv". Fields must contain only letters, numbers, and underscores, start with a letter or underscore, and be at most 128 characters long. Table: tabletest
This is the second dataset:
{"users":{"someUserID_abc":{"tests":{"-Kq4_0dsRwKfOGGxGoQv":"07/02/2019 19:44:22","-Kq4_vUQTQ3b6gqMkfRL":"07/02/2019 19:48:20","-Kq4a84n9WMu3NGiE4qW":"07/02/2019 19:53:36"}}}}
In my initial (very large) dataset, there are lots of unique firebase keys that were uniquely generated by firebase and usually start with "-" (which seems to create the error with BigQuery).
My settings on the Create table screen are:
Source
Create table from: Google Cloud Storage
Select file from GCS bucket: myproject-backups/2019-07-03T02:23:34Z_myproject_data.json.gz
File format: JSON (Newline delimited)
Destination
Project name: myproject
Dataset name: database
Table type: Native table
Table name: tabletest
Schema
Auto detect
(checked) Schema and input parameters
enter code here
After I click the "Create table" button on the "Create table" screen, I get the error above.
I have tried different dataset and table names, adjusted the table type, looked through each of the following for answers without any luck:
https://github.com/metabase/metabase/issues/4087
https://cloud.google.com/bigquery/docs/schemas
https://github.com/bxparks/bigquery-schema-generator/issues/14
https://github.com/metabase/metabase/pull/4707
https://github.com/metabase/metabase/issues/3997
loading data from a datastore backup into big query throws invalid field name error
Based on here, I am guessing the error is that "Name" has a space in it (and might be from the database?) and I have lots of random keys generated by firebase (which start with a leading "-"). It's a very large dataset, so I can't even unzip and open the initial gzip file without the program freezing (even plain text editor).
EDIT:
I created the following script to gunzip, "clean" the data for all keys in the .gzip, and re-gzip the file:
# remove spaces, replace leading dashmarks (replace - with the word 'dashmark'), add an underscore before numbers
file=$"myfile.json.gz"
gunzip "$file"
sed -e "s/Name /Name/g" -e "s/-/dashmark/g" -e "s/{\"\([0-9]+\)/{\"_\1/g" -e "s/,\"\([0-9]+\)/,\"_\1/g" -e "s/,\"\"/\,\"_\"/g" -e "s/{\"\"/\{\"_\"/g" < "${file%.gz}" | gzip -c > "${file%.gz}.gz"
This seems to eliminate the "Invalid field name..." errors, but introduces the following error:
Error while reading data, error message: Failed to parse JSON:
Unexpected token; Could not parse value; Could not parse value; Could
not parse value; Could not parse value; Could not parse value; Could
not parse value; Could not parse value; Could not parse value; Could
not parse value; Could not parse value; Could not parse value; Could
not parse value; Parser terminated before end of string
I am admittedly new to sed statements, so perhaps I typo'd or miswrote script in a way that caused an invalid JSON object to be created in my attempt?
Does anyone know how to remove spaces/special characters from all keys in the .gzip (or any other way to resolve this error to import the Firebase RTD .gzip into BigQuery)?
If you copied the error message exactly, it appears that there is a space or some other whitespace character in the string "Name ". That whitespace character is invalid, as the error message is telling you. You'll have to dig through your data to figure out where exactly that invalid character is coming from.
This error can also be printed when you run
bq mk --table --schema xyz.json
And the JSON file is not present in the folder from which you ran the command.
BigQuery error in mk operation: Invalid field name "xyz.json". Fields must contain only letters, numbers, and underscores, start with
a letter or underscore, and be at most 300 characters long.
It's file not found but the error makes you think there's a problem in your schema JSON.
when I try to encrypt data from terminal like
echo -n "TestData" | openssl enc -aes-256-cbc -a -K C81E728D9D4C2F636F067F89CC14862C65990ABE58735B91B6B8798E8CE45F22 -iv D342F9C6310F6B21E97AB38595BD8CAA
than the Base64 encoded result I receive is
VJwJBTtVntJvRGkD24S4wg==
But when I try same thing with rails using exactly same key and initialization vector
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.encrypt
cipher.key = "C81E728D9D4C2F636F067F89CC14862C65990ABE58735B91B6B8798E8CE45F22"
cipher.iv = "D342F9C6310F6B21E97AB38595BD8CAA"
encrypted_data = cipher.update("TestData")
encrypted_data << cipher.final
Base64.strict_encode64(encrypted_data)
than I receive entirely different Base64 encoded result
qavpNrU7llgauAyyEZz/bw==
can someone point that what I missed?
You provide the key and iv attributes as hex strings, but the expected format is raw bytes. Converting them to binary yields the expected result, with the following script:
require 'openssl'
require 'base64'
def hex_to_bin(s)
s.scan(/../).map { |x| x.hex.chr }.join
end
cipher = OpenSSL::Cipher::Cipher.new('aes-256-cbc')
cipher.encrypt
cipher.key = hex_to_bin("C81E728D9D4C2F636F067F89CC14862C65990ABE58735B91B6B8798E8CE45F22")
cipher.iv = hex_to_bin("D342F9C6310F6B21E97AB38595BD8CAA")
encrypted_data = cipher.update("TestData")
encrypted_data << cipher.final
puts Base64.strict_encode64(encrypted_data)
(Source for the hex_to_bin function: To Hex and Back (With Ruby)).
Calling it encrypt.rb, this is the result of running it:
$ ruby encrypt.rb
encrypt.rb:8: warning: constant OpenSSL::Cipher::Cipher is deprecated
VJwJBTtVntJvRGkD24S4wg==
To get rid of the "is deprecated" warning I had to replace the deprecated class OpenSSL::Cipher::Cipher with OpenSSL::Cipher.
The key is to short, 98304A2480DDC0FA354278936DAC2A0D7D9074650AD6 is an invalid key size, AES keys are 128, 192 or 256 bits in length (16, 24 or 32 bytes). Since it appears the key should be 256-bits (32-bytes) the missing key bytes will be either garbage or possibly nulls, key extension is undefined. Thus different results.
Assuming null padding and PKCS#7 padding for the first case the result is correct:
AESCALC
The second example is filling out the key in some other manor.
The solution is to use a full length key.
I need to generate RSA Public Key from Modulus and Exponent. I am new about Swift so I searched about that. I found these:
Generate RSA Public Key from Modulus and Exponent
Generating public key from modulus and exponent
https://meniny.cn/posts/RSA_public_key_with_modulus_and_exponent/
But this suggestions are not compiled with Swift 4. there are so much errors. Also I found this : https://forums.developer.apple.com/thread/4866
it said "iOS does not provide any public APIs for this". so how can I do this ?
Assemble the following information:The full URL you wish to call, including any path and query parameters.Your Consumer ID (for example, 9a4d7659-100c-4d5e-a6b0-26faad4c9132).Your Base 64-encoded Private Key.Your request method in all capitals (for example, GET).Construct input for the signature (NOTE: The order of the parameters and the line returns \n are important to generate the signature properly):the Consumer ID issued to you_ + "\n" + the url of the call you are making + "\n" + the request method of the call you are making in all capitals + "\n" + the Epoch timestamp now (in milliseconds since Jan 01 1970 UTC) + "\n"Sign the byte array representation of this data by:Decoding the Base-64, PKCS-8 representation of your Private Key. Note that the key is encoded using PKCS-8. Libraries in various languages offer the ability to specify that the key is in this format and not in other conflicting formats such as PKCS-1.Use this byte representation of your key to sign the data using SHA-256 with RSA.Encode the resulting signature using Base-64.Use the generated signature and the timestamp to make your API call.
I uploaded the new version of my app without any problems. Validation was successful. About 2 minutes after the upload I got a mail with the following content:
Dear developer,
We have discovered one or more issues with your recent delivery for "-AppName-". To process your delivery, the following issues must be corrected:
Invalid Bundle - Failed to read property list: unrecognized option: -AppName-.app/Info.plist plutil: [command_option] [other_options] file... The file '-' means stdin Command options are (-lint is the default): -help show this message and exit -lint check the property list files for syntax errors -convert fmt rewrite property list files in format fmt is one of: xml1 binary1 json -insert keypath -type value insert a value into the property list before writing it out keypath is a key-value coding key path, with one extension: a numerical path component applied to an array will act on the object at that index in the array or insert it into the array if the numerical path component is the last one in the key path type is one of: bool, integer, float, date, string, data, xml, json -bool: YES if passed "YES" or "true", otherwise NO -integer: any valid 64 bit integer -float: any valid 64 bit float -string: UTF8 encoded string -date: a date in XML property list format, not supported if outputting JSON -data: a base-64 encoded string -xml: an XML property list, useful for inserting compound values -json: a JSON fragment, useful for inserting compound values value YES, NO, a number, a date, or a base-64 encoded blob of data -replace keypath -type value same as -insert, but it will overwrite an existing value -remove keypath removes the value at 'keypath' from the property list before writing it out -extract keypath fmt outputs the value at 'keypath' in the property list as a new plist of type 'fmt' fmt is one of: xml1 binary1 json -p print property list in a human-readable fashion (not for machine parsing! this 'format' is not stable) There are some additional optional arguments that apply to the -convert, -insert, -remove, -replace, and -extract verbs: -s be silent on success -o path specify alternate file path name for result; the -o option is used with -convert, and is only useful with one file argument (last file overwrites); the path '-' means stdout -e extension specify alternate extension for converted files -r if writing JSON, output in human-readable form -- specifies that all further arguments are file names ...
Once these issues have been corrected, you can then redeliver the corrected binary.
Regards,
The App Store team
This is only an update upload. In the past it works with prior versions of Xcode without problems. My actual version of Xcode is 7.0 (7A220). On test devices the app also runs without any problems.
I hope to find a solution this way! Thank you for your time,
kind regards!
Looks to me like you are confusing plutil that Apple must be running on your app as part of the validation process.
This is because the app bundle contains - characters which plutil thinks is a command line option (hence the help text it is displaying).
I can reproduce this on my machine (note: I have to use -- with mkdir and touch in order to avoid confusing them too):
$ cd ~/tmp
$ mkdir -- -AppName-.app
$ touch -- -AppName-.app/Info.plist
$ plutil -lint -AppName-.app/Info.plist
unrecognized option: -AppName-.app/Info.plist
plutil: [command_option] [other_options] file...
The file '-' means stdin
Command options are (-lint is the default):
-help show this message and exit
-lint check the property list files for syntax errors
-convert fmt rewrite property list files in format
fmt is one of: xml1 binary1 json
-insert keypath -type value insert a value into the property list before writing it out
keypath is a key-value coding key path, with one extension:
a numerical path component applied to an array will act on the object at that index in the array
or insert it into the array if the numerical path component is the last one in the key path
type is one of: bool, integer, float, date, string, data, xml, json
-bool: YES if passed "YES" or "true", otherwise NO
-integer: any valid 64 bit integer
-float: any valid 64 bit float
-string: UTF8 encoded string
-date: a date in XML property list format, not supported if outputting JSON
-data: a base-64 encoded string
-xml: an XML property list, useful for inserting compound values
-json: a JSON fragment, useful for inserting compound values
value YES, NO, a number, a date, or a base-64 encoded blob of data
-replace keypath -type value same as -insert, but it will overwrite an existing value
-remove keypath removes the value at 'keypath' from the property list before writing it out
-extract keypath fmt outputs the value at 'keypath' in the property list as a new plist of type 'fmt'
fmt is one of: xml1 binary1 json
-p print property list in a human-readable fashion
(not for machine parsing! this 'format' is not stable)
There are some additional optional arguments that apply to the -convert, -insert, -remove, -replace, and -extract verbs:
-s be silent on success
-o path specify alternate file path name for result;
the -o option is used with -convert, and is only
useful with one file argument (last file overwrites);
the path '-' means stdout
-e extension specify alternate extension for converted files
-r if writing JSON, output in human-readable form
-- specifies that all further arguments are file names
Solution: Give the app bundle a better name (one that doesn't contain - characters and describes the app better).