Get readable format from public key - public-key

I have o convert an public key in redable format.
I have this public key
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3qRkBn/pouirEVL/H+at
FLbB1Wmm41Y/4REbuNwMkPNGHhTwVNF4mSvUFdK6L0SYZ8f8B1oe6YzfZ+bWBAde
oiupY6ABHJ8xGkb14RBwFm152kUT29jXXKU9N8aKGsWayOO7ZLIMOnFrWrjIrrLa
NSkKfuCxywOInjcpkuaq/c+cdxKIDTw1QsUrz3rN1s/TJsvfY+oeFtzCh1auE2dQ
dVPxSeK1ApMsXsXfUP85UNloGlruSrf2IyxK33trNGigH2qD/1PHcjft8PiG00gD
9R2DR0bBASpz/CYKEm6Ry37lVJif0OtQKnyVTap4yLuWaMlZiFaSfBh0FlueHLxN
QwIDAQAB
-----END PUBLIC KEY-----
I wanted extract JWK from this public key like this
{
"kty": "RSA",
"n": "3FCW9xBBsviZUQnDlcZczC_GO0J0Vhzx1VUj_JyM8hTBzJ_--1GHH5k60OHz169jEHgJ9kkwIKP0ld8Ah5-6x1z5a_Vh-uFizbPn45MgUjwuEL2VPeZsn_wPb2t-A6otOR5zUNU_n4xXcjqZETpvEjFQFQ2n_SDujZUMtQfda9f_plTaekqws4iWIr2sA4QNXXSvv6JvWmjp49N6AQexrVRW9XFBkEFpqofwdWkJMU9TJThzzZB--GA8f4CHfD_DsX_aOsM1u1oCmjWiKmz793NhDQChY0LApEjosTm9a0kQuYEEfGrLNiLfplIsXq6Qw4lFCiWDv553J4_nJusAsw",
"e": "AQAB",
"alg": "RS256",
"use": "sig"
}
How can i covert this public key and get JWK using terminal or any other tool.

You can use authlib library + python3
https://authlib.org/
from authlib.jose import jwk
rsa_key={
'public_key':'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3qRkBn/pouirEVL/H+at\nFLbB1Wmm41Y/4REbuNwMkPNGHhTwVNF4mSvUFdK6L0SYZ8f8B1oe6YzfZ+bWBAde\noiupY6ABHJ8xGkb14RBwFm152kUT29jXXKU9N8aKGsWayOO7ZLIMOnFrWrjIrrLa\nNSkKfuCxywOInjcpkuaq/c+cdxKIDTw1QsUrz3rN1s/TJsvfY+oeFtzCh1auE2dQ\ndVPxSeK1ApMsXsXfUP85UNloGlruSrf2IyxK33trNGigH2qD/1PHcjft8PiG00gD\n9R2DR0bBASpz/CYKEm6Ry37lVJif0OtQKnyVTap4yLuWaMlZiFaSfBh0FlueHLxN\nQwIDAQAB\n-----END PUBLIC KEY-----'
}
print(jwk.dumps(rsa_key['public_key'], kty='RSA'))
notice there is a \n at end of each line of the key

If you are using C# then the following here solution could be helpful for you.

Related

How do we parse JSON in Unity3d?

