开发者

Problem in retrieving a decrypted data

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 databa开发者_StackOverflow社区se 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...


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).


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.


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: How to specify blob type in MS Access?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