文件加密 我想对保存在本地上的文件进行加密,比如说文件里保存了一些帐号和密码,我就要对本地文件进行加密,请问用JAVA怎么来实现啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 java本身的包里就带有各种加密的工具 楼上 zgysc(翠湖寒) 所说的我不大清楚,能不能说更清楚一些,谢谢! Class DESedeKeySpec 里好像有des的加密的吧,具体不清楚,查下API吧java还提供了rsa的加密,不过rsa 速度较慢的说。顺便说下,怎么不到java区问啊 下面是对称加密算法中的一种,我在项目中用到的,可以对字符串和byte[]进行加密,解密/** * CryptionData.java * @version 1.0 * @author 2005/4/20 Yao (WICT) */package DES;import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.*;import sun.misc.*;/** * DES encryption algorithm, providing the encryption and decryption * algorithm for byte array and string * @author : Yao (WICT) * @version 1.0 */public class CryptionData { // The length of Encryptionstring should be 8 bytes and not be // a weak key private String EncryptionString; // The initialization vector should be 8 bytes private final byte[] EncryptionIV = "abcdefgh".getBytes(); private final static String DES = "DES/CBC/PKCS5Padding"; /** * Saving key for encryption and decryption * @param EncryptionString String */ public CryptionData(String EncryptionString) { this.EncryptionString = EncryptionString; } /** * Encrypt a byte array * @param SourceData byte[] * @throws Exception * @return byte[] */ public byte[] EncryptionByteData(byte[] SourceData) throws Exception { byte[] retByte = null; // Create SecretKey object byte[] EncryptionByte = EncryptionString.getBytes(); DESKeySpec dks = new DESKeySpec(EncryptionByte); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); // Create IvParameterSpec object with initialization vector IvParameterSpec spec=new IvParameterSpec(EncryptionIV); // Create Cipter object Cipher cipher = Cipher.getInstance(DES); // Initialize Cipher object cipher.init(Cipher.ENCRYPT_MODE, securekey, spec); // Encrypting data retByte = cipher.doFinal(SourceData); return retByte; } /** * Decrypt a byte array * @param SourceData byte[] * @throws Exception * @return byte[] */ public byte[] DecryptionByteData(byte[] SourceData) throws Exception { byte[] retByte = null; // Create SecretKey object byte[] EncryptionByte = EncryptionString.getBytes(); DESKeySpec dks = new DESKeySpec(EncryptionByte); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(dks); // Create IvParameterSpec object with initialization vector IvParameterSpec spec=new IvParameterSpec(EncryptionIV); // Create Cipter object Cipher cipher = Cipher.getInstance(DES); // Initialize Cipher object cipher.init(Cipher.DECRYPT_MODE, securekey, spec); // Decrypting data retByte = cipher.doFinal(SourceData); return retByte; } /** * Encrypt a string * @param SourceData String * @throws Exception * @return String */ public String EncryptionStringData(String SourceData) throws Exception { String retStr = null; byte[] retByte = null; // Transform SourceData to byte array byte[] sorData = SourceData.getBytes(); // Encrypte data retByte = EncryptionByteData(sorData); // Encode encryption data BASE64Encoder be = new BASE64Encoder(); retStr = be.encode(retByte); return retStr; } /** * Decrypt a string * @param SourceData String * @throws Exception * @return String */ public String DecryptionStringData(String SourceData) throws Exception { String retStr = null; byte[] retByte = null; // Decode encryption data BASE64Decoder bd = new BASE64Decoder(); byte[] sorData = bd.decodeBuffer(SourceData); // Decrypting data retByte = DecryptionByteData(sorData); retStr = new String(retByte); return retStr; }} 关于类继承的小问题 图片的连续播放问题,求解决~~ Java 下如何依次读取一个目录下的所有文件 有空进来看看 JTable的问题 以下程序是如何实现递归的 一个超难的问题,特向各位请教 请教几个问题! 爲什麽我的JTable的model的removeRow方法總是出錯 请问如何在JTable中实现单元格数据的实时计算?重谢! 再一次变量的自增问题 MD5加密中文字符? 求DELPHI加密JAVA解密的算法
java还提供了rsa的加密,不过rsa 速度较慢的说。
顺便说下,怎么不到java区问啊
/**
* CryptionData.java
* @version 1.0
* @author 2005/4/20 Yao (WICT)
*/
package DES;import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.*;
import sun.misc.*;/**
* DES encryption algorithm, providing the encryption and decryption
* algorithm for byte array and string
* @author : Yao (WICT)
* @version 1.0
*/
public class CryptionData {
// The length of Encryptionstring should be 8 bytes and not be
// a weak key
private String EncryptionString; // The initialization vector should be 8 bytes
private final byte[] EncryptionIV = "abcdefgh".getBytes();
private final static String DES = "DES/CBC/PKCS5Padding"; /**
* Saving key for encryption and decryption
* @param EncryptionString String
*/
public CryptionData(String EncryptionString) {
this.EncryptionString = EncryptionString;
} /**
* Encrypt a byte array
* @param SourceData byte[]
* @throws Exception
* @return byte[]
*/
public byte[] EncryptionByteData(byte[] SourceData) throws Exception {
byte[] retByte = null; // Create SecretKey object byte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks); // Create IvParameterSpec object with initialization vector
IvParameterSpec spec=new IvParameterSpec(EncryptionIV); // Create Cipter object
Cipher cipher = Cipher.getInstance(DES); // Initialize Cipher object
cipher.init(Cipher.ENCRYPT_MODE, securekey, spec); // Encrypting data
retByte = cipher.doFinal(SourceData);
return retByte;
} /**
* Decrypt a byte array
* @param SourceData byte[]
* @throws Exception
* @return byte[]
*/
public byte[] DecryptionByteData(byte[] SourceData) throws Exception {
byte[] retByte = null; // Create SecretKey object
byte[] EncryptionByte = EncryptionString.getBytes();
DESKeySpec dks = new DESKeySpec(EncryptionByte);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey securekey = keyFactory.generateSecret(dks); // Create IvParameterSpec object with initialization vector
IvParameterSpec spec=new IvParameterSpec(EncryptionIV); // Create Cipter object
Cipher cipher = Cipher.getInstance(DES); // Initialize Cipher object
cipher.init(Cipher.DECRYPT_MODE, securekey, spec); // Decrypting data
retByte = cipher.doFinal(SourceData); return retByte;
} /**
* Encrypt a string
* @param SourceData String
* @throws Exception
* @return String
*/
public String EncryptionStringData(String SourceData) throws Exception {
String retStr = null;
byte[] retByte = null; // Transform SourceData to byte array
byte[] sorData = SourceData.getBytes(); // Encrypte data
retByte = EncryptionByteData(sorData); // Encode encryption data
BASE64Encoder be = new BASE64Encoder();
retStr = be.encode(retByte); return retStr;
} /**
* Decrypt a string
* @param SourceData String
* @throws Exception
* @return String
*/
public String DecryptionStringData(String SourceData) throws Exception {
String retStr = null;
byte[] retByte = null; // Decode encryption data
BASE64Decoder bd = new BASE64Decoder();
byte[] sorData = bd.decodeBuffer(SourceData); // Decrypting data
retByte = DecryptionByteData(sorData);
retStr = new String(retByte); return retStr;
}
}