When I try to use JsonUtility to parse the JSON from a REST call returning an array of elements, it fails saying that top level must be an object. C'mon guys. That's ridiculous.
OK, moving on to figuring out how to use Newtonsoft.json because it works well everywhere else.
Tried using JSON .NET for Unity out of the asset store. Seemed like it would work fine and did in the editor, but didn't work (calls to it failed with exceptions) when I tried to use it in a Mac local build on my dev macbook pro. So if it doesn't work on my dev machine and for mac builds, then it's a no go. https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347
Tried using jilleJr's version from here: https://github.com/jilleJr/Newtonsoft.Json-for-Unity but the installation instructions caused the package manager to not be able to open. I get a null reference exception when following the directions to add the package directly into my manifest.json
So what's the secret to making JSON work in Unity 2019/2020 right now? Seems like a huge problem for a platform to have.
Open *YourUnityProject*/Packages/manifest.json and add the following line to the dependencies object.
"com.unity.nuget.newtonsoft-json": "2.0.0",
To serialize and deserialize data use these functions:
using Newtonsoft.Json;
public class Data
{
int[] numbers;
}
string json = JsonConvert.SerializeObject(new Data());
Data data = JsonConvert.DeserializeObject<Data>(json);
I wouldn't say it's a problem with Unity, back when I was trying to read data in from serialised JSON. JsonUtility worked a treat when serialising/deserialising to/from a class. I also preferred it to Newtonsoft.json, mainly because it was part of UnityEngine
For example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class JSONRW : MonoBehaviour
{
PersonData data;
string jsonPath = "Assets/TestJSON.json";
private void Start()
{
//Read in JSON file
string jsonString = File.ReadAllText(jsonPath);
data = JsonUtility.FromJson<PersonData>(jsonString);
Debug.Log(data.client[0].name); //Outputs A
//Write to JSON file
data.client[0].name = "Luc"; //Changes the name of the entry named A
File.WriteAllText(jsonPath, JsonUtility.ToJson(data)); //Writes updata back to file
}
}
[System.Serializable]
public class Person
{
public string name;
public int age;
public Person(string _name, int _age)
{
name = _name;
age = _age;
}
}
[System.Serializable]
public class PersonData
{
public List<Person> client;
}
JSON file:
{
"client": [
{
"name": "A",
"age": 10
},
{
"name": "B",
"age": 20
},
{
"name": "C",
"age": 30
},
{
"name": "D",
"age": 40
}
]
}

Elasticsearch.NET (NEST) fails to deserialize my POCO JSON in version 7.x

