Encryption with Parameters C# SQL Server Not Saving Properly -
i trying insert encrypted data sql table. trying save 2 random numbers. here code generating random numbers:
random randomnumber = new random(); int key1 = randomnumber.next(10000000, 99999999); thread.sleep(1000); random randomnumber2 = new random(datetime.now.second); int key2 = randomnumber2.next(10000000, 99999999);
then insert table. first set of code works can't use because doesn't work random numbers. confirm have right:
sqlcommand cmd = new sqlcommand(@"open symmetric key keycodeskey decryption certificate keycodes; insert keyfile(encrypted_key1, encrypted_key2, startingkeydate) values (encryptbykey(key_guid('keycodeskey'), '19630515'), encryptbykey(key_guid('keycodeskey'), '19520921'), current_timestamp)", cx); int success = cmd.executenonquery();
this code prefer use can't seem work:
sqlcommand cmd = new sqlcommand(@"open symmetric key keycodeskey decryption certificate keycodes; insert keyfile(encrypted_key1, encrypted_key2, startingkeydate) values (encryptbykey(key_guid('keycodeskey'), @key1), encryptbykey(key_guid('keycodeskey'), @key2), current_timestamp)", cx); cx.open(); cmd.parameters.addwithvalue("@key1",key1.tostring()); cmd.parameters.addwithvalue("@key2", key2.tostring()); int success = cmd.executenonquery();
i not getting errors when @ unencrypted data, single digit number. if hard code parameters, not work either. works if actual numbers in sqlcommand.
any or advice appreciated!
this code ended using:
sqlcommand cmd = new sqlcommand(@"open symmetric key keycodeskey decryption certificate keycodes; insert keyfile(encrypted_key1, encrypted_key2, startingkeydate) values (encryptbykey(key_guid('keycodeskey'),"+key1.tostring()+@"), encryptbykey(key_guid('keycodeskey'), "+key2.tostring()+@"), current_timestamp)", cx); cx.open(); int success = cmd.executenonquery(); cx.close()
since windows forms application , there no possibility of injection issues, best solution come works.
Comments
Post a Comment