java字符串与二进制文件问题
现有一个字符串,如:123456 ,我想把它写进文件里,别人拿到这个文件打开时提示乱码,这个该怎么做呢?提供代码的,多加分哦 --------------------编程问答-------------------- 这是对象跟二进制数组的互换静态方法
--------------------编程问答-------------------- 字符串加密解密一下就行了
/**
* 将object对象转换为byte[]数组
*
* @param obj
* @return
*/
public static byte[] ObjectToByte(java.lang.Object obj) {
byte[] bytes = new byte[1024];
try {
// object to bytearray
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(obj);
bytes = bo.toByteArray();
bo.close();
oo.close();
} catch (Exception e) {
System.out.println("translation" + e.getMessage());
e.printStackTrace();
}
return (bytes);
}
/**
* 将byte[]转换为object数组
*
* @param bytes
* @return
*/
public static Object ByteToObject(byte[] bytes) {
Object obj = new Object();
try {
// bytearray to object
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
obj = oi.readObject();
bi.close();
oi.close();
} catch (Exception e) {
System.out.println("translation" + e.getMessage());
e.printStackTrace();
}
return obj;
}
http://www.cnblogs.com/lianghuilin/archive/2013/04/15/3des.html --------------------编程问答-------------------- 加密呗 --------------------编程问答-------------------- 加密吧:
--------------------编程问答-------------------- 我没看清楚就恢复了 --------------------编程问答--------------------
import java.security.Key;
import java.security.Security;
import javax.crypto.Cipher;
/**
* DES加密和解密工具,可以对字符串进行加密和解密操作 。
*/
public class DesUtils {
/** 字符串默认键值 */
private static String strDefaultKey = "national";
/** 加密工具 */
private Cipher encryptCipher = null;
/** 解密工具 */
private Cipher decryptCipher = null;
/**
* 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:leo841001@163.com">LiGuoQing</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public DesUtils() throws Exception {
this(strDefaultKey);
}
/**
* 指定密钥构造方法
*
* @param strKey
* 指定的密钥
* @throws Exception
*/
public DesUtils(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args) {
try {
String test = "admin";
DesUtils des = new DesUtils("test");// 自定义密钥
System.out.println("加密前的字符:" + test);
System.out.println("加密后的字符:" + des.encrypt(test));
System.out.println("解密后的字符:" + des.decrypt(des.encrypt(test)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
加密后的数据 我明白,但是你把数据加密后,客户拿到文件,用txt打开还是可以修改加密的值,只不过可能读数据的时候读不出来,我需要生成的文件,客户打不开 --------------------编程问答--------------------
我用你的方法,生成的文件用txt还是可以打开呀 我想生成一个用户打不开的文件,他不能修改里面的内容 --------------------编程问答--------------------
写文件的时候把txt设置成readOnly
--------------------编程问答--------------------
File f = new File("a.txt");
f.setReadOnly();
这是对象跟二进制数组的互换静态方法
/**
* 将object对象转换为byte[]数组
*
* @param obj
* @return
*/
public static byte[] ObjectToByte(java.lang.Object obj) {
byte[] bytes = new byte[1024];
try {
// object to bytearray
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
oo.writeObject(obj);
bytes = bo.toByteArray();
bo.close();
oo.close();
} catch (Exception e) {
System.out.println("translation" + e.getMessage());
e.printStackTrace();
}
return (bytes);
}
/**
* 将byte[]转换为object数组
*
* @param bytes
* @return
*/
public static Object ByteToObject(byte[] bytes) {
Object obj = new Object();
try {
// bytearray to object
ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
ObjectInputStream oi = new ObjectInputStream(bi);
obj = oi.readObject();
bi.close();
oi.close();
} catch (Exception e) {
System.out.println("translation" + e.getMessage());
e.printStackTrace();
}
return obj;
}
我用你的方法,生成的文件用txt还是可以打开呀 我想生成一个用户打不开的文件,他不能修改里面的内容
写文件的时候把txt设置成readOnly
File f = new File("a.txt");
f.setReadOnly();
哪能设置成只读呀 用户右键属性 只读一勾 就又能改了 我意思是能不能输出成二进制文件不能被打开那样的
--------------------编程问答-------------------- 输出成不可显示的文件很容易,比如,我的代码把字符串转换成字节数组,然后给每个字节用0xaa异或,再写入文件, 看上去就是乱码。
楼主参考一下:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class ChangeStringToEditless {
public static void main(String[] args) {
//待测字符串
String s = "12345 67890 \r\n" +
"abcdef \r\n" +
"还认得吗? \r\n";
//写入文件路径
String fileOriginalPath = "d:\\myjava\\test20131212\\original.txt";
String fileChangedPath = "d:\\myjava\\test20131212\\fileChanged.own";
File fileOriginal = new File(fileOriginalPath);
File fileChanged = new File(fileChangedPath);
//保存原字符串。
try {
if (!fileOriginal.exists()) {
fileOriginal.createNewFile();
}
FileWriter fw = new FileWriter(fileOriginal);
fw.append(s);
fw.flush();
fw.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
//写入改变后的字符串。
byte[] byteChanged = getChangedString(s);
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
if (!fileChanged.exists()) {
fileChanged.createNewFile();
}
fos = new FileOutputStream(fileChanged);
bos = new BufferedOutputStream(fos);
bos.write(byteChanged, 0, byteChanged.length);
bos.flush();
bos.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 用0xaa对字节进行异或操作。返回改编后的数组。
* @param s
* @return
*/
public static byte[] getChangedString(String s) {
byte[] unchanged = null;
byte[] result = null;
try {
unchanged = s.getBytes("utf-8");
}
catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
if(null != unchanged) {
result = new byte[unchanged.length];
for (int i = 0; i < unchanged.length; i++) {
int temp = unchanged[i] ^ 0xaa;//先转换成了int型,再操作的。
result[i] = (byte) (temp & 0xff);//把int型,再换成byte型。
}
}
return result;
}
}
--------------------编程问答-------------------- 下面的代码,把"乱码"文件,再恢复成原来的样子。
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class RebuildString {
public static void main(String[] args) {
String fileChangedPath = "d:\\myjava\\test20131212\\fileChanged.own";
String fileRebuildPath = "d:\\myjava\\test20131212\\fileRebuild.txt";
//还原后的内容写入文件,文件不存在,新建。
File fileRebuild = new File(fileRebuildPath);
try {
if(!fileRebuild.exists()) {
fileRebuild.createNewFile();
}
}catch (IOException ioe) {
ioe.printStackTrace();
fileRebuild = null;
}
if(null == fileRebuild) {
System.out.println("file create failed!");
return;
}
//读原文件,处理后写入新文件。以字节流读,以字符流写。
File fileChanged = new File(fileChangedPath);
BufferedInputStream bis= null;
FileInputStream fis = null;
byte[] toberead = new byte[1024];
String s = "";
try {
FileWriter fw = new FileWriter(fileRebuild);
fis = new FileInputStream(fileChanged);
bis = new BufferedInputStream(fis);
int n;
while ((n = bis.read(toberead)) != -1) {//循环读写。
s = rebuildString(toberead, n);
fw.append(s);
}
bis.close();
fw.flush();
fw.close();
}
catch(IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 用0xaa(10101010)对每个字节进行异或操作。还原原来的字符。以字符串返回。
* @param input
* @param n
* @return
*/
public static String rebuildString(byte[] input, int n) {
byte[] tempByte = new byte[n];
for(int i = 0; i< n; i++) {
int temp = input[i] ^ 0xaa;
tempByte[i] = (byte)(temp & 0xff);//还原字节。
}
String s = "";
try {
s = new String(tempByte, "utf-8");
}
catch(UnsupportedEncodingException uee) {
uee.printStackTrace();
}
return s;
}
}
补充:Java , Java相关