Fail to encrypt scriptPubKey in testnet I have the following testnet wallet address:

2MsQEtPJ6JJZszMYrD6udjUyTDFLczWQrv9


Which I am trying to encode as pubkeyhash to the coinbase TX in testnet.
When i run decode58, I get this

C401B47E5722F808856A308FA043CCF28323F51711E8165536


I strip it off the version byte prefix and the checksum postfix, and I get:

01B47E5722F808856A308FA043CCF28323F51711


So the scriptPubKey should be 

76a91401b47e5722f808856a308fa043ccf28323f5171188ac which is OP_DUP OP_HASH160 01b47e5722f808856a308fa043ccf28323f51711 OP_EQUALVERIFY OP_CHECKSIG


But as a sanity unit test, i verify it Vs. the debug console 

decodescript 76a91401b47e5722f808856a308fa043ccf28323f5171188ac


But the debug console gives me this:

  "asm": "OP_DUP OP_HASH160 01b47e5722f808856a308fa043ccf28323f51711 OP_EQUALVERIFY OP_CHECKSIG",
  "reqSigs": 1,
  "type": "pubkeyhash",
  "addresses": [
    "mffyC9xbwyBQUWhV8SbYWpqaiNSSWR2vpo"
  ],
  "p2sh": "2ND12E9b9oa9hvTTckXwNaSJiRZ39eRFWSJ"


So why do I get address "mffyC9xbwyBQUWhV8SbYWpqaiNSSWR2vpo" and not "2MsQEtPJ6JJZszMYrD6udjUyTDFLczWQrv9" as I expect?
First of all, scriptPubKeys are not encrypted and what you are doing is not encryption. This is encoding.

The problem is that you are trying to encode a P2SH address as a P2PKH scriptPubKey. P2SH addresses are different from P2PKH addresses and have different opcodes. You can identify them by looking at the version number of the address which you are not doing. The version number actually has a meaning, it is not ignorable. Your scriptPubKey should actually be 

OP_HASH160 01b47e5722f808856a308fa043ccf28323f51711 OP_EQUAL