Can Anyone Explain what keys are in dict of jwk when generating key - jwk

Can anyone help regarding this??
I generated a key in python using jwk using below command and stored in a variable key
key = jwk.JWK.generate(kty='RSA', size=512)
and when i used key.export() it returned the below dict
{'d': 'Z1apo6KRMoS0xyqqTu7lEwZ7f_AON_tve42nSUkwXypMF1rDNj_xgIn9J5I4TvAisUaRYq82uZfYf76eMgj8uQ',
'dp': '4k-hSfYmT8H2zdHVFVQpBD-_w5G9ASSADgKn3F08AAs',
'dq': 'E4fXlCY6oT3yPTnOb3LvLxMtKDPmwoI-FLYbNP2L0-k',
'e': 'AQAB',
'kty': 'RSA',
'n': 'wuALgiButVPQy8bCnSkvU-QlBqYB5pk6rfwlcTr-csc8DOvPzekHJYWPjbP_ptAxSW3r5Bnpac1MDgMQKFjOtw',
'p': '8ZI61ugJ3WblKvY-JfkyWXUcdoGAWQB8B9VcfWRvLuM',
'q': 'zoPN8ItkA_0rf_XobRkjhYIdtoXyOLXCqYSU0i8etR0',
'qi': 'JhXuF6EDTrrPysGzsVhco4hpVsSHCXgS7UGZUISc2Ug'}
can anyone explain what are the keys in this dict like d, dp, dq, e, n, p, q, qi

You generated a JWK (JSON Web Key), a special representation of a key. In your case it's an RSA Key which contains the parameters for the private and the public key.
Please refer to the RFC7517 for the general keys. e.g.
"kty" (Key Type) Parameter
and
RFC7518 for the algorithm specific part.
n and e are the modulus and exponent of the public key, all others are used for the private key. Section 6.3 of the RFC7518 lists all the specific entries of the RSA key:
"n" (Modulus) Parameter
"e" (Exponent) Parameter
"d" (Private Exponent) Parameter
"p" (First Prime Factor) Parameter
"q" (Second Prime Factor) Parameter
"dp" (First Factor CRT Exponent) Parameter
"dq" (Second Factor CRT Exponent) Parameter
"qi" (First CRT Coefficient) Parameter

Related

encryption decryption

I am trying to create a custom encryption/decryption code in python. I read few articles and found that I can store the encryption ciphers in the dictionary. I tried using it but it is not working. keys will be letters in alphabetical order(A-Z) and values will be the letters that I want to put(ciphers). There is no specifics shift pattern in the encryption cipher. There are also numbers(0-9) in the encryption-decryption. they are in sequence though.
dict = {'A' : 'O', 'B' : 'P', 'C' : 'J' ......'Z' : 'Z', ....
'0' : 9, '1' : 8, ............'8' : 1, '9' : 0}
These encrypted password will be stored in a database. I need help to create the code to encrypt and decrypt the password. If there's any other method to start with please let me know.
If you don't want to use the Python Cryptography Module...
Here's some code that may help:
I'm replacing your dict with the name en_dict
#unencrypted_item should be a string
def encrypt(unencrypted_item):
result = ""
for i in unencrypted_item:
result+=en_dict[i]
return result
def encrypt(encrypted_item):
result = ""
for i in encrypted_item:
for n in en_dict:
if en_dict[n] == i:
result += n
break
return result
Try mapping the original string with the keys in your dictionary.
mapping = {'A':'Z', 'B':'C', '1':'0', '2':'7'}
reverse_mapping = dict((j,i) for i,j in mapping.items())
string_to_encrypt = 'AB1'
encrypted_string = ''.join(map(mapping.__getitem__, string_to_encrypt))
print(encrypted_string) #ZC0
decrypted_string = ''.join(map(reverse_mapping.__getitem__, encrypted_string))
print(decrypted_string) #AB1
Firstly, I hope that you plan to use your encryption scheme for academic purposes only because this simple substitution cipher is very insecure. For the most basic level of security you should look at storing salted hashes of user passwords instead, or use something like bcrypt.
WRT to your specific question, use your mapping dictionary with str.translate() to translate the string in one go without the need to explicitly loop in your code, or use other functions such as map() and join().
encrypt_dict = {'A': 'O', 'B': 'P', 'C': 'J', 'Z': 'Z', '0': 9, '1': 8, '8': 1, '9': 0}
tr_encrypt = {ord(str(k)): ord(str(encrypt_dict[k])) for k in encrypt_dict}
tr_decrypt = {tr_encrypt[k]: k for k in tr_encrypt}
plaintext = 'ABCZ0189'
ciphertext = plaintext.translate(tr_encrypt)
>>> ciphertext
'OPJZ9810'
assert ciphertext.translate(tr_decrypt) == plaintext
plaintext = 'THIS IS test 128'
ciphertext = plaintext.translate(tr_encrypt)
>>> ciphertext
'THIS IS test 821'
assert ciphertext.translate(tr_decrypt) == plaintext
The first few lines set up encryption and decryption translation tables in the form required by str.translate(). Then it's simply a matter of calling translate on the string to encrypt and decrypt as required.
The second example shows that any character in the string that is not present in the translation table is left unchanged which may or may not be what you want. You can create a translation mapping that covers all expected characters, but that's potentially a lot if you are using unicode.
I think this is what you want. You can use string.printable[:61] where string.printable prints all the ASCII characters and [:61] takes all characters from index 0 to 61 - which are letters and numbers
import string
import random
normla_l=list(string.printable[:61])
new_l=list(string.printable[:61])
random.shuffle(new_l)
see_list={x:y for x,y in zip(normla_l,new_l)}
def change_list(char):
return see_list.get(char) if char!='' else random.choice(normla_l)
x=input("Enter string: ")
print(''.join([change_list(char.strip()) for char in x]).strip())
Sample
Enter string: This string will be encrypted
Xqv7G74HvTxbPv55psIVITzHhg4IN

