一个java的DES加解密类转换成C#2
时间:2013-10-22
来源:Java认证考试网 浏览次数:63
【华夏培训网:中国教育培训第一门户】
* @return 加密后的字节数组 * @throws Exception */ public static byte[] encrypt(byte[] arrB, String key) throw
* @return 加密后的字节数组
* @throws Exception
*/
public static byte[] encrypt(byte[] arrB, String key) throws Exception {
DESKeySpec desKeySpec = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec ivp = new IvParameterSpec(DESPlus.iv);
Cipher encryptCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivp);
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
*
* @param xml
* 需加密的字符串
* @param key
* 密钥
* @return 加密后的字符串
* @throws Exception
*/
public static String encrypt(String xml, String key) throws Exception {
//return DESPlus.enc.encode(encrypt(xml.getBytes(), key));
return new String(encrypt(xml.getBytes(), key));
}
/**
* 使用默认公钥加密字符串
* @param xml 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
Java认证