使用structfind和structfindvalue加密结构中的值

时间:2018-04-16 14:09:47

标签: coldfusion

我有一个structform

cffunction函数

structform是一个结构,我尝试使用structfind来查找值并将其值加宽

我试过这个

<cfset fvalue = structfind(structform,"name")>
<cfset fvalue>
<cfset stuctform = encrypt(structfindvalue(

不确定如何以及如何解决它

我不确定我所遵循的方法是否正确,任何指导都将不胜感激

1 个答案:

答案 0 :(得分:1)

我无法通过TryCF(https://trycf.com)进行保存,因此请在此处运行以查看其工作原理。当您准备好使用它时,您将要删除注释和转储。我已经包含了你的价值的加密和解密,因为如果你不能解密,加密的价值并不高。您需要将密钥保存在某个位置,以便以后可以解密。我使用generateSecretKey()来制作加密密钥。如果你使用另一个,你可以将它传递给函数。这使用ColdFusion的encrypt()decrypt(),因此如果您需要更强大的内容,可以更改此内容。

<cfscript>
    // First we build up a nested struct.
    mystruct = {
        first : {
            second : {
                name  : "notEncrypted" ,
                dob   : "1950-1-1" ,
                other : "stuff"
            }
        }
    } ;

    // This dumps the original struct for verification
    writeDump(var=mystruct,label="originalStruct") ;

    // Now we create our Secret Key for encryption. Store this 
    //somewhere. We'll need it to decrypt.
    mySecretKey = GenerateSecretKey("AES") ;

    // This is our function to encrypt and decrypt our key's value.
    // We pass in our structure, the key we're looking for, the encryption key, and 
    // whether we're encrypting or decrypting. Since both are essentially the same 
    // code, I just included them in the same function
    function xxcryptStuffByKey ( 
        required Struct inStruct, 
        required String theKey, 
        required String theEncKey, 
        required String EorD 
    ) {
        // Where is the key we want in our struct?    
        var whereAmI = structFindKey(arguments.inStruct,arguments.theKey) ;

        // Create the return variable
        var outStruct = {
            // This is the value of the key we're looking for.
            myData : whereAmI[1].value , 
            myKey  : arguments.theEncKey 
        } ;

        // Do we encrypt or decrypt?
        var newData = "" ;
        if (arguments.EorD == "E" ) {  // Encrypt
            newData = encrypt(outStruct.myData, outStruct.myKey, "AES", "Base64") ;
        }
        else if (arguments.EorD == "D") {  // Decrypt
            newData = decrypt(outStruct.myData, outStruct.myKey, "AES", "Base64") ;
        }
        else {  // Oops. Just return our original struct, or throw an error.
            return outStruct;
        }

        "inStruct#whereAmI[1].path#" = newData ; 
        // The " notation on the left lets us update the struct. 
        // NOTE: CF Structures are Pass By Reference = you will be
        // modifying the original struct.

        // We want to see what happened in the function.
        writeDump(var=outStruct,label="InFunction");

        return outStruct ;
    }

    // Encrypt our key value.
    // This will overwrite your original struct. Are you sure?
    xxcryptStuffByKey(myStruct, "name", mySecretKey, "E") ;
    // This gives us ...
    writeDump(var=myStruct,label="EncryptedStruct");

    // Now let's decrypt our encrypted value.
    xxcryptStuffByKey(myStruct, "name", mySecretKey, "D") ;
    // This gives us ...
    writeDump(var=myStruct,label="DecryptedStruct");
</cfscript>