Elasticsearch 7.x for .NET has a documented breaking change with the internal Utf8 JSON serializer.
Instead of me using the another option -> Newtonsoft's Json.NET serializer, I'm trying to figure out which part of my JSON is breaking the serializer.
Client nuget: 7.3.1
Server: 7.2.0
Firstly the error feels like it's helpful, but I don't think it really is:
2019-10-18 17:15:33 ERR Fatal error.
Elasticsearch.Net.UnexpectedElasticsearchClientException: expected:'String Begin Token', actual:'2', at offset:540 ---> Elasticsearch.Net.Utf8Json.JsonParsingException: expected:'String Begin Token', actual:'2', at offset:540
at Elasticsearch.Net.Utf8Json.JsonReader.ReadStringSegmentCore(Byte[]& resultBytes, Int32& resultOffset, Int32& resultLength)
at Elasticsearch.Net.Utf8Json.JsonReader.ReadString()
at Deserialize(Object[] , JsonReader& , IJsonFormatterResolver )
at Elasticsearch.Net.Utf8Json.JsonSerializer.Deserialize[T](Byte[] bytes, Int32 offset, IJsonFormatterResolver resolver)
at Elasticsearch.Net.Utf8Json.JsonSerializer.Deserialize[T](Stream stream, IJsonFormatterResolver resolver)
at Elasticsearch.Net.DiagnosticsSerializerProxy.Deserialize[T](Stream stream)
at Nest.SourceFormatter`1.Deserialize(JsonReader& reader, IJsonFormatterResolver formatterResolver)
at Deserialize(Object[] , JsonReader& , IJsonFormatterResolver )
at Nest.ReadAsFormatter`2.Deserialize(JsonReader& reader, IJsonFormatterResolver formatterResolver)
at Elasticsearch.Net.Utf8Json.Formatters.CollectionFormatterBase`4.Deserialize(JsonReader& reader, IJsonFormatterResolver formatterResolver)
at Deserialize(Object[] , JsonReader& , IJsonFormatterResolver )
at Nest.ReadAsFormatter`2.Deserialize(JsonReader& reader, IJsonFormatterResolver formatterResolver)
... etc ...
So at character 540, something is bad.
So I think I have the json which came back. I think because I asked a collegue to use Fiddler and give me the payload result. I can't paste the full payload anywhere because it contains sensitive stuff but this is the part of code at line 540
There's no '2' there.
So what about the Json? Here's a nicely formatted screenshot of the json, with the custom results hidden:
and finally, the poco section, for the section I think is erroring (but I'm not sure):
[Keyword(Index = false)]
public string Street { get; set; }
[Keyword]
public string Suburb { get; set; }
[Keyword]
public string State { get; set; }
[Keyword]
public string StateCode { get; set; }
[Keyword]
public string Country { get; set; }
[Keyword]
public string Postcode { get; set; }
[GeoPoint]
public GeoLocation LatLong { get; set; }
So:
I'm not sure how to grab the exact json which is parsed by NEST. Is it the json I grabbed, which can be confirmed by the snippet I provided?
Is there a way I can manually create a unit test + use the internal Utf8 serializer with NEST to load my json if I save this JSON payload to a file in order to debug this further?
Issue is with enums. Out ES DB has some small subset of documents with a number/int value while our POCO has a string value. The string value is the string representation of our enums, while the int value was the previous int representation of the enums.
public class Foo
{
public string statusType { get; set }
}
To figure out what the error is/was, I needed to do the following:
Clone ES repo and put a breakpoint in ES's JsonSerializer Deserialize<T> method because SourceLink is not enabled on the ES nuget packages (as of the time of this posting).
Update the method to convert the byte array to a string, so I can see what the JSON payload is, before it tries to get deserialized into our POCO.
Step through the code, to finally hit the breakpoint and see what the json payload is.
So the error message is in respect to the json segment of my document. Not the entire json ES payload. In the above examplei have 5 'hits' (document segments) so the Deserialize was getting hit 1x per document segment. When it hit the segment with the int values (which should have been string value) it then crashed. So I can now see what the error at char 540 is and viola....
"statusType": 2,
which is bad. is should be:
"statusType": "available",
So - problem identified.

REST: how to serialize a java object to JSON in a "shallow" way?

Suppose I have the following JPA entities:
#Entity
public class Inner {
#Id private Long id;
private String name;
// getters/setters
}
#Entity
public class Outer {
#Id private Long id;
private String name;
#ManyToOne private Inner inner;
// getters/setters
}
Both Spring and java EE have REST implementations with default serializers which will marshall the entities to/from JSON without further coding. But when converting Outer to JSON, both Spring and EE nest a full copy of Inner within it:
// Outer
{
"id": "1234",
"name": "MyOuterName",
"inner": {
"id": "4321",
"name": "MyInnerName"
}
}
This is correct behavior but problematic for my web services, since the object graphs can get deep/complex and can contain circular references. Is there any way to configure the supplied marshaller to marshall the POJOs/entities in a "shallow" way instead without having to create a custom JSON serializer for each one? One custom serializer that works on all entities would be fine. I'd ideally like something like this:
// Outer
{
"id": "1234",
"name": "MyOuterName",
"innerId": "4321"
}
I'd also like it to "unmarshall" the JSON back into the equivalent java object. Bonus kudos if the solution works with both Spring and java EE. Thanks!
After many problems I give reason to Cássio Mazzochi Molin saying that "the use of entities persistence in your REST API can not be a good idea"
I would do that the business layer transform persistence entities to DTO.
You can do this very easily with libraries like mapstruct
If you still want to continue with this bad practice you can use jackson and customize your jackson mapper
To unscramble complex object graphs using jaxb #XmlID and #XmlIDREF is made for.
public class JSONTestCase {
#XmlRootElement
public static final class Entity {
private String id;
private String someInfo;
private DetailEntity detail;
#XmlIDREF
private DetailEntity detailAgain;
public Entity(String id, String someInfo, DetailEntity detail) {
this.id = id;
this.someInfo = someInfo;
this.detail = detail;
this.detailAgain = detail;
}
// default constructor, getters, setters
}
public static final class DetailEntity {
#XmlID
private String id;
private String someDetailInfo;
// constructors, getters, setters
}
#Test
public void testMarshalling() throws JAXBException {
Entity e = new Entity( "42", "info", new DetailEntity("47","detailInfo") );
JAXBContext context = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[]{Entity.class}, null);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
m.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
m.marshal(e, System.out);
}
}
This will result in the following json-fragment
{
"detailAgain" : "47",
"detail" : {
"id" : "47",
"someDetailInfo" : "detailInfo"
},
"id" : "42",
"someInfo" : "info"
}
Unmarshalling of this json will ensure that detail and detailAgain are the same instances.
The two annotations are part of jaxb, so it will work in Spring as well as in java EE. Marshalling to json is not part of the standard, so i use moxy in the example.
Update
Explicitly using moxy is not neccessary in a JAX-RS Resource. The following snipped perfectly runs on a java-EE-7 container (glassfish 4.1.1) and results in the above json-fragment:
#Stateless
#Path("/entities")
public class EntityResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public Entity getEntity() {
return new Entity( "42", "info", new DetailEntity("47","detailInfo") );
}
}
I had the same problem and ended up using jackson annotations on my Entities to control the serialization:
What you need is #JsonIdentityReference(alwaysAsId=true) to instruct the bean serializer that this reference should be only an ID. You can see an example on my repo:
https://github.com/sashokbg/company-rest-service/blob/master/src/main/java/bg/alexander/model/Order.java
#OneToMany(mappedBy="order", fetch=FetchType.EAGER)
#JsonIdentityReference(alwaysAsId=true) // otherwise first ref as POJO, others as id
private Set<OrderDetail> orderDetails;
If you want a full control of how your entities are represented as JSON, you can use JsonView to define which field is serialized related to your view.
#JsonView(Views.Public.class)
public int id;
#JsonView(Views.Public.class)
public String itemName;
#JsonView(Views.Internal.class)
public String ownerName;
http://www.baeldung.com/jackson-json-view-annotation
Cheers !
for this problem There are two solutions.
1-using jackson json view
2- Createing two mapping classe for innner entity. one of them includes custom fields and another one includes all fields ...
i think jackson json view is better solution ...
Go through the FLEXJSON library to smartly include/exclude nested class hierarchy while serializing Java objects.
Examples for flexjson.JSONSerializer presented here
You can detach the JPA entity before serialization, if you use lazyloading it's avoid to load sub objects.
Another way, but is depend of the JSON serializer API, you can use "transient" or specifics annotation.
Why does JPA have a #Transient annotation?
A bad way is to use tool like dozer to copy JPA object in another class with only the properties need for json (but it works... little overhead of memory, CPU and time...)
#Entity
public class Outer {
#Id private Long id;
private String name;
#ManyToOne private Inner inner;
//load manually inner.id
private final Long innerId;
// getters/setters
}

