災いを転じて福となす(わざわいをてんじてふくとなす)

Japanese vocabulary

Meaning: to turn misfortune into fortune (esp. through one's own efforts); to turn the potential disaster to one's advantage

See it used in real works