求救,如何将一个字符串加密? Java / J2SE / 基础类
来源:百度文库 编辑:神马文学网 时间:2024/05/23 20:06:58
可以采用对称算法或者是非对称算法来解决你的问题,我这里有一个项目中使用的对称加密解密算法,你可以直接拿来使用:
/**
* 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;
}
}
Top
/**
* 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;
}
}
Top
求救,如何将一个字符串加密? Java / J2SE / 基础类
如何将一个字符串加密
如何从java控制台获得用户输入? Java / J2SE / 基础类
谁知道怎么用htmlconverter,我下载了一个htmlconv12,但是不知道怎么用? Java / J2SE / 基础类
我该如何写这个策略文件????高手帮忙,再线等待,立即送分!!! Java / J2SE / 基础类
java反编译程序MOCHA,谁知怎么用 Java / J2SE / 基础类
如何才算掌握Java(J2SE篇)
如何才算掌握Java(J2SE篇)
如何将文件加密
如何将文件加密
如何才算掌握Java(J2SE篇)-太平洋电脑网Pconline-[Java]
如何将文件加密12
在java如何判断字符串有多少中文字?
如何才算真正的掌握Java语言(J2SE篇)
如何才算掌握Java(J2SE篇) --苦涩的youth
深入Java字符串
如何使用 ASP.NET 实用工具加密凭据和会话状态连接字符串
解析字符串成Java的基本数据类Scaner
java字符串的各种编码转换类ChangeCharset11
如何将文件夹隐藏并加密
如何将文件夹隐藏并加密1
如何将JAVA生成可执行文件?
如何将JAVA生成可执行文件
如何将JAVA生成可执行文件