Can't DER encode and BER decode RSA public key

I have problems using Crypto++ to save a RSA public key (that I obtained loading a private key file in PKCS#8 format). When decoding the key, I always get a BERDecodeErr exception.
Here is the code I am using:
CryptoPP::RSASSA_PKCS1v15_SHA_Signer _signer;
CryptoPP::RSASSA_PKCS1v15_SHA_Verifier _verifier;
CryptoPP::ByteQueue bytes;
//_signer.AccessPublicKey().Save(bytes); // seem to save private key instead
_signer.AccessKey().DEREncodePublicKey(bytes);
//_verifier.AccessKey().Load(bytes);
//_verifier.AccessKey().BERDecodePublicKey(bytes, 0, 0);
_verifier.AccessPublicKey().Load(bytes);
I also tried with the instructions commented above, without success.
How do you do to save or open the public key?
The public key looks like this in hex format, is there a tool to check its format / validity (regarding what crypto++ supports) ?
3081890281810097e24f2e95504a397e90fbc56d1b330ab2ab97a0d326007b890e40013f9e1d9bd9
f54b0c0840782ddae19b5b4595d8f8b9ffe0d2120174fcbc39585c5867cd2dfba69f8e540caa2c52
de8f08278a34e9249120500117f0ba756c5bb2be660013160db9f82f75deb7ccf63742a9e945da6c
cf30c2b109b73342daaabd02b872e50203010001
I'm not sure I understand your problem completely. But you look like you are on the right track with using either Load/Save or BERDecodePublicKey/DEREncodePublicKey.
Here's how I would approach it given you have a PKCS#8 encoded private key.
FileSource privateKey("<private key>", true);
RSASSA_PKCS1v15_SHA_Signer signer;
signer.AccessKey().Load(privateKey);
AutoSeededRandomPool prng;
bool valid = signer.AccessKey().Validate(prng, 3);
...
RSASSA_PKCS1v15_SHA_Verifier verifier(signer);
FileSink publicKey("<public key>", true);
verifier.AccessKey().Save(publicKey);
Then, you can use Gutmann's dumpasn1 to print it:
$ dumpasn1 <public key>
...
I believe you can also convert a private key/signer to a public key/verifier with:
RSASSA_PKCS1v15_SHA_Signer signer;
signer.AccessKey().Load(privateKey);
RSASSA_PKCS1v15_SHA_Verifier verifier;
signer.MakePublic(verifier);
There's also a page on the Crypto++ wiki that talks about it in greater detail: Keys and Formats. And there's a page dedicated to PEM encoding, if interested: PEM Pack. If you want the PEM encoding, you have to compile the library yourself, though.
Here's the code I used with the public key you posted. It had no problems.
string key = "3081890281810097e24f2e95504a397e90fbc56d1b330ab2ab97a0d326007b890e40013f9e1d9bd9 \
f54b0c0840782ddae19b5b4595d8f8b9ffe0d2120174fcbc39585c5867cd2dfba69f8e540caa2c52 \
de8f08278a34e9249120500117f0ba756c5bb2be660013160db9f82f75deb7ccf63742a9e945da6c \
cf30c2b109b73342daaabd02b872e50203010001";
ByteQueue queue;
StringSource ss(key, true, new HexDecoder(new Redirector(queue)));
RSASSA_PKCS1v15_SHA_Verifier verifier;
verifier.AccessKey().BERDecodePublicKey(queue, false, 0);
AutoSeededRandomPool prng;
bool result = verifier.AccessKey().Validate(prng, 3);
if(!result)
throw Exception(Exception::OTHER_ERROR, "Failed to validate public key");
If you install the PEM Pack then you can add the following:
FileSink sink("public-key.pem", true);
PEM_Save(sink, verifier.GetKey());
That will get you:
$ cat public-key.pem
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCX4k8ulVBKOX6Q+8VtGzMKsquX
oNMmAHuJDkABP54dm9n1SwwIQHgt2uGbW0WV2Pi5/+DSEgF0/Lw5WFxYZ80t+6af
jlQMqixS3o8IJ4o06SSRIFABF/C6dWxbsr5mABMWDbn4L3Xet8z2N0Kp6UXabM8w
wrEJtzNC2qq9Arhy5QIDAQAB
-----END PUBLIC KEY-----
And:
$ openssl rsa -in public-key.pem -pubin -text -noout
Public-Key: (1024 bit)
Modulus:
00:97:e2:4f:2e:95:50:4a:39:7e:90:fb:c5:6d:1b:
33:0a:b2:ab:97:a0:d3:26:00:7b:89:0e:40:01:3f:
9e:1d:9b:d9:f5:4b:0c:08:40:78:2d:da:e1:9b:5b:
45:95:d8:f8:b9:ff:e0:d2:12:01:74:fc:bc:39:58:
5c:58:67:cd:2d:fb:a6:9f:8e:54:0c:aa:2c:52:de:
8f:08:27:8a:34:e9:24:91:20:50:01:17:f0:ba:75:
6c:5b:b2:be:66:00:13:16:0d:b9:f8:2f:75:de:b7:
cc:f6:37:42:a9:e9:45:da:6c:cf:30:c2:b1:09:b7:
33:42:da:aa:bd:02:b8:72:e5
Exponent: 65537 (0x10001)
Finally, the difference between:
verifier.AccessKey(): gets the RSA::Public key, the key is non-const
verifier.GetKey(): gets the RSA::Public key, the key is const

Strange Mapping Behaviour Jackson JSON

I've got a strange mapping Issue with Jackson on Android.
I've got a "Content" Class which should be used by the Jackson Mapper.
It looks like this:
public class content {
private String header;
private String subheader;
private String bodytext;
#JsonProperty("singleimage")
private String image;
#JsonProperty("uid")
private String id;
#JsonProperty("link")
private String article;
#JsonProperty("CType")
private String cType;
// Eclipse auto generated getters & setters
...
}
The corresponding JSON Object looks like this:
{
"header": "xyz",
"subheader": "abc",
"bodytext": "abc",
"singleimage": "abc",
"images": "abc.jpg",
"teaser_elements": "",
"uid": "13",
"link": "xyz.htm",
"CType": "row_header"
}
Now when I use the Jackson Maper to create instances of Content from a provided JSON all fields of the content class get populated correctly - all except "cType".
I already tried to move the #JsonProperty("CType") annotation to the setCType Method but still no effect.
I don't get any Exceptions while mapping the class or anything else and as it seems to me that all mappings pretty much do the same (mapping to String) im kinda buffled why it doesn't work wit the "CType".
Any suggestions what the problem might be are highly appreciated.