Private Public Key String Encryption?

Just starting out? Need help? Post your questions and find answers here.
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Private Public Key String Encryption?

Post by novablue »

Hello, i am looking for a simple solution to encrypt a string with one key that can only be decrypted with another. i searched the forums and looked at asymmetric encryption and Diffie Hellman but i can not wrap my head around it. Is there any simple example on how this can be done?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Private Public Key String Encryption?

Post by idle »

There also a port of Curve25519 elliptic curve, public key function for use in Dieffie-Hellman key exchange
viewtopic.php?f=12&t=55892

Code: Select all

   
   client =  modEC::NewEC("salt n pepper")  ;Create new EC context with out of channel passphrase 
   server  = modEC::NewEC("salt n pepper") ;to mitigate man in the middle attacks 
   
   clients_public_key = client\GenKeys()   ;Client generates keys  -> sends the public key  to server 
   servers_public_key = server\GenKeys()  ; Server generates keys -> returns the public key to client  
   
   client\SaveKeys("EC_Keys")   ;test save: saves the whole keyset    
   clients_public_key = client\LoadKeys("EC_Keys") ;loads a whole key set and returns the public key  
      
   Clients_shared_secret = client\getkey(servers_public_key)  ;Client plugs in the servers public key to get the secret encyption key   
   Servers_shared_secret = server\getkey(clients_public_key)  ;Server plugs in the Clients public key to get the secret encryption key 
   
   ;from this point the client and server can now use the shared secret to transfer encrypted data to perform a log in...  
   ;using a pass phrase salt mitigates the risk of a man in the middle attack 
   
  
   
novablue
Enthusiast
Enthusiast
Posts: 165
Joined: Sun Nov 27, 2016 6:38 am

Re: Private Public Key String Encryption?

Post by novablue »

Thanks for the examples and links but I have looked at those before and it is not really what i am looking for unless i am misunderstanding.

My scenario:

Person A creates a key and gives it to x number of people. then they all can encrypt something with that key but they can not decrypt it again with the key once it is encrypted. then they all can send the encrypted message back to person A who can only decrypt it.

Is this possible? From all the examples i read its about client and server coming up together with a secret shared key but that is not what i am looking for unless i am missing the big picture here?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Private Public Key String Encryption?

Post by idle »

novablue wrote: Sun Sep 19, 2021 2:36 am Person A creates a key and gives it to x number of people. then they all can encrypt something with that key but they can not decrypt it again with the key once it is encrypted. then they all can send the encrypted message back to person A who can only decrypt it.
maybe this helps
A generates his keys sends his public key to x y z
x makes a set of ephemeral keys and encrypts his message and sends both the message and his public key to A, once x has freed his keys the only way x could decrypt his message is by having A's secret key.

Code: Select all

IncludeFile "modEC.pbi"

UseModule modEC 

A.iEC = NewEC("") 
x.iEC = NewEC("")
y.iEC = NewEC("")
z.iEC = NewEC("") 

A_public_key$ = A\GenKeys() ;A generates a set of keys, it returns the public key for sharing 
                            ;A Gives his public key To x y z 

x_public_key$ = x\GenKeys() ; x y z creates a set of ephemeral keys 
y_public_key$ = y\GenKeys() ; 
z_public_key$ = z\GenKeys() ; 

encryption_key_x_a$ =  x\getkey(A_public_key$)  ;x plugs in A's public key to his enryption key      
encryption_key_y_a$ =  y\getkey(A_public_key$)  
encryption_key_z_a$ =  z\getkey(A_public_key$) 

message$ = "the quick brown fox jumps over the lazy dog" 
size = Len(message$)*2
*encbufX = AllocateMemory(size) 
*encbufY = AllocateMemory(size) 
*encbufZ = AllocateMemory(size) 
*decbuf = AllocateMemory(size) 

AESEncoder(@message$,*encbufX,size,@encryption_key_x_a$,256,@x_public_key$)   ;x encodes message with his encode key uses his public key for IV
AESEncoder(@message$,*encbufY,size,@encryption_key_y_a$,256,@y_public_key$)   ;y encodes message  
AESEncoder(@message$,*encbufZ,size,@encryption_key_z_a$,256,@z_public_key$)   ;z encodes message  

decryption_key_a_x$ = A\GetKey(x_public_key$)                                 ;A gets x's encryption key from x_public_key

If AESDecoder(*encbufX,*decbuf,size,@decryption_key_a_x$,256,@x_public_key$)  ;A decrypts with X's encryption key and public key   
  Debug PeekS(*decbuf) 
  FillMemory(*decbuf,size,0)  
EndIf   

decryption_key_a_y$ = A\GetKey(y_public_key$)

If AESDecoder(*encbufY,*decbuf,size,@decryption_key_a_y$,256,@y_public_key$)   
  Debug PeekS(*decbuf) 
  FillMemory(*decbuf,size,0)  
EndIf   

decryption_key_a_z$ = A\GetKey(z_public_key$)

If AESDecoder(*encbufZ,*decbuf,size,@decryption_key_a_z$,256,@z_public_key$)   
  Debug PeekS(*decbuf) 
  FillMemory(*decbuf,size,0)  
EndIf

a\free() 
x\free() 
y\Free()
z\free()





Post Reply