         //self.iv = ivt
         let cipher:NSData?
         if recv_ctx == nil {
-            let iv  =  encrypt_bytes.subdataWithRange(NSMakeRange(0,16))
-            recvCTX(iv)
-            cipher = encrypt_bytes.subdataWithRange(NSMakeRange(16,encrypt_bytes.length-16));
+            let iv_len = send_ctx!.m.iv_size
+            let iv  =  encrypt_bytes.subdataWithRange(NSMakeRange(0,iv_len))
+            recvCTX(iv) //
+            cipher = encrypt_bytes.subdataWithRange(NSMakeRange(iv_len,encrypt_bytes.length - iv_len ));
+            print("iv \(iv) \(iv_len)")
+            print("ramdonKey \(ramdonKey!)")
+            print("data \(cipher!) \(cipher?.length) \(encrypt_bytes.length - iv_len)")
+            print("encrypt_bytes 000 \(encrypt_bytes)")
         }else {
             cipher = encrypt_bytes
         }
-        //        if let left = cipher {
-        //            let tempbuffer = NSMutableData.init()
-        //            if let last = recv_buffer {
-        //                tempbuffer.appendData(last)
-        //                tempbuffer.appendData(left)
-        //                recv_buffer = NSData.init()
-        //            }else {
-        //                tempbuffer.setData(left)
-        //            }
-        //            let block_size = 128 / 8
-        //            let left_size =  tempbuffer.length %  block_size
-        //            if left_size != 0{
-        //                recv_buffer = tempbuffer.subdataWithRange(NSMakeRange(tempbuffer.length - left_size, left_size))
-        //                tempbuffer.length = tempbuffer.length - left_size
-        //
-        //            }
-        //            AxLogger.log("recv cipher length:\(tempbuffer.length % 16)")
-        //            return tempbuffer
-        //        }
+
         return cipher
-        
+
     }
     func decrypt(encrypt_bytes:NSData) ->NSData?{
         if (  encrypt_bytes.length == 0 ) {

    static func evpBytesToKey(password:String, keyLen:Int) ->NSData {
        let  md5Len:Int = 16
        
        let cnt = 1// (keyLen -1)/md5Len + 1
        let m = NSMutableData.init(length: cnt*md5Len)!
        let bytes = password.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
        // memcpy((m?.mutableBytes)!, bytes.bytes , password.characters.count)

        
        //let encrypt_bytes = padding(encrypt_bytes_org)
        //alloc number of bytes written to data Out
        guard let ctx = send_ctx else {
            AxLogger.log("ss ctx error")
            return nil
        }
        
        //Update Cryptor
        if ctx.m.rawValue >= CryptoMethod.SALSA20.rawValue {
            debugLog("111 encrypt")
              let padding = ctx.counter % SODIUM_BLOCK_SIZE;
            let cipher = NSMutableData.init(length:  2*(encrypt_bytes.length + Int(padding)))
            
              //cipher.length += encrypt_bytes.length
//            brealloc(cipher, iv_len + (padding + cipher->len) * 2, capacity);
            var  plain:NSMutableData
            if padding != 0 {
                plain = NSMutableData.init(length: Int(padding))!
                plain.appendData(encrypt_bytes)
                //plain.length =  plain.length + Int(padding)
//                brealloc(plain, plain->len + padding, capacity);
//                memmove(plain->array + padding, plain->array, plain->len);
                //sodium_memzero(plain->array, padding);
            }else {
                 plain = NSMutableData.init(data: encrypt_bytes)
            }
            debugLog("222 encrypt")
            //let enc_key = NSMutableData.init(data: ramdonKey!)
            //enc_key.length = send_ctx!.m.key_size
//            let ptr:UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.init((cipher?.mutableBytes)!)
//            let ptr2:UnsafePointer<UInt8> = UnsafePointer<UInt8>.init(encrypt_bytes.bytes)
            debugLog("333 encrypt")
            crypto_stream_xor_ic(cipher! ,
                                 md: plain,
                                 mlen: UInt64(plain.length),
                                 nd: ctx.IV,
                                 ic: ctx.counter / SODIUM_BLOCK_SIZE,
                                 kd: ramdonKey!)
            var result:NSMutableData
            if ctx.counter == 0 {
            
                result = NSMutableData.init(data: ctx.IV)
                result.length = m.iv_size
            }else {
                result = NSMutableData.init()
            }
            
            ctx.counter += UInt64(encrypt_bytes.length)
            
            
            if padding != 0 {
//                memmove(cipher->array + iv_len,
//                    cipher->array + iv_len + padding, cipher->len);
                result.appendData(cipher!.subdataWithRange(NSMakeRange(Int(padding), encrypt_bytes.length
                    )))
            }else {
                result.appendData(cipher!.subdataWithRange(NSMakeRange(0, encrypt_bytes.length
                    )))

            }
            debugLog("000 encrypt")
            return result
        }else {
            var  outLength:NSInteger = 0 ;
            // Alloc Data Out
            let cipherData:NSMutableData = NSMutableData.init(length: encrypt_bytes.length)!;
            let  update:CCCryptorStatus = CCCryptorUpdate(ctx.ctx,
                                                          encrypt_bytes.bytes,
                                                          encrypt_bytes.length,
                                                          cipherData.mutableBytes,
                                                          cipherData.length,
                                                          &outLength);
            if (update == CCCryptorStatus(0))
            {
                //Cut Data Out with nedded length
                cipherData.length = outLength;
                
                //Final Cryptor
                let final:CCCryptorStatus = CCCryptorFinal(ctx.ctx, //CCCryptorRef cryptorRef,
                    cipherData.mutableBytes, //void *dataOut,
                    cipherData.length, // size_t dataOutAvailable,
                    &outLength); // size_t *dataOutMoved)
                
                if (final == CCCryptorStatus(0))
                {
                    
                    //CCCryptorRelease(cryptor )
                }
                if ctx.counter == 0 {
                    ctx.counter += 1
                    let d:NSMutableData = NSMutableData()
                    d.appendData(ctx.IV);
                    
                    d.appendData(cipherData)
                    return d
                }else {
                    return cipherData
                }
                
                //AxLogger.log("cipher length:\(d.length % 16)")
                
                
            }

        }
        
        return nil
    }
    
    func ss_onetimeauth(buffer:NSData) ->NSData {
        
        let keyData = NSMutableData.init(data: send_ctx!.IV)
        //let key_size = send_ctx!.m.key_size
        //let ramdonK2 = ramdonKey?.subdataWithRange(NSMakeRange(0, key_size))
        keyData.appendData(ramdonKey!)
        let hash = buffer.hmacsha1(keyData)
        //let result = NSMutableData.init(data: buffer)
        //result.appendData(hash)
        //=result.sha1
        return hash
    }
    func ss_gen_hash(buffer:NSData,counter:Int32) ->NSData {
        
        let blen = buffer.length
        var chunk_len:UInt16 = UInt16(blen).bigEndian
        var c =  counter.bigEndian
        
        let keyData = NSMutableData.init(data: send_ctx!.IV)
        keyData.appendBytes(&c, length: 4)
        let hash = buffer.hmacsha1(keyData)
        let result = NSMutableData.init(bytes: &chunk_len, length: 2)
        
        result.appendData(hash)
        //result.appendData(buffer)
        //=result.sha1
        return result
    }
}
