打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
AVA版的DES加密解密
package com.ckms.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * 
 * @ClassName UtryDES
 * @Description JAVA版的DES加密解密 
 * @date 2012-7-31 下午10:51:30
 *
 */
public class UtryDES {

public static void main(String args[]) {
String str1 = encrypt("utryckms", "100");
System.out.println(str1);
String str2 = decrypt("utryckms", str1);
System.out.println(str2);
str2 = decrypt("CCmsUTry", "50D753E49242EF2C");
System.out.println(str2);
}

/**
* 加密
* @param strKey
* @param strToEncrypt
* @return
*/
public static String encrypt(String strKey, String strToEncrypt) {
String result = "";
try {
// 设置为DES/ECB/PKCS5Padding的Cipher
// ECB对应.Net的CipherMode.ECB
// PKCS5Padding对应为.NET和JAVA的默认填充方式
DESKeySpec ks = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);
Cipher cip = Cipher.getInstance("DES/ECB/PKCS5Padding");//Cipher.getInstance("DES");
// 设置为加密模式, 並设置解密的key
cip.init(Cipher.ENCRYPT_MODE, sk);
//考虑复杂字符所以加密时用UTF-8转换为byte[]
result = byteToHexStr(cip.doFinal(strToEncrypt.getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** strKey 长度大于8 */
public static String decrypt(String strKey, String strToDecrypt) {
String result = "";
try {
// 建立解密所需的Key
DESKeySpec ks = new DESKeySpec(strKey.getBytes("UTF-8"));
SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
SecretKey sk = skf.generateSecret(ks);

// 设置为DES/ECB/PKCS5Padding的Cipher
// ECB对应.Net的CipherMode.ECB
// PKCS5Padding对应为.NET和JAVA的默认填充方式
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// 设置为解密模式, 並设置解密的key
cipher.init(Cipher.DECRYPT_MODE, sk);

// 输出解密后的字串. 考虑复杂字符所以加密时用UTF-8转换为byte[], 解密时用UTF-8转换回去
// 考虑加密时可能有字符补位,String的trim()将这些空字符刪掉
result = new String(cipher.doFinal(hexStr2Byte(strToDecrypt)), "UTF-8").trim();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**将16进制转换为二进制 
* @param hexStr 
* @return 
*/  
public static byte[] hexStr2Byte(String hexStr) {
       if (hexStr.length() < 1)
        return null;
       byte[] result = new byte[hexStr.length()/2];
       for (int i = 0;i< hexStr.length()/2; i++) {
        //对应VB方法中的2位分段格式转换
        int hex = Integer.parseInt(hexStr.substring(i*2, i*2+2), 16);
        result[i] = (byte) (hex);
       }
       return result;
}

/**
* 将二进制 转换为16进制
* @param byteStr
* @return
*/
public static String byteToHexStr(byte[] byteStr) {
String str = "";
for(int i=0;i<byteStr.length ; i++) {
//考虑VB中的byte与JAVA中的byte取值范围差异,这里需要进行转换
String hex = Integer.toHexString(byteStr[i] & 0xFF);
            if (hex.length() == 1) {
            hex = '0' + hex;
            }
            str += hex;
}
return str.toUpperCase();
}

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Des加密(转:http://www.cnblogs.com/mingforyou/p/4107637.html)
[Python] 字符串与 hex 之间的相互转换
盘点 App 逆向中常见的加密算法!
Android: JAVA和C# 3DES加密解密
在线AES加密源码
非对称加密 (公钥加密私钥解密或者公钥解密私钥加密)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服