使用 structfind 和 structfindvalue 加密结构中的值
encrypting a value within a structure using structfind and structfindvalue
我有一个函数是 structform
到 cffunction
structform 是一个结构体,我尝试使用 structfind 来查找值并加密其值
我试过这样
<cfset fvalue = structfind(structform,"name")>
<cfset fvalue>
<cfset stuctform = encrypt(structfindvalue(
不确定如何以及如何修复它
我不确定我遵循的方法是否正确,任何指导将不胜感激
我无法保存 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>
我有一个函数是 structform
到 cffunction
structform 是一个结构体,我尝试使用 structfind 来查找值并加密其值
我试过这样
<cfset fvalue = structfind(structform,"name")>
<cfset fvalue>
<cfset stuctform = encrypt(structfindvalue(
不确定如何以及如何修复它
我不确定我遵循的方法是否正确,任何指导将不胜感激
我无法保存 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>