X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id AD3E352E07 for ; Thu, 11 Aug 2005 17:49:50 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 33562-06 for ; Thu, 11 Aug 2005 20:49:46 +0000 (GMT) Received: from mail.osl.com (216-21-133-98.ip.van.radiant.net [216.21.133.98]) by svr1.postgresql.org (Postfix) with ESMTP id 0BA4D52873 for ; Thu, 11 Aug 2005 17:49:43 -0300 (ADT) Received: from scottpj ([90.1.0.155]) (authenticated) by mail.osl.com (8.11.6/8.11.2) with ESMTP id j7BKpZc02508 for ; Thu, 11 Aug 2005 13:51:37 -0700 From: "Owen Jacobson" To: Subject: Re: Fwd: How to encrypt a column Date: Thu, 11 Aug 2005 13:50:45 -0700 Message-ID: <000901c59eb6$5977b120$9b00015a@osl.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook CWS, Build 9.0.2416 (9.0.2910.0) Importance: Normal In-Reply-To: <20050811203922.84270.qmail@web50602.mail.yahoo.com> X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.05 tagged_above=0 required=5 tests=[FORGED_RCVD_HELO=0.05] X-Spam-Level: X-Archive-Number: 200508/58 X-Sequence-Number: 22286 Jeff Lu wrote: > Hi, > > I'm interested in encrypting an column in table. Are there any > example using "C" to create the encrypted column, inserting and > retreiving data to/from it? > > the table is: > CREATE TABLE mytable ( > id SERIAL PRIMARY KEY, > crypted_content BYTEA > ); > > I'm getting (null) in the field with the following sql statement: > > strcpy(data, "data to be encrypted"); > sprintf(query_buff, "insert into mytable values('%s', '%s')", key, Try printing out the contents of 'query_buff' here. You shouldn't be getting NULLs in the table, but you may well be getting '' (the empty string) if encrypt(data, 'foo', 'bar') returns "" or (char *) (0). > encrypt(data, 'foo', 'bar')); > PQexec(conn, query_string); Even better, don't do it this way. This is potentially open to SQL injection attacks and definitely open to problems with "interesting" values of key. Look up PQprepare/PQexecParams for separating the query string from the values. I use ODBC indirectly, so I can't help you directly, but the docs at should help. > Another question is can the encrypted column be of type "text" ? Can't see any reason why not, so long as the encrypted data is represented as text.