An optimized encoding method for secure key distribution by swapping quantum entanglement and its extension