最简单的办法就是对这个文件加密,在程序启动时对它解密后读取内存中的hashtable。
程序运行完毕,可以把hashtable中的内容转为一个长字符串,再加密,最后写入文件。此处涉及的关键有:
1.加密,解密
2.hashtable--->加密--->string
3.string--->解密--->hashtable1.可以用PBE,MD5,DES,或者是最简单的Caesar加密。
2.name=abc\nage=20\nsex=true\n 加密再写入文件
3.读取所有数据,解密,再从中readline,解析后放入hashtable
程序运行完毕,可以把hashtable中的内容转为一个长字符串,再加密,最后写入文件。此处涉及的关键有:
1.加密,解密
2.hashtable--->加密--->string
3.string--->解密--->hashtable1.可以用PBE,MD5,DES,或者是最简单的Caesar加密。
2.name=abc\nage=20\nsex=true\n 加密再写入文件
3.读取所有数据,解密,再从中readline,解析后放入hashtable
DES stands for Data Encryption Standard, uses dynamic key. it's not reasonable to save the SecretKey at somewhere else to encrypt and decrypt. the SecretKey could be stolen as well.
Finally, u might have to use PBE. Sample code:
PBEKeySpec key_spec = new PBEKeySpec(password); // password is a char[].
SecretKeyFactory key_factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey key = key_factory.generateSecret(key_spec);
Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
c.init(Cipher.ENCRYPT_MODE, key);
return c.doFinal(src);//src is a byte[] of what you wanna encrypt.
// returns an encrypted byte array.
Problem: the encrypted byte array cannot be converted to String, how do you save in the properties file which is text based.
Suggestion:
1. Use Object Serialization save to a binary file and read back. The serialized codes don't make too much scense when it's open with UltraEditor or others.
2. Implement your own function/method to scramble a String to another String. and it has to be able to reversed.
SwingClient <---> DataManagerTier <---JDBC---> Database.
不过看你们的具体需求而定.这样未必就是最好的方式,而且改动量较大.
我想你只要清楚md5是个不可逆的过程即可。
但是为了防止用户随意的修改次文件,所以我想你需要对文件进行数字签名。
import java.security.*;class GenSig { public static void main(String[] args) { /* Generate a DSA signature */ if (args.length != 1) {
System.out.println("Usage: GenSig nameOfFileToSign");
}
else try{ /* Generate a key pair */ KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA", "SUN");
SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); keyGen.initialize(1024, random); KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
/* Create a Signature object and initialize it with the private key */ Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); dsa.initSign(priv); /* Update and sign the data */ FileInputStream fis = new FileInputStream(args[0]);
BufferedInputStream bufin = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int len;
while (bufin.available() != 0) {
len = bufin.read(buffer);
dsa.update(buffer, 0, len);
}; bufin.close(); /* Now that all the data to be signed has been read in,
generate a signature for it */ byte[] realSig = dsa.sign();
/* Save the signature in a file */
FileOutputStream sigfos = new FileOutputStream("sig");
sigfos.write(realSig); sigfos.close();
/* Save the public key in a file */
byte[] key = pub.getEncoded();
FileOutputStream keyfos = new FileOutputStream("suepk");
keyfos.write(key); keyfos.close(); } catch (Exception e) {
System.err.println("Caught exception " + e.toString());
} };}
import java.io.*;
import java.security.*;
import java.security.spec.*;class VerSig { public static void main(String[] args) { /* Verify a DSA signature */ if (args.length != 3) {
System.out.println("Usage: VerSig publickeyfile signaturefile datafile");
}
else try{ /* import encoded public key */ FileInputStream keyfis = new FileInputStream(args[0]);
byte[] encKey = new byte[keyfis.available()];
keyfis.read(encKey); keyfis.close(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey); KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); /* input the signature bytes */
FileInputStream sigfis = new FileInputStream(args[1]);
byte[] sigToVerify = new byte[sigfis.available()];
sigfis.read(sigToVerify ); sigfis.close(); /* create a Signature object and initialize it with the public key */
Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
sig.initVerify(pubKey); /* Update and verify the data */ FileInputStream datafis = new FileInputStream(args[2]);
BufferedInputStream bufin = new BufferedInputStream(datafis); byte[] buffer = new byte[1024];
int len;
while (bufin.available() != 0) {
len = bufin.read(buffer);
sig.update(buffer, 0, len);
}; bufin.close();
boolean verifies = sig.verify(sigToVerify); System.out.println("signature verifies: " + verifies);
} catch (Exception e) {
System.err.println("Caught exception " + e.toString());
}; }}
1、MD5算法是对输入的数据进行补位,使得如果数据位长度LEN对512求余的结果
是448。
即数据扩展至K*512+448位。即K*64+56个字节,K为整数。
具体补位操作:补一个1,然后补0至满足上述要求
2、补数据长度:
用一个64位的数字表示数据的原始长度B,把B用两个32位数表示。这时,数据
就被填
补成长度为512位的倍数。
3. 初始化MD5参数
四个32位整数 (A,B,C,D) 用来计算信息摘要,初始化使用的是十六进制表示
的数字
A=0X01234567
B=0X89abcdef
C=0Xfedcba98
D=0X76543210
4、处理位操作函数
X,Y,Z为32位整数。
F(X,Y,Z) = X&Y|NOT(X)&Z
G(X,Y,Z) = X&Z|Y¬(Z)
H(X,Y,Z) = X xor Y xor Z
I(X,Y,Z) = Y xor (X|not(Z))
5、主要变换过程:
使用常数组T[1 ... 64], T为32位整数用16进制表示,数据用16个32位的
整
数数组M[]表示。
具体过程如下:
/* 处理数据原文 */
For i = 0 to N/16-1 do
/*每一次,把数据原文存放在16个元素的数组X中. */
For j = 0 to 15 do
Set X[j] to M[i*16+j].
end /结束对J的循环
/* Save A as AA, B as BB, C as CC, and D as DD. */
AA = A
BB = B
CC = C
DD = D
/* 第1轮*/
/* 以 [abcd k s i]表示如下操作
a = b + ((a + F(b,c,d) + X[k] + T) <<< s). */
/* Do the following 16 operations. */
[ABCD 0 7 1] [DABC 1 12 2] [CDAB 2 17 3] [BCDA 3 22 4]
[ABCD 4 7 5] [DABC 5 12 6] [CDAB 6 17 7] [BCDA 7 22 8]
[ABCD 8 7 9] [DABC 9 12 10] [CDAB 10 17 11] [BCDA 11 22 12]
[ABCD 12 7 13] [DABC 13 12 14] [CDAB 14 17 15] [BCDA 15 22 16]
/* 第2轮* */
/* 以 [abcd k s i]表示如下操作
a = b + ((a + G(b,c,d) + X[k] + T) <<< s). */
/* Do the following 16 operations. */
[ABCD 1 5 17] [DABC 6 9 18] [CDAB 11 14 19] [BCDA 0 20 20]
[ABCD 5 5 21] [DABC 10 9 22] [CDAB 15 14 23] [BCDA 4 20 24]
[ABCD 9 5 25] [DABC 14 9 26] [CDAB 3 14 27] [BCDA 8 20 28]
[ABCD 13 5 29] [DABC 2 9 30] [CDAB 7 14 31] [BCDA 12 20 32]
/* 第3轮*/
/* 以 [abcd k s i]表示如下操作
a = b + ((a + H(b,c,d) + X[k] + T) <<< s). */
/* Do the following 16 operations. */
[ABCD 5 4 33] [DABC 8 11 34] [CDAB 11 16 35] [BCDA 14 23 36]
[ABCD 1 4 37] [DABC 4 11 38] [CDAB 7 16 39] [BCDA 10 23 40]
[ABCD 13 4 41] [DABC 0 11 42] [CDAB 3 16 43] [BCDA 6 23 44]
[ABCD 9 4 45] [DABC 12 11 46] [CDAB 15 16 47] [BCDA 2 23 48]
/* 第4轮*/
/* 以 [abcd k s i]表示如下操作
a = b + ((a + I(b,c,d) + X[k] + T) <<< s). */
/* Do the following 16 operations. */
[ABCD 0 6 49] [DABC 7 10 50] [CDAB 14 15 51] [BCDA 5 21 52]
[ABCD 12 6 53] [DABC 3 10 54] [CDAB 10 15 55] [BCDA 1 21 56]
[ABCD 8 6 57] [DABC 15 10 58] [CDAB 6 15 59] [BCDA 13 21 60]
[ABCD 4 6 61] [DABC 11 10 62] [CDAB 2 15 63] [BCDA 9 21 64]
/* 然后进行如下操作 */
A = A + AA
B = B + BB
C = C + CC
D = D + DD
end /* 结束对I的循环*/
6、输出结果。