Generate a private key from public key - algorithm

I am try to generate a private key from rsa public key. My attempts to use the formulas from this solution were unfortunately unsuccessful, because the codes never matched. My target is to calculate an RSA 1024 bit private key from this public key.
Edit: I have now added the public key, because the previous one was an entire certificate.
-----BEGIN PUBLIC KEY-----
MIGdMA0GCSqGSIb3DQEBAQUAA4GLADCBhwKBgQD1cJDUjO87H/eM+VxtUxB5gHIH
W2Q1wniS6kxXPzaEnPPzaQflsEHOV2mrCnXd2qsa9Soqpl5U0x3OlzjXnaYRfck7
0BsppFRnEFfhPFmXOKxv4pCx30O9O+yAD69Z8h+OGJtL2DQfPpcTuOxxwYS77BfG
rSymPSF0gLFV1mn4AwIBAw==
-----END PUBLIC KEY-----

Related

Laravel's Cashier-Paddle Not Working Locally?

I've tried to use both ngrok and Expose with the Paddle Sandbox and while the subscription is being created perfectly in Paddle, on the Laravel side, I keep getting the same error:
[2022-05-08 23:23:50] local.ERROR: openssl_verify(): supplied key
param cannot be coerced into a public key {"exception":"[object]
(ErrorException(code: 0): openssl_verify(): supplied key param cannot
be coerced into a public key at
~/vendor/laravel/cashier-paddle/src/Http/Middleware/VerifyWebhookSignature.php:71)
Is there a particular format the public key in .env needs to be in to prevent this error?
Use like this ( no need use -----BEGIN PUBLIC KEY----- and -----END PUBLIC KEY-----
"MIICIjANBiuqhiiG9w0BAQEFXAOCAg8AMIIjjgKCAraAyj/UyC89sqpOnpEZcM76
guppK9vfF7balLj87rE9VXq5...EAAQ=="
I figured this one out. It turns out that when you're putting your PADDLE_PUBLIC_KEY in .env, you should include everything i.e.
PADDLE_PUBLIC_KEY="-----BEGIN PUBLIC KEY-----
MIICIjANBiuqhiiG9w0BAQEFXAOCAg8AMIIjjgKCAraAyj/UyC89sqpOnpEZcM76
guppK9vfF7balLj87rE9VXq5...EAAQ==
-----END PUBLIC KEY-----"

Ruby OpenSSL: How to fetch just token from public key

I am using OpenSSL::PKey::RSA.new(2048) to create public and private key..
privae_key = OpenSSL::PKey::RSA.new(2048)
public_key = private_key.public_key
Below is my public key
puts public_key.class
=> OpenSSL::PKey::RSA
puts public_key
=> -----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAywjG1X8f87fAg7yW/u7V
wPxOoR/yVAs87ew0rpcWuSKClVF8sXSuGI9LPXDVFIrBegw4zVI6PBAdKWqw6Ogv
y3KwHGD3z6AtwzQzUHvkT74zkSKcpGMSqITwAzUOjX0JbJSH0n3YBhekg3og6A+G
TxZDivoa3VPsN+hoW7bvai4dUDbCpeDbahLgPggVe9mc/jNc2+Ozf79lROgC4q9P
cpyQi7e4qHsZfptNU9lE3a69fI23O2MhGNtu1+ke2D+GL8whXC66pXctnhPD+3+5
OC7x/dFaN7OIv44rYc7udc7lwEtHhfKIgVP8mIYai+cJMfq4G+Ip/nn5xwihCkUt
4QIDAQAB
-----END PUBLIC KEY-----
from the above public key i want to extract only key portion i.e i want to extract only below data from above public key in the form of string. i want to ignore header -----BEGIN PUBLIC KEY----- and footer -----END PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAywjG1X8f87fAg7yW/u7V
wPxOoR/yVAs87ew0rpcWuSKClVF8sXSuGI9LPXDVFIrBegw4zVI6PBAdKWqw6Ogv
y3KwHGD3z6AtwzQzUHvkT74zkSKcpGMSqITwAzUOjX0JbJSH0n3YBhekg3og6A+G
TxZDivoa3VPsN+hoW7bvai4dUDbCpeDbahLgPggVe9mc/jNc2+Ozf79lROgC4q9P
cpyQi7e4qHsZfptNU9lE3a69fI23O2MhGNtu1+ke2D+GL8whXC66pXctnhPD+3+5
OC7x/dFaN7OIv44rYc7udc7lwEtHhfKIgVP8mIYai+cJMfq4G+Ip/nn5xwihCkUt
4QIDAQAB
Is there a way to do this? i tried all methods supported on object public_key but nothing is working.. can someone help?
You can export the key to a string with to_pem(). This string contains the PEM encoded key, i.e. header, Base64 encoded body with line breaks, and footer. Header and footer can then be removed with sub():
require "openssl"
private_key = OpenSSL::PKey::RSA.new(2048)
public_key = private_key.public_key
pem = public_key.to_pem
pem.sub! "-----BEGIN PUBLIC KEY-----\n", ''
pem.sub! "\n-----END PUBLIC KEY-----", ''
#pem.gsub! "\n", '' # remove all remaining linebreaks
puts pem.class
puts pem
Possible output:
String
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmTbDL1Tqz83QPvQ8cbFp
GWIXk5ukTX0E18PHY//3GaSF0xMLGBx6KeGprCpJbhUGcxFQi2/Hdr9BAjaqDwF6
89Bo2CHW5wU4HL0IgcahrfcpZGlggMEbEpKH6boppt6XZuppAAkxBprHVkJ951Ve
TK3tqpO8i4x9t5JuJteSbb67ts2IOXbi/YGUOassby3y4Q286YCYZh5VXHMLdKKJ
qHgICUn1dlAMI7ie0n4s1ESnkqo9GXgWAy3WrJTUsX/FK97+8h+b1aa0qnHbwWpp
dxNxExdUUEgBGHKz4piVwTJ9gfQu4hSQpcVstUgjDx6qTo9HSu4iQi1FPKxZG0lN
xwIDAQAB
Alternatively, the key can be exported with to_der() and the result Base64 encoded.

Error : spring.cloud.config.server.git.privateKey' is not a valid private key

I am trying to get my spring cloud config sever to connect to bitbucket. But getting the following error
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.cloud.config.server.git' to org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties failed:
Reason: Property 'spring.cloud.config.server.git.privateKey' is not a valid private key
Now I read that to generate the key in the correct format. I need to run the following
ssh-keygen -m PEM -t rsa -b 4096
It creates two keys a public and a private. The public
ssh-rsa xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I added to BitBucket personal settings SSH key
The private to my yml file.
cloud:
config:
server:
git:
# uri: ${user.home}\dev\workdev\Fetcher\SpringCloudConfig\hp-fecher-configrepo
uri: git#bitbucket.org:myapp/configurations.git
default-label: main
clone-on-start: true
ignore-local-ssh-settings: true
privateKey :
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
Is the problem that I generated the key on windows 10.
Is the error message correct, is the key valid, but there is another error that is throwing this error.
A very basic mistake. I forgot to add a pipe |
privateKey : |
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----
Also remove clone-on-start: true
cloud:
config:
server:
git:
uri: git#bitbucket.org:myapp/configurations.git
default-label: main
ignore-local-ssh-settings: true
privateKey :
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

how does private key contain modulus?

In RSA, we got public/private key.
let's say I have a private key. How can I get public key ? to get public key, i would need to know private key pairs(q and N), but private key seems one long thing only.

OpenSSL RSA Public Key encryption in Java in Server and decryption with Private Key in Client in OSX

I have an application which is client/server based. The client is developed for both iOS and OSX. The server is common for both the clients (and server's implementation cannot be changed).
The client developed in iOS generates an RSA public/private key pair and send the public key in base64 encoding to the server. The server (implemented in Java) encrypts some secure data and sends the same to the client who is required to decrypt data with the private key. The RSA key pair is generated on iOS using the Crypto Exercise Sample provided by apple.
The public and private key generated (of key length 1024) in base64Encoding is of length 216 and 844 bytes respectively and of the form provided below.
Private Key:
MIMAAnQwDQYJKoZIhvcNAQEBBQAkabeCYAAwggJbAgEAAoGBANDV3P+17zCIw/ZIwjM/5q7DeEEi4AVYE2STPnbuApvc0JjC3gx+F+mLtCfR+lxi0TAWqdFK6MkjGzyKONcEHRWxA/7ltFC1RlgEWzxmdr4kOEL5DV6DRep4Ykh4guvGf3A4N1A87com0rSjOoWR++N9HCmGxrnEhgV7gb9wknoRAgMBAAECgYAS+pyvEJXAT22fwFUF21TXpSQUp1q8oZiBl3Ah1ted2p+Kgoszj3IU44Fn7QlXxBNGz4h3YNtvDCWS3JVp1RHfZRonPdrvcwSMSk6M7crxS0NTxZlhvbgTuD2AlKjAT381gMUAxGGm9tvHlxwxhuIFEmXV1rz3hxbPYyFppCCrwQJBAd83wa9FwfGkcWU/gN3I0kJc8EfQwYDooY/V6hLcCYNW7wXyn3ja6Xl9XtgQnPeYSx+h2bGs339cDPKYtG2imm0CQG+PiLA8IHmIJDiEeB+jIRXAizd2aMgsvUomjWmMiYl/SPocvL3HxgGhvNNs8tUJKcw+/JhKPSBFX2+aRF55l7UCQQEZmVsQUs6P35De7T0dlrevVYvAt7QtuwXNTueYo4JXkosslJEPZJxTzs8f6ktC11Q1x1b0KGDBJ2dYW1GTJzzVAkBB6iq3Bi+h3wCXrB1VhAsOUR9we7PZYiXNZA31qSWyadRygvw5nYmueSOaQCi1Ep7xoN2aFXMcWCDVTe7La0hpAkBjBU9PDamhULFstd4hlx2UkPOGGnWZCAnr2zU+r2Q12eK20tjDZzS4TQWZWmKXkI2QAd+SJ44oI6+hZq5AHHEl
Public Key:
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADkIJlKBgQDQ1dz/te8wiMP2SMIzP+auw3hBIuAFWBNkkz527gKb3NCYwt4Mfhfpi7Qn0fpcYtEwFqnRSujJIxmTFzjXBB0VsQP+5bRQtUZYBFs8Zna+JDhC+Q1eg0XqeGJIeILrxn9wODdQPO3KJtK0ozqFkfvjfRwphsa5xIYFe4G/cJJ6EQIDAQAB
Things are working fine in the iOS world.
I am trying to implement this in the MAC using Objective-c and find that the Crypto Exercise sample is not working as expected. The SecItemCopyMatching is returning failure.
While investigating for the solution found that the public/private key using Open SSL met the criteria of private and public key except for the header and footer that is appended to the generated key pair.
I used the SSCrypto OpenSSL wrapper (http://septicus.com/products/opensource/) to generate the public and private key pair using OpenSSL which is of the form.
-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQCrEgoLjG34pNLj4ahpW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stripped of the "-----BEGIN PUBLIC KEY-----" and "-----END PUBLIC KEY-----" footer and white spaces to extract the public key to be sent to the server.
The problem is that when I strip the header and footer the sent the public key to the server, though the server successfully encrypts the data (using the public key) and sent encrypted data to the client, the decryption using private key (in PEM format) using OpenSSL fails.
I think the decryption has failed as when i modify the public key's content (by stripping of the header/fooders), the public and private key seize to be key pairs.
Can somebody help me with the solution such that I am able to use the private key to decrypt the received encrypted data.
Alternatively, can somebody help me in generation of public key/private key pair of abovementioned lengths using objective-c api's (pair as generated by OpenSSL) which will fix the issue I am facing?
Thanks & Regards.
The data encrypted by server using Public Key is an AES key. The size of the AES key after encryption using public key is 176 characters. Here is the code snippet which does it.
public static String encryptAESKey(byte[] aesKeyBytes, String publicKeyStr) {
String cipherStr = null;
try {
PublicKey pkey = getPublicKey(publicKeyStr);
final Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, pkey);
byte[] cipherBytes = cipher.doFinal(aesKeyBytes);
cipherStr = Base64.encodeBase64String(cipherBytes);
} catch (Exception e) {
e.printStackTrace();
}
return cipherStr;
}
private static PublicKey getPublicKey(String publicKey) throws InvalidKeySpecException, NoSuchAlgorithmException {
KeyFactory rsaKeyFac = KeyFactory.getInstance(RSA_ALGORITHM);
byte[] keyBytes = Base64.decodeBase64(publicKey.getBytes());
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
RSAPublicKey rsaPubKey = (RSAPublicKey)rsaKeyFac.generatePublic(keySpec);
return rsaPubKey;
}

Resources