Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate public key base64-string? #8

Open
anranyicheng opened this issue Apr 24, 2023 · 5 comments
Open

How to generate public key base64-string? #8

anranyicheng opened this issue Apr 24, 2023 · 5 comments

Comments

@anranyicheng
Copy link

Hi,
if i have N and E with RSA, how to generate public key? is this library can do that?

(cl-tls:create-asn-sequence
(list '(1 2 840 113549 1 1 1) :oid)
(list n :integer)
(list e :integer))

and result is correct oct-vector, can you help me?

@adlai
Copy link

adlai commented Apr 25, 2023

Please start by enclosing code samples in markdown, so they are more easily readable; click the triple dots at the corner of this comment, and "Quote reply", to see the source of the following example:

(eval-when ()
  (throw :dice))

@anranyicheng
Copy link
Author

Please start by enclosing code samples in markdown, so they are more easily readable; click the triple dots at the corner of this comment, and "Quote reply", to see the source of the following example:

(eval-when ()
  (throw :dice))

Yes,

(ql:quickload '(asn1 ironclad cl-tls cl-base64))
;; public-key, base64 string 
(defparameter *pub-key-str*
  "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDHdsyxT66pDG4p73yope7jxA92c0AT4qIJ/xtbBcHkFPK77upnsfDTJiVEuQDH+MiMeb+XhCLNKZGp0yaUU6GlxZdp+nLW8b7Kmijr3iepaDhcbVTsYBWchaWUXauj9Lrhz58/6AE/NF0aMolxIGpsi+ST2hSHPu3GSXMdhPCkWQIDAQAB")

;; public-key octet-vect 
(defparameter *pub-key-oct* 
  (base64:base64-string-to-usb8-array *pub-key-str*))

(defparameter *rsa-pub-key*
  (trivia:match (asn1:decode *pub-key-oct*)
    ((asn1:rsa-public-key-info n e)
     (ironclad:make-public-key :rsa :n n :e e))))

(defparameter *n*
  (ironclad:rsa-key-modulus *rsa-pub-key*))
(defparameter *e*
  (ironclad:rsa-key-exponent *rsa-pub-key*))

(defparameter *generate-oct*
  (cl-tls:create-asn-sequence
   (list '(1 2 840 113549 1 1 1) :oid)
   (list '() :null)
   (list *n* :integer)
   (list *e* :integer)))

(equalp *pub-key-oct* *generate-oct*)
;; -> NIL

@adlai
Copy link

adlai commented Apr 27, 2023

and result is correct oct-vector, can you help me?

Probably; however...

(equalp *pub-key-oct* *generate-oct*)
;; -> NIL

please link to the source from whence you took the failed test vectors; the only thing I hate worse than GitHub, is Google.

@anranyicheng
Copy link
Author

anranyicheng commented Apr 27, 2023

and result is correct oct-vector, can you help me?

Probably; however...

(equalp *pub-key-oct* *generate-oct*)
;; -> NIL

please link to the source from whence you took the failed test vectors; the only thing I hate worse than GitHub, is Google.

"and result is not correct oct-vector, can you help me?"

Sorry, the result is wrong, I originally wanted to express how to generate the same key(base64 string) as the original through N and E
and I get publick_key_str from python:

from Crypto.PublicKey import RSA
cipher = RSA.generate(1024)
public = cipher.publickey()
public_key_str = public.exportKey().decode()
print(public_key_str)
# etc:
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCbtonF8bSV1oSHsc5hPsZlVDo6
LNld7cSc5lVkwdciwXTBPjO+YqK4IXMmyYRCnycUJsQRI7ceZTNCbb0QInzFURR5
tP8E09QrmbkN4qzD7dJarJnedxgHiCkjZV/QS0GfKhp07Dh5P4/+GVbgan+9vMkF
rhgjnwmI48+eZmAXXQIDAQAB
-----END PUBLIC KEY-----

I want:

(equalp *pub-key-oct* *generate-oct*)
;; -> T

@adlai
Copy link

adlai commented Apr 30, 2023

[...]

I want:

(equalp *pub-key-oct* *generate-oct*)
;; -> T

Having re-read the code failing this test, I now understand what you're trying to do; not being familiar with the entire library, it'll take me a while to reply anything useful, so I recommend conducting your own study; mine could take a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants