在Python代码中使用Mysql aes_encrypt()和aes_decrypt()函数

时间:2019-11-20 05:01:39

标签: python mysql sql python-3.x aes

我想使用Python的MYSQL aes_encrypt()和aes_decrypt()函数来加密重要信息。我知道,我们可以使用python crypto functions

来实现这些功能

我的方案是: 从POST请求中,我可以看到不同字段的列表

<input value={this.state.inputequation}/>

此JSON数据可能有所不同,例如它也可以具有其他键(key5,key6,....)或没有其他键。 因此,我需要构建一个动态SQL查询,例如

{
"email"  :  "abc@gmail.com",
"phone"     :   "1234567890",
"key1"       :  "value1",
"key2"       :  "value2",
"key3"       :  "value3"
}

sql = "Insert into dummy.test(email, phone, key1, key2, key3) values(email, phone, AES_ENCRYPT('value1', 'secret key'), AES_ENCRYPT('value2', 'secret key'), AES_ENCRYPT('value3', 'secret key'))"
cursor.execute(sql)

有没有可能实现上述任何一种方法? 这是好方法吗? 谢谢

1 个答案:

答案 0 :(得分:0)

由于BodyLayout是MySQL,而不是是Python函数,并且为了避免SQL注入攻击,您需要采用以下方式:

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title; 
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  List<Category> cats = [];
  List<Widget> _myLayouts = [];
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _myLayouts = [
      new BodyLayout("latest","",key: Key('1')),
      new BodyLayout("pop","",key: Key('2')),
    ];
  }
  void _onItemTapped(int index) {
    setState(() {
      _currentIndex = index; 
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: IndexedStack(children: _myLayouts, index: _currentIndex,),

      bottomNavigationBar: BottomNavigationBar(
      items: const <BottomNavigationBarItem>[
        BottomNavigationBarItem(
          icon: Icon(Icons.home),
          title: Text('Latest'),
        ),
        BottomNavigationBarItem(
          icon: Icon(Icons.business),
          title: Text('Fab'),
        ),
      ],
        currentIndex: _currentIndex,
        selectedItemColor: Colors.amber[800],
        onTap: _onItemTapped,
      ),
    );
  }
}



class BodyLayout extends StatefulWidget {
  final String mode;
  final String catKey;
  BodyLayout(this.mode,this.catKey,{Key key}) : super(key: key);
  @override
  _BodyLayoutState createState() => _BodyLayoutState();
}
class _BodyLayoutState extends State<BodyLayout>{

  void initState(){
    super.initState();


  }

  void onTapped(context,var url) {
   // I want to get variables '_currentIndex' of _MyHomePageState here.
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: <Widget>[
        ListView.builder(
          controller: _controller,
          itemCount: articles.length,
          itemBuilder: (context, index) {
            return ListTile(
              title: Text(articles[index].title),
              onTap: () => onTapped(context,articles[index].url),
            );
          },
        ),
      ]
    );
  }
}

其中AES_ENCRYPTsql = """Insert into dummy.test(email, phone, key1, key2, key3) values(%s, %s, AES_ENCRYPT(%s, 'secret key'), AES_ENCRYPT(%s, 'secret key'), AES_ENCRYPT(%s, 'secret key'))""" cursor.execute(sql, (email_value, phone_value, key1_value, key2_value, key3_value)) 等分别是表单字段email_valuephone_value等发布的值。