It's not illegal. Cloaking is where you cover your link with something else with an url shorterner like Adfly or Bitly etc...
Cloaking means to present different version of web page contents from of search engines to search robots and human visitors based on their browser's user agent or IP address.
Check out this post for the answer: http://justindupre.com/what-is-cloaking-and-why-do-affiliate-marketers-use-it/ Hope it helps