怎么用java加密 比如给一个文本文件里的内容加密都有什么方法?怎么实现?简单的是ASCII +1怎么实现??或更复杂的有什么办法,怎么实现 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package net.cqpower.util;/** * <p>Title: puwei CRM</p> * <p>Description: Chong Qing puwei CRM</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: puwei</p> * @author ZhangPu * @version 1.0 */import Acme.Crypto.*;import java.io.PrintStream;// Referenced classes of package com.eastsun.sys:// SysConfigpublic class Encrypt{ public Encrypt() { } public static String decrypt(String s) { byte abyte0[] = new byte[8]; BlowfishCipher blowfishcipher = new BlowfishCipher("cqpower_crm"); blowfishcipher.decrypt(CryptoUtils.toBlockString(s), abyte0); return new String(abyte0); } public static String encrypt(String s) { byte abyte0[] = new byte[8]; BlowfishCipher blowfishcipher = new BlowfishCipher("cqpower_crm"); blowfishcipher.encrypt(s.getBytes(), abyte0); return CryptoUtils.toStringBlock(abyte0); } public static void main(String args[]) { System.out.println("Key = " + encrypt("[322c101d5939d36e]")); System.out.println("Key = " + decrypt(encrypt("soft" + "em" + "ware"))); }} 不能用md5,如果用md5加密后,则内容不能还原。可以用对称加密算法(DES,TripleDES,Rijndael)或者是非对称加密算法(RSA、DSA)来解决你的问题。下面是我在项目中采用的一个DES加密算法。/** * 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; }} 为什么线程的同步比不同步要慢 如何设置滚动条的长度 初学者一个关于JDBC和Oracle的问题 作为JAVA新手应该怎么办~!! 跪求!!!一个servlet实现文件上传的例子 java几套练习题求答案 一个访问http网页的问题,谢谢赐教 编写一个简单程序 字符转换问题,高手请进,急救!!!!!!!!!!! 如何发布一个基于JAVA的应用 求DELPHI加密JAVA解密的算法 如何将gb2312字符串转成utf-8字符串?
* <p>Title: puwei CRM</p>
* <p>Description: Chong Qing puwei CRM</p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: puwei</p>
* @author ZhangPu
* @version 1.0
*/import Acme.Crypto.*;
import java.io.PrintStream;// Referenced classes of package com.eastsun.sys:
// SysConfigpublic class Encrypt
{ public Encrypt()
{
} public static String decrypt(String s)
{
byte abyte0[] = new byte[8];
BlowfishCipher blowfishcipher = new BlowfishCipher("cqpower_crm");
blowfishcipher.decrypt(CryptoUtils.toBlockString(s), abyte0);
return new String(abyte0);
} public static String encrypt(String s)
{
byte abyte0[] = new byte[8];
BlowfishCipher blowfishcipher = new BlowfishCipher("cqpower_crm");
blowfishcipher.encrypt(s.getBytes(), abyte0);
return CryptoUtils.toStringBlock(abyte0);
} public static void main(String args[])
{
System.out.println("Key = " + encrypt("[322c101d5939d36e]"));
System.out.println("Key = " + decrypt(encrypt("soft" + "em" + "ware")));
}
}
可以用对称加密算法(DES,TripleDES,Rijndael)或者是非对称加密算法(RSA、DSA)来解决你的问题。下面是我在项目中采用的一个DES加密算法。
/**
* 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;
}
}