给文件内容加密,急,高手快点帮忙啊!!!!!!!!

xhm0616 2012-09-04 09:39:20
打开Number.txt文件时,我要显示乱码,让别人看不到该文件原来的具体内容!但是在程序中读取该文件中内容是该文件原本的内容!这个要怎么写啊,急~~~~~
...全文
280 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yymandy001 2012-09-04
  • 打赏
  • 举报
回复
同样需求,学习下
安特矮油 2012-09-04
  • 打赏
  • 举报
回复
很多现成的工具你可以使用,如果不需要太高的安全性的话,像1L说的,每个字节跟一个常量异或就可以了。
AlenOU 2012-09-04
  • 打赏
  • 举报
回复
这只是对字符串加密。看懂这个,对文件加密也就不难了!应该!
AlenOU 2012-09-04
  • 打赏
  • 举报
回复
package com.infoservice.dms.chana.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

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

/**
* @author oubocheng
*
*/
public class EncryptionAndDecryption {

/********************** 对称加密 *****************************/
/**将字符串加密成可解密但不可视的密文
* 对字符串加密
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public String Encrytor(String str, String secretKey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, keygen.generateKey());
byte[] src = str.getBytes();
// 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(cipherByte);
return str_;
}

/**
* 对字符串解密
*
* @param buff
* @return
* @throws Exception
*/
public String Decryptor(String str, String secretKey) throws Exception {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, keygen.generateKey());
//base64将不可见字符串转成可见字符串
BASE64Decoder decoder = new BASE64Decoder();
byte[] buff = decoder.decodeBuffer(str);
byte[] cipherByte = c.doFinal(buff);
String str_ = new String(cipherByte);
return str_;
}

/**
* 将字符串加密成可视但不可解密的无空格字符串
* MD5加密
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public String encrypMD5(String str) throws NoSuchAlgorithmException {
// 根据MD5算法生成MessageDigest对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes();
// 使用bytes更新摘要
md5.update(bytes);
// 完成哈希运算,生成不可解密的密文
byte[] result = md5.digest();
//用base64再次加密
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(result);
return str_;
}

/**
* 生成UUID
*
* @return
*/
public String uuid() {
String str = UUID.randomUUID().toString().toUpperCase();
str = str.replaceAll("-", "");
return str;
}

/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws Exception {
EncryptionAndDecryption de1 = new EncryptionAndDecryption();
String secret= de1.uuid();
String msg= "secret";
// DES加密
String enStr = de1.Encrytor(msg,secret);
// DES解密
String deStr = de1.Decryptor(enStr,secret);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + enStr);
System.out.println("解密后:" + deStr);
if (msg.equals(deStr))
System.out.println(true);
else
System.out.println(false);
System.out.println("MD5加密后:" + de1.encrypMD5(msg));
// BASE64Encoder encoder = new BASE64Encoder();
// System.out.println(encoder.encode("32010000000003".getBytes()));
byte[] bytes = compress("32010000000003");
String str = decompress(bytes);
System.out.println(bytes.toString().length());
System.out.println(str);
String strLong = Long.toString(32010000000003l, 36);
System.out.println("strLong==="+strLong);
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
}
public static final byte[] compress(String str) {
if (str == null)
return null;

byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;

try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}

return compressed;
}

public static final String decompress(byte[] compressed) {
if (compressed == null)
return null;

ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
// ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}

return decompressed;
}
}
AlenOU 2012-09-04
  • 打赏
  • 举报
回复
package com.infoservice.dms.chana.common;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

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

/**
* @author oubocheng
*
*/
public class EncryptionAndDecryption {

/********************** 对称加密 *****************************/
/**将字符串加密成可解密但不可视的密文
* 对字符串加密
* @param str
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public String Encrytor(String str, String secretKey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, keygen.generateKey());
byte[] src = str.getBytes();
// 加密,结果保存进cipherByte
byte[] cipherByte = c.doFinal(src);
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(cipherByte);
return str_;
}

/**
* 对字符串解密
*
* @param buff
* @return
* @throws Exception
*/
public String Decryptor(String str, String secretKey) throws Exception {
// 实例化支持DES算法的密钥生成器(算法名称命名需按规定,否则抛出异常)
KeyGenerator keygen = KeyGenerator.getInstance("DES");
// 生成Cipher对象,指定其支持的DES算法
Cipher c = Cipher.getInstance("DES");
//生成密钥
keygen.init(new SecureRandom(secretKey.getBytes()));
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, keygen.generateKey());
//base64将不可见字符串转成可见字符串
BASE64Decoder decoder = new BASE64Decoder();
byte[] buff = decoder.decodeBuffer(str);
byte[] cipherByte = c.doFinal(buff);
String str_ = new String(cipherByte);
return str_;
}

/**
* 将字符串加密成可视但不可解密的无空格字符串
* MD5加密
*
* @param str
* @return
* @throws NoSuchAlgorithmException
*/
public String encrypMD5(String str) throws NoSuchAlgorithmException {
// 根据MD5算法生成MessageDigest对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes();
// 使用bytes更新摘要
md5.update(bytes);
// 完成哈希运算,生成不可解密的密文
byte[] result = md5.digest();
//用base64再次加密
BASE64Encoder encoder = new BASE64Encoder();
String str_ = encoder.encode(result);
return str_;
}

/**
* 生成UUID
*
* @return
*/
public String uuid() {
String str = UUID.randomUUID().toString().toUpperCase();
str = str.replaceAll("-", "");
return str;
}

/**
* @param args
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws Exception {
EncryptionAndDecryption de1 = new EncryptionAndDecryption();
String secret= de1.uuid();
String msg= "secret";
// DES加密
String enStr = de1.Encrytor(msg,secret);
// DES解密
String deStr = de1.Decryptor(enStr,secret);
System.out.println("明文是:" + msg);
System.out.println("加密后:" + enStr);
System.out.println("解密后:" + deStr);
if (msg.equals(deStr))
System.out.println(true);
else
System.out.println(false);
System.out.println("MD5加密后:" + de1.encrypMD5(msg));
// BASE64Encoder encoder = new BASE64Encoder();
// System.out.println(encoder.encode("32010000000003".getBytes()));
byte[] bytes = compress("32010000000003");
String str = decompress(bytes);
System.out.println(bytes.toString().length());
System.out.println(str);
String strLong = Long.toString(32010000000003l, 36);
System.out.println("strLong==="+strLong);
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
System.out.println(Long.toString(32010000000003l, 36));
}
public static final byte[] compress(String str) {
if (str == null)
return null;

byte[] compressed;
ByteArrayOutputStream out = null;
ZipOutputStream zout = null;

try {
out = new ByteArrayOutputStream();
zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed = out.toByteArray();
} catch (IOException e) {
compressed = null;
} finally {
if (zout != null) {
try {
zout.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}

return compressed;
}

public static final String decompress(byte[] compressed) {
if (compressed == null)
return null;

ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
// ZipEntry entry = zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString();
} catch (IOException e) {
decompressed = null;
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
}

return decompressed;
}
}
MiceRice 2012-09-04
  • 打赏
  • 举报
回复
保存时就得加密,如果追求简单,就用对称密钥的加密算法。

比如DES之类的,Java自带支持的,参见:
http://www.icnote.com/des-encrypt/



如果还想更简单,直接对所保存文件逐个字节(byte)异或一个常量就行了,不过被解密的风险就大些。

13,097

社区成员

发帖
与我相关
我的任务
社区描述
Java J2ME
社区管理员
  • J2ME社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