title: Converting Coldfusion encryption code to C# (again)
body: <p>Once again I'm tasked with converting ColdFusion code used for a single sign-on to C# and am running short on time.  This one is completely different than my question that was answered <a href="https://stackoverflow.com/questions/27848994/converting-coldfusion-encryption-code-to-c-sharp">here</a>, so I'm back to being in over my head.</p>
  
  <p>The original ColdFusion code was executed in a &lt;cfscript&gt; tag.  I've replaced the src and pwd variables with abbreviated placeholders just to obscure their actual values:</p>
  
  <pre><code>//create a key to be used
  src="xxx";
  pwd="abc";
  
  // Base64 Decoding the key
  base64Decoder = createObject("java", "sun.misc.BASE64Decoder");
  desKeyData = base64Decoder.decodeBuffer(pwd);
  
  
  // Initialize the constructor of DESedeKeySpec with private key
  KeySpec=createObject("java", "javax.crypto.spec.DESedeKeySpec");
  KeySpec=KeySpec.init(desKeyData);
  
  // Generate the secret key using SecretKeyFactory
  keyFac=createObject("java", "javax.crypto.SecretKeyFactory").getInstance("DESede");
  secretKey =keyFac.generateSecret(KeySpec);
  
  
  // Get CIPHER OBJ ready to use
  decodecipher = createObject("java", "javax.crypto.Cipher").getInstance("DESede/ECB/PKCS5Padding");
  decodecipher.init(2, secretKey);
  
  encodecipher = createObject("java", "javax.crypto.Cipher").getInstance("DESede/ECB/PKCS5Padding");
  
  encodecipher.init(1, secretKey);
  
  stringBytes = toString(src).getBytes("UTF8");
  raw = encodecipher.doFinal(stringBytes);
  
  // Base64Encoding of generated cipher
  cipherText=ToBase64(raw);
  </code></pre>
  
  <p>I also have a document from the other party that outlines the steps for creating the single sign-on as follows:</p>
  
  <p>Creating the encrypted token</p>
  
  <ul>
  <li>Create the plain text (this corresponds to the variable src above, and that part I've done successfully in C#)</li>
  <li><p>Pad the plain text </p></li>
  <li><p>Decode the key (the key corresponds to the variable pwd above, and must be base 64 decoded; I think I've successfully gotten up to this point as well.)</p></li>
  <li><p>Perform the encryption (use the decoded key obtained above and the plain text to do the encryption)</p></li>
  <li><p>Encode the cipher text (url encoded)</p></li>
  </ul>
  
  <p>I have the BouncyCastle libraries installed and am trying to make use of those, but I'm stuck on the actual encryption step. So far the beginning of my C# conversion looks like this (once again the token and key have abbreviated placeholders to obscure the actual values):</p>
  
  <pre><code>//steps omitted here to create src string
  string token = "xxx";
  string key = "abc";
  byte[] decodedKeyBytes = Convert.FromBase64String(key);
  </code></pre>
  
  <p>I know that's not a whole lot to go on, but I've tried so many things that haven't worked that I've lost track.  Eventually when I get to the piece where I'm initializing the cipher, I assume I need something like this:</p>
  
  <pre><code>PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new DesEdeEngine());
  </code></pre>
  
  <p>Thanks very much for any suggestions/examples.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>Thanks to the very helpful answer below, I was able to get this working using the following code:</p>
  
  <pre><code>string token = "xxx";
  string key = "abc";
  
  byte[] base64DecodedKeyBytes = Convert.FromBase64String(key);
  byte[] inputBytesToken = System.Text.Encoding.UTF8.GetBytes(token);
  
  // initialize for EBC mode and PKCS5/PKCS7 padding
  PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new DesEdeEngine());
  KeyParameter param = new KeyParameter(base64DecodedKeyBytes);
  cipher.Init(true, param);
  
  // encrypt and encode as base64
  byte[] encryptedBytesToken = cipher.DoFinal(inputBytesToken);
  string tokenBase64 = System.Convert.ToBase64String(encryptedBytesToken); 
  </code></pre>