You can write under any name you want, depending on your intent. If your intent is to deceive, you probably can't.
Here's an example of a literary forgery (https://en.wikipedia.org/wiki/Literary_forgery) where the author ended up in prison: https://en.wikipedia.org/wiki/Hitler_Diaries ... But it would probably take some digging to see what particular action he was had for.