Test which public keys were generated by a specific private key - nearprotocol

Let's say I've used near keys foo.near to identify all the keys for a given account and I want to understand which of those public keys were generated by a particular private key. How would I do that most easily?

If you know the private key in question, this is a pretty trivial problem. You can use near-api-js to generate the KeyPair and get the public key. A quick code-snippet:
const { KeyPair } = require("near-api-js");
let keyPair = KeyPair.fromString(secretKey);
let publicKey = keyPair.getPublicKey().toString();
At this point, you have the Public Key and can compare with the keys returned from the CLI.

Related

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.

How to access Spring properties from an entity?

I have a spring app, that pushes data in an s3 bucket.
public class Ebook implements Serializable {
#Column(name= "cover_path", unique = true, nullable = true)
private String coverPath;
private String coverDownloadUrl;
#Value("${aws.cloudfront.region}")
private String awsCloudFrontDns;
#PostLoad
public void init(){
// I want to access the property here
System.out.println("PostConstruct");
String coverDownloadUrl = "https://"+awsCloudFrontDns+"/"+coverPath;
}
When a data is pushed, let's say my cover here, I get the key 1/test-folder/mycover.jpg which is the important part of the future http URL of the data.
When I read the data from database, I enter inside #PostLoad method and I want construct the complete URL using the cloudfront value. This value changes frequently so we don't want to save hardly in the database.
How could I do to construct my full path just after reading the data in database?
The only way to do this is to use a service that update the data after using repository to read it? For readbyId it can be a good solution, but for reading list or using other jpa methods, this solutions won't work because I have each time to create a dedicated service for the update.
It doesn't look good for Entity to depend on property.
How about EntityListener.
#Component
public class EbookEntityListener {
#Value("${aws.cloudfront.region}")
private String awsCloudFrontDns;
#PostLoad
void postload(Ebook entity) { entity.updateDns(awsCloudFrontDns); }
}
I recommend trying this way :)

How to check if a public key matches a given private key?

I would like to use Ruby and its OpenSSL module to check whether a public key matches a given private key. Basically a user enters both a private key and a public key and I want to make sure that they match (they belong to the same key pair).
How can I achieve that?
The solution I have found is to sign a message with the private key and then try to verify the message with the public key:
public_key.verify(OpenSSL::Digest::SHA256.new, private_key.sign(OpenSSL::Digest::SHA256.new, 'message'), 'message')

How do I add an interceptor to save/fetch methods of Spring's JPARepository to update transient values?

First let me say I'm a complete novice with Spring AOP, and I apologize if this is a duplicate question.
Here's my scenario:
Let's say I have the following domain class:
#Entity(name="MyTable")
#Table(name="MY_TABLE")
public class MyTable {
private static final long serialVersionUID = 1234567890123456L;
#Id
#Column(name = "USER_ID")
private Long userID;
#Transient
private String key;
#Column(name = "KEY")
private String secureKey;
/* Other columns */
/* Getters and Setters */
}
and I have the following JPARepository class to manage it:
#Repository
public interface MyTableRepository extends JpaRepository<MyTable, Long> {
/* findBy methods */
}
As you can see, I have a secureKey field and a transient key field. In this case secureKey is an encrypted version of key.
What I need is for the secureKey value to be populated before a domain object is saved, and for the key value to be populated after a domain object is fetched. (This is a trivial example but in the real case I have multiple transient and encrypted values.) The idea is for the secure values to be persisted to the DB, but users of the domain class will only need to work with the "insecure" values.
Currently I'm handling this in my service layer. After I call a fetch method I'm populating the transient values, and before calling a save method I'm populating the "secure" values. This is working as expected but ideally I'd like this to be managed transparently, because now the burden is on each developer to remember to update those values after fetching or before saving.
I'm assuming the best way to handle this would be through some AOP class, but I confess I have little to no idea where to begin there. Is this a common scenario, and if so, would someone be willing to point me in the right direction? Also, if you have a suggestion for a better way to implement this decrypted/encrypted field pair scenario, please let me know.
Ideally I'd like to be able to add an annotation to both the secure and insecure fields, maybe pointing to each other, maybe something like:
#Insecure(secureValue = "secureKey")
#Transient
private String key;
#Secure(insecureValue = "key")
#Column(name = "KEY")
private String secureKey;
Any assistance you could provide is most appreciated.
Thanks,
B.J.
I think Spring AOP isn't the correct technology in your use case, i would recommend to use EntityListeners.
Hibernate: https://docs.jboss.org/hibernate/entitymanager/3.5/reference/en/html/listeners.html
Eclipselink: https://wiki.eclipse.org/EclipseLink/Release/2.5/JPA21#CDI_Entity_Listeners

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-----
MIICXQIBAAKBgQCrEgoLjG34pNLj4ahpWlvXZZEhzimg/mJoKJLdrjE3Fg817Qf2
+iXTPMWAtmCkYgHn9Y99VJSdJzrb0/E1JnxMPva52ZMS8ilS/hSZnXRAlq2OPFMj
64SU9XDu/eWqJMULELNkBcTtPgTCAkjrDU6Qt2AbwNUwrgufJC8WJEhWdQIDAQAB
AoGBAKc2Y8E0C44dtdFvEgmge+MH8RuFA6XM4O2Es0Gh8ZMxqb6BKObdTbmzTi3o
loA3GDveB9puoTEXVm3nNX9JVfYr952+54vEfFGDnUfnsjpzQih5NkODKKxS0NXW
DsCgemo9QmOYlGUm/mcvdv2gnjrl/E1TgRbC5cJ8bX5O3sAhAkEA04mFvAx+B+mx
uVH8RxBkv0iB7lEuR87jrZuL7n7LnyHdnstK85xs9mmx95nMhmh6jWD1VIRVbx3X
XsRqoRVQjQJBAM8HER8bae89Vw6ptBezB3ihs4NZ/dF5jM3ksLN10hm9n00hSSmo
FaT7PcSizFkoUs2kGcytZuzTkSYVaeM154kCQQC6AXQJ7cYoeRJgjTnS1xRvqnct
sk6Kr949usepl+6+Z83zInkuiv65Eil+OcvA6D/S703p2k8xXMETQI0uRYrFAkBV
sIEftQMV6Pe9s/Q80vdGsPdSaM8sAvmKxxt0TFIYIWpsTFiyC0ZaMTuRxih6xrvv
LfsXwrYVVESB1N8tEkSxAkAbPNx57ceCLMvkknDWuNdygtBgtAskSccktWnbXk9C
CnpHn/toehb9Grk6pbR1PqLRgD2l8ctiYBq/+2t+L/lp
-----END RSA PRIVATE KEY-----
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrEgoLjG34pNLj4ahpWlvXZZEh
zimg/mJoKJLdrjE3Fg817Qf2+iXTPMWAtmCkYgHn9Y99VJSdJzrb0/E1JnxMPva5
2ZMS8ilS/hSZnXRAlq2OPFMj64SU9XDu/eWqJMULELNkBcTtPgTCAkjrDU6Qt2Ab
wNUwrgufJC8WJEhWdQIDAQAB
-----END PUBLIC KEY-----
To extract the public key, I 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