Swiftui: how do you assign the value in a "String?" object to a "String" object?

Swiftui dictionaries have the feature that the value returned by using key access is always of type "optional". For example, a dictionary that has type String keys and type String values is tricky to access because each returned value is of type optional.
An obvious need is to assign x=myDictionary[key] where you are trying to get the String of the dictionary "value" into the String variable x.
Well this is tricky because the String value is always returned as an Optional String, usually identified as type String?.
So how is it possible to convert the String?-type value returned by the dictionary access into a plain String-type that can be assigned to a plain String-type variable?
I guess the problem is that there is no way to know for sure that there exists a dictionary value for the key. The key used to access the dictionary could be anything so somehow you have to deal with that.
As described in #jnpdx answer to this SO question (How do you assign a String?-type object to a String-type variable?), there are at least three ways to convert a String? to a String:
import SwiftUI
var x: Double? = 6.0
var a = 2.0
if x != nil {
a = x!
}
if let b = x {
a = x!
}
a = x ?? 0.0
Two key concepts:
Check the optional to see if it is nil
if the optional is not equal to nil, then go ahead
In the first method above, "if x != nil" explicitly checks to make sure x is not nil be fore the closure is executed.
In the second method above, "if let a = b" will execute the closure as long as b is not equal to nil.
In the third method above, the "nil-coalescing" operator ?? is employed. If x=nil, then the default value after ?? is assigned to a.
The above code will run in a playground.
Besides the three methods above, there is at least one other method using "guard let" but I am uncertain of the syntax.
I believe that the three above methods also apply to variables other than String? and String.

the relation between add key/value and mapassign method in go1.10.3

