55
66from Plugin import PluginManager
77from Crypt import CryptBitcoin , CryptHash
8- import lib .pybitcointools as btctools
98from Config import config
9+ import sslcrypto
10+
1011from . import CryptMessage
1112
13+ curve = sslcrypto .ecc .get_curve ("secp256k1" )
14+
1215
1316@PluginManager .registerTo ("UiWebsocket" )
1417class UiWebsocketPlugin (object ):
15- def eciesDecrypt (self , encrypted , privatekey ):
16- back = CryptMessage .getEcc (privatekey ).decrypt (encrypted )
17- return back .decode ("utf8" )
18-
1918 # - Actions -
2019
2120 # Returns user's public key unique to site
2221 # Return: Public key
2322 def actionUserPublickey (self , to , index = 0 ):
24- publickey = self .user .getEncryptPublickey (self .site .address , index )
25- self .response (to , publickey )
23+ self .response (to , self .user .getEncryptPublickey (self .site .address , index ))
2624
2725 # Encrypt a text using the publickey or user's sites unique publickey
2826 # Return: Encrypted text using base64 encoding
@@ -55,32 +53,23 @@ def actionEciesDecrypt(self, to, param, privatekey=0):
5553
5654 # Encrypt a text using AES
5755 # Return: Iv, AES key, Encrypted text
58- def actionAesEncrypt (self , to , text , key = None , iv = None ):
59- from lib import pyelliptic
60-
56+ def actionAesEncrypt (self , to , text , key = None ):
6157 if key :
6258 key = base64 .b64decode (key )
6359 else :
64- key = os .urandom (32 )
65-
66- if iv : # Generate new AES key if not definied
67- iv = base64 .b64decode (iv )
68- else :
69- iv = pyelliptic .Cipher .gen_IV ('aes-256-cbc' )
60+ key = sslcrypto .aes .new_key ()
7061
7162 if text :
72- encrypted = pyelliptic . Cipher ( key , iv , 1 , ciphername = ' aes-256-cbc' ). ciphering (text .encode ("utf8" ))
63+ encrypted , iv = sslcrypto . aes . encrypt (text .encode ("utf8" ), key )
7364 else :
74- encrypted = b""
65+ encrypted , iv = b"" , b""
7566
7667 res = [base64 .b64encode (item ).decode ("utf8" ) for item in [key , iv , encrypted ]]
7768 self .response (to , res )
7869
7970 # Decrypt a text using AES
8071 # Return: Decrypted text
8172 def actionAesDecrypt (self , to , * args ):
82- from lib import pyelliptic
83-
8473 if len (args ) == 3 : # Single decrypt
8574 encrypted_texts = [(args [0 ], args [1 ])]
8675 keys = [args [2 ]]
@@ -93,9 +82,8 @@ def actionAesDecrypt(self, to, *args):
9382 iv = base64 .b64decode (iv )
9483 text = None
9584 for key in keys :
96- ctx = pyelliptic .Cipher (base64 .b64decode (key ), iv , 0 , ciphername = 'aes-256-cbc' )
9785 try :
98- decrypted = ctx . ciphering (encrypted_text )
86+ decrypted = sslcrypto . aes . decrypt (encrypted_text , iv , base64 . b64decode ( key ) )
9987 if decrypted and decrypted .decode ("utf8" ): # Valid text decoded
10088 text = decrypted .decode ("utf8" )
10189 except Exception as err :
@@ -122,12 +110,11 @@ def actionEcdsaVerify(self, to, data, address, signature):
122110
123111 # Gets the publickey of a given privatekey
124112 def actionEccPrivToPub (self , to , privatekey ):
125- self .response (to , btctools . privtopub ( privatekey ))
113+ self .response (to , curve . private_to_public ( curve . wif_to_private ( privatekey ) ))
126114
127115 # Gets the address of a given publickey
128116 def actionEccPubToAddr (self , to , publickey ):
129- address = btctools .pubtoaddr (btctools .decode_pubkey (publickey ))
130- self .response (to , address )
117+ self .response (to , curve .public_to_address (bytes .fromhex (publickey )))
131118
132119
133120@PluginManager .registerTo ("User" )
@@ -163,7 +150,7 @@ def getEncryptPublickey(self, address, param_index=0):
163150
164151 if "encrypt_publickey_%s" % index not in site_data :
165152 privatekey = self .getEncryptPrivatekey (address , param_index )
166- publickey = btctools . encode_pubkey ( btctools . privtopub (privatekey ), "bin_compressed" )
153+ publickey = curve . private_to_public ( curve . wif_to_private (privatekey ))
167154 site_data ["encrypt_publickey_%s" % index ] = base64 .b64encode (publickey ).decode ("utf8" )
168155 return site_data ["encrypt_publickey_%s" % index ]
169156
@@ -200,8 +187,8 @@ def testCryptEciesDecrypt(self, num_run=1):
200187 aes_key , encrypted = CryptMessage .eciesEncrypt (self .utf8_text .encode ("utf8" ), self .publickey )
201188 for i in range (num_run ):
202189 assert len (aes_key ) == 32
203- ecc = CryptMessage .getEcc ( self .privatekey )
204- assert ecc . decrypt ( encrypted ) == self .utf8_text .encode ("utf8" ), "%s != %s" % (ecc . decrypt ( encrypted ) , self .utf8_text .encode ("utf8" ))
190+ decrypted = CryptMessage .eciesDecrypt ( base64 . b64encode ( encrypted ), self .privatekey )
191+ assert decrypted == self .utf8_text .encode ("utf8" ), "%s != %s" % (decrypted , self .utf8_text .encode ("utf8" ))
205192 yield "."
206193
207194 def testCryptEciesDecryptMulti (self , num_run = 1 ):
@@ -223,23 +210,16 @@ def testCryptEciesDecryptMulti(self, num_run=1):
223210 gevent .joinall (threads )
224211
225212 def testCryptAesEncrypt (self , num_run = 1 ):
226- from lib import pyelliptic
227-
228213 for i in range (num_run ):
229214 key = os .urandom (32 )
230- iv = pyelliptic .Cipher .gen_IV ('aes-256-cbc' )
231- encrypted = pyelliptic .Cipher (key , iv , 1 , ciphername = 'aes-256-cbc' ).ciphering (self .utf8_text .encode ("utf8" ))
215+ encrypted = sslcrypto .aes .encrypt (self .utf8_text .encode ("utf8" ), key )
232216 yield "."
233217
234218 def testCryptAesDecrypt (self , num_run = 1 ):
235- from lib import pyelliptic
236-
237219 key = os .urandom (32 )
238- iv = pyelliptic .Cipher .gen_IV ('aes-256-cbc' )
239- encrypted_text = pyelliptic .Cipher (key , iv , 1 , ciphername = 'aes-256-cbc' ).ciphering (self .utf8_text .encode ("utf8" ))
220+ encrypted_text , iv = sslcrypto .aes .encrypt (self .utf8_text .encode ("utf8" ), key )
240221
241222 for i in range (num_run ):
242- ctx = pyelliptic .Cipher (key , iv , 0 , ciphername = 'aes-256-cbc' )
243- decrypted = ctx .ciphering (encrypted_text ).decode ("utf8" )
223+ decrypted = sslcrypto .aes .decrypt (encrypted_text , iv , key ).decode ("utf8" )
244224 assert decrypted == self .utf8_text
245225 yield "."
0 commit comments