views:

86

answers:

3

Hello everyone,

I am working on encryption-decryption program. Program gets an input from the user and encrypts it. Then it stores the encrypted data in ms access database table. Later, the data is retrieved from the table , decrypted and given back to the user.

I am storing the data as text in the ms access. The encryption algorithm returns a byte array of size 16. But when i retrieve the data from the database, i am getting a byte array of size 8 only. Help me to get through this...

+2  A: 

I think the problem is that you are using it as text while it isn't (it is binary data). The halving of the length sounds like a Unicode related issue (i.e. the 'text' is stored as wide with two bytes for character, but retrieved as one byte per character).

Ofir
thanks, but is there any compatible datatype for storing binary data?
Use a BLOB to store your information.
Borealid
It's not necessary to use a BLOB field if you do the appropriate conversions on retrieving and storing data.
David-W-Fenton
A: 

one possible solution is to encode the cipher text as String using Base64 encoding

you can use Appache Commons Library for that: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

Edited: i dont know why you want MS-ACCESS Specific solution ! the DMBS may change, the OS also may change.. you must to write general solution that can work in many cases..

here small example for using Base64 Encoder/Decoder:

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; 

import java.io.IOException ;

public class Decoder {
    public static void main(String[] args) throws IOException{
        byte[] cipherBytes = "stackoverflow".getBytes();    // say this the is encrypted bytes

        String encodedBytes = new BASE64Encoder().encode(cipherBytes);
        System.out.println("stored as: " + encodedBytes );

        byte[] decodedBytes = new BASE64Decoder().decodeBuffer(encodedBytes); 
        System.out.println("extracted as: " + new String(decodedBytes) );
    }
}

Note: this code using Internal Sun Classes (BASE64Encoder/Decoder) and its not recommended to use these classes in your program because it may change in the next version of JDK.

using BASE64 Encoder/Decoder in Appache Commons is better.

if you want the MS-ACCESS solution, try to store the ciphertext in LONGBINARY , see this: http://stackoverflow.com/questions/3002026/how-to-specify-blob-type-in-ms-access

Wajdy Essam
Note his tags. How can this be used inside MS Access?
Tony Toews
if you read the wiki article: "base64 encode binary data by treating it numerically" you want to store binary data ? there is many options, the first is store the binary in "binary field" is the DBMS you work with. second option that i prefere, is to convert binary data (cipher text) to textual format, then store the result in normal string field, as you store users names. you can apply this by using Base64 encoding, you encode cipher text then store it in db.when you want the ciphertext from db you will preform the opposite operation, decoding string.
Wajdy Essam
And how can this be used inside MS Access? That is, your reponse to Tony's inquiry does not include anything Access-specific at all, and the question is about Access. -1
David-W-Fenton
A: 

I have an app that stores encrypted credit card numbers using the MS Crypto interface. I got the code from the MS Knowledge Base, and the key thing is running ByteToString() and StringToByte() conversion in the proper places. I'm storing the actual data in a plain Jet text field and have had no problems whatsoever.

David-W-Fenton
-1 ByteToString() and StringToByte() conversions is not related to MS-ACCESS. these is converter/encoder methods and anyone can write similar methods without using MS Crypto interface! don't use/copy/paste code without understanding of what the code do.
Wajdy Essam
There is sample VB code in the Knowledge Base article, and the point is that if you convert from string-to-byte and byte-to-string, it doesn't matter if you use a text data type for storing your encrypted values. The point is that you have to keep track of where you need to convert, and that was the point of my citing the method I used in Access -- not because it was Access-specific, but as an example of how you avoid issues with the data type you're storing the encrypted value in.
David-W-Fenton
your right and i agree with you. This convertion work as (encoder/decoder) method work, in the end they convert from bytes to string and from string to bytse.. see the example in my answer. so why you not like my answer and decrease it while its smiliar to your answer, they both convert from bytes to string and from string to bytes (whereas my answer is also better than your solution because its independent from MS-Crypto API ). plz think twice before you hit decrease/increase button because it's unfair. with regards.
Wajdy Essam