I am reading the source code of map in go1.10.3.It seemed there exist corresponding method about operation such as:
makemap(t *maptype, hint int, h *hmap) *hmap ==> m = make(map[xx]yy)
mapaccess1(t *maptype, h *hmap, key unsafe.Pointer)==> m['key']
but I cant find the correspond method for the operation which add key/value as below:
m['xx']='yy'
there exist a method called mapassign which has some similarity with this
operation.
mapassign(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer
this will add a new key to the map, but as we can see, the input arguments has no value. And another question is when it has already this key, it maybe update this key.
if !alg.equal(key, k) {
continue
}
// already have a mapping for key. Update it.
if t.needkeyupdate {//why??
typedmemmove(t.key, k, key)
}
since the two key is equal, why should update it?
summary:
1. the relation between add key/value operation and method mapassign?
2. why it maybe need to update the key since the insert key and the key which has already exist is equal in mapassign method?
In the operation m[k] = v, the caller copies the value v to the address returned by mapassign.
The comments in the function needkeyupdate explain why some types need key updates: floating point & complex -0 and 0 are equal, but different values; string might have smaller backing store.

Which parts of a RSA private key is secret?

This is a RSA private key :
<BitStrength>4944</BitStrength>
<RSAKeyValue>
<Modulus>vqZ07gIBoLEzJZHhNYj3WSt9F5iw/Klr/hb22Q8YT5NivYS8UpuzteNHpE7u0pk4Izcto4fqtqPHFpXxzhpswtCwOO7czabK/8B+im6T6aRHSSbZXB59NPxj883BS04F1/zziwPj84Ez+/t1ckrK8O/Gbo8DOr4UPQ3CDoDw4gAQpNe4H48zfGV42WIOQjm00DHOhgpFpZaYapXjDz3KfbdY+p4y1R0Yt4eNViRmvQ2jBPIeXVZACC3Ef3J0TCTWne+1Y4gCfMl0z+4hkE1XZpPiDxt2BR46GYU+NwsrI82pIJ6Bigj51jYpgPE4uQBmjYylY/HTJw4b6N45MuljWFuaxOrDHaHdskhSGUS+fAzp5KXh1xsRdzXysv6uliPU3BmgNsM0Av5Gm4EnVoeqjeC/0G3dLoIJi//IqBCNGOfV8sSo/Y6JNGZoAlE6KT101Q89ClrJyHVoZCbU3qVg9F6tPCorn9+UH9rEKQbABpMMjBM3H2MFuiUlvDWlatP+VH1WugKInlP3b20cINGs+Z5obUYcnAiZ8Dj5WN+OVOZH2nqG3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couple of elements (Modules, Exponent, P, E, DP, DQ, InverseQ, D)
I read somewhere that the main element of a RSA private key is D.
But I want to know which parts of these elements should be secret and which parts can be publicly shared ? (Means there's no security problem if it be shared)
Thanks
From Wikipedia:
The public key consists of the modulus n and the public (or encryption) exponent e. The private key consists of the modulus n and the private (or decryption) exponent d, which must be kept secret. p, q, and φ(n) must also be kept secret because they can be used to calculate d.
This means that that you are correct. D is the most important part, but because 'd' can be found from other information (p,DP,DQ, InverseQ) they too should also be kept secret. The modulus and exponent form the public key.
source: http://en.wikipedia.org/wiki/RSA_(cryptosystem)

Standard way to hash an RSA key?

What's the algorithm for creating hash (sha-1 or MD5) of an RSA public key? Is there a standard way to do this? Hash just the modulus, string addition of both and then take a hash? Is SHA-1 or MD5 usually used?
I want to use it to ensure that I got the right key (have the sender send a hash, and I calculate it myself), and log said hash so I always know which exact key I used when I encrypt the payload.
Based on the OpenSSH source code, the way that a fingerprint is generated for RSA keys is to convert n and e from the public key to big-endian binary data, concatenate the data and then hash that data with the given hash function.
Portions of the OpenSSH source code follows. The comments were added to clarify what is happening.
// from key_fingerprint_raw() in key.c
switch (k->type) {
case KEY_RSA1:
// figure out how long n and e will be in binary form
nlen = BN_num_bytes(k->rsa->n);
elen = BN_num_bytes(k->rsa->e);
len = nlen + elen;
// allocate space for n and e and copy the binary data into blob
blob = xmalloc(len);
BN_bn2bin(k->rsa->n, blob);
BN_bn2bin(k->rsa->e, blob + nlen);
...
// pick a digest to use
switch (dgst_type) {
case SSH_FP_MD5:
md = EVP_md5();
break;
case SSH_FP_SHA1:
md = EVP_sha1();
break;
...
// hash the data in blob (n and e)
EVP_DigestInit(&ctx, md);
EVP_DigestUpdate(&ctx, blob, len);
EVP_DigestFinal(&ctx, retval, dgst_raw_length);
From the BN_bn2bin manual page:
BN_bn2bin(a, to) converts the absolute value of a into big-endian form and stores it at to. to must point to BN_num_bytes(a) bytes of memory.

Resources