Convert Coldfusion encryption code in C #

I have a Coldfusion page that contains a section of code that encrypts such a variable:

<cfset data64 = toBase64(key)>
<cfset encryptedID = encrypt(getUser.ID, data64, "BLOWFISH", "Base64")>

We are moving the site to CMS on .NET. I need to convert this page to C #, but I have problems.

I successfully converted the first line to this:

byte[] keyBytes = System.Text.Encoding.UTF8.GetBytes(key);
string keyBase64 = System.Convert.ToBase64String(keyBytes);

I also added the blowfish.cs class found at https://defuse.ca/blowfish.htm , but I'm a bit vague about how to use this with a key (and whether I want to use ECB, CBC or CTR). I'm also not sure if the analogy is to use base64 encoding in Coldfusion ... this is what I am trying now, which does not give the same results as the source code:

BlowFish b = new BlowFish(keyBase64);
byte[] idBytes = System.Text.Encoding.UTF8.GetBytes(thisUser["ID"].ToString());
byte[] idBytesEncrypted = b.Encrypt_ECB(idBytes);
string idBase64 = System.Convert.ToBase64String(idBytesEncrypted);

, Coldfusion , #. . !

+4
1

, BouncyCastle # API. , POC, , , , CF-.

, : Strong Encryption ColdFusion, , ColdFusion ECB PKCS5Padding. , Blowfish, , Blowfish/ECB/PKCS5Padding. # ( ), .

, #, , BlowfishEngine ECB. , PaddedBufferedBlockCipher, PKCS5 . , CF-:

    byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(userIDString);
    byte[] keyBytes = System.Convert.FromBase64String(keyInBase64);

    // initialize for ECB mode and PKCS5/PKCS7 padding
    PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine());
    KeyParameter param = new KeyParameter(keyBytes);
    cipher.Init(true, param);

    // encrypt and encode as base64
    byte[] encryptedBytes =  cipher.DoFinal(inputBytes);
    string idBase64 = System.Convert.ToBase64String(encryptedBytes);

NB: , , "ECB" . . wiki . .

+2

Source: https://habr.com/ru/post/1570511/


All Articles