storing PublicKey in MS Access

B

Bond

Hi! I'm using MS Access for a database to store a public key.

My java application should store the bytes in the database and then
load the bytes later in the application where the bytes are converted
back to the public key.

Here's my sql statement to insert the Public Key:

String sql = "INSERT INTO Users (username, password, publicKey, status)
VALUES ('"
+ username + "', '" + password + "', '"
+ publicKey.getEncoded() + "', " + 0 + ")";

The attribute publicKey is OLE Object.

Later in my application, I will load the public key again using
result.getByes("publicKey");

Then I do some translation to convert the bytes back to PublicKey (this
I have figured out no problem).

PROBLEM: My problem is that the bytes after loading the public key are
different from when i saved them.

For example:

when i first store the public key, I execute the following code:

public static void createAccount(String username, String password,
PublicKey publicKey) throws SQLException
{
System.out.println("Storing public key: " +
publicKey.getEncoded());
String sql = "INSERT INTO Users (username, password, publicKey,
status) VALUES ('"
+ username + "', '" + password + "', '"
+ publicKey.getEncoded() + "', " + 0 + ")";
ConnectionPool.executeUpdate(sql);
}

------> This prints: "Storing public key: [B@a18aa2

Later on, I load the public key (bytes):

public static AddressBook loadAddressBook(String username) throws
SQLException
{
String sql = "SELECT * from Contacts WHERE userOwner='" +
username + "'";
ResultSet results = ConnectionPool.executeQuery(sql);
AddressBook addressbook = new AddressBook();
while(results.next())
{
String contactUsername = results.getString("username");
byte[] temp = results.getBytes("publicKey");
System.out.println("Loading public key: " + temp);
PublicKey publicKey = Conversions.toPublicKey(temp);
int status = results.getInt("status");
Contact contact = new Contact(contactUsername, publicKey,
status);
addressbook.addContact(contact);
}

return addressbook;
}

-----> This prints: "Loading public key: [B@1a7bf11

I'm very confused about this and it's been bothering me for a long
time. I just want to store a PublicKey into MS Access and then load it
again later on. If anyone has any suggestions, please let me know.

Thanks in advance!
 
R

Roedy Green

INSERT INTO Users (username, password, publicKey,
what datatype is publicKey? It has better be some sort of blob/byte
type not character. If character you will get some translation.
 
Z

zhaoyh.hxtt

Bond said:
Hi! I'm using MS Access for a database to store a public key.

My java application should store the bytes in the database and then
load the bytes later in the application where the bytes are converted
back to the public key.

Here's my sql statement to insert the Public Key:

String sql = "INSERT INTO Users (username, password, publicKey, status)
VALUES ('"
+ username + "', '" + password + "', '"
+ publicKey.getEncoded() + "', " + 0 + ")";

The attribute publicKey is OLE Object.

Later in my application, I will load the public key again using
result.getByes("publicKey");

Then I do some translation to convert the bytes back to PublicKey (this
I have figured out no problem).

PROBLEM: My problem is that the bytes after loading the public key are
different from when i saved them.

For example:

when i first store the public key, I execute the following code:

public static void createAccount(String username, String password,
PublicKey publicKey) throws SQLException
{
System.out.println("Storing public key: " +
publicKey.getEncoded());
String sql = "INSERT INTO Users (username, password, publicKey,
status) VALUES ('"
+ username + "', '" + password + "', '"
+ publicKey.getEncoded() + "', " + 0 + ")";
ConnectionPool.executeUpdate(sql);
}

------> This prints: "Storing public key: [B@a18aa2

Later on, I load the public key (bytes):

public static AddressBook loadAddressBook(String username) throws
SQLException
{
String sql = "SELECT * from Contacts WHERE userOwner='" +
username + "'";
ResultSet results = ConnectionPool.executeQuery(sql);
AddressBook addressbook = new AddressBook();
while(results.next())
{
String contactUsername = results.getString("username");
byte[] temp = results.getBytes("publicKey");
System.out.println("Loading public key: " + temp);
PublicKey publicKey = Conversions.toPublicKey(temp);
int status = results.getInt("status");
Contact contact = new Contact(contactUsername, publicKey,
status);
addressbook.addContact(contact);
}

return addressbook;
}

-----> This prints: "Loading public key: [B@1a7bf11

I'm very confused about this and it's been bothering me for a long
time. I just want to store a PublicKey into MS Access and then load it
again later on. If anyone has any suggestions, please let me know.

Thanks in advance!
 
B

Bjorn Abelli

...
Hi! I'm using MS Access for a database to store a public key.
[snip]

PROBLEM: My problem is that the bytes after loading the
public key are different from when i saved them.

You haven't shown that they *are* different...

[snip]
System.out.println("Storing public key: " +
publicKey.getEncoded());
[snip]

------> This prints: "Storing public key: [B@a18aa2
[snip]

System.out.println("Loading public key: " + temp);
[snip]

-----> This prints: "Loading public key: [B@1a7bf11

Quite logical.

When you use the "println" method in this way, it doesn't print out the
*content* of the byte array. It only prints what comes out of the array's
toString-method.

In this case it prints out

- The type of array: [B (array of bytes)
- The at-sign: @
- The hexadecimal hashcode of the array-object

The two arrays are logically two different *objects*, with separate
identifiers, even though their contents very well might be the same. Hence
it would be no surprise to get two different hashcodes.
I'm very confused about this and it's been bothering me for
a long time. I just want to store a PublicKey into MS Access
and then load it again later on.

So, have you really checked that what you store isn't what you get?

// Bjorn A
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top