java代码如下:
package com.ypsoft.base.utils;
/**
* 创建于2008-11-7
*/
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
public class StringEncryptDecrypt {
/**
* 加密字符串
* @param strIn String 待加密的字符串
* @param key String 密钥
* @return String 加密后的字符串
* @throws Exception
*/
public static String encrypt(String strIn, String key) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes(), key));
}
/**
* 解密字符串
* @param strIn String 待解密的字符串
* @param key String 密钥
* @return String 解密后的字符串
* @throws Exception
*/
public static String decrypt(String strIn, String key) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn), key));
}
/**
* 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
* 互为可逆的转换过程
* @param strIn
* @return
* @throws Exception
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 将byte数组转换为表示16进制值的字符串 hexStr2ByteArr(String strIn) 互为可逆的转换过程
* @param arrB 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* 加密字节数组
* @param arrB
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] arrB, String key) throws Exception {
SecretKey deskey =new javax.crypto.spec.SecretKeySpec (key.getBytes() ,"DES" );
Cipher encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, deskey);
return encryptCipher.doFinal(arrB);
}
/**
* 解密字节数组
* @param arrB
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] arrB, String key) throws Exception {
SecretKey deskey =new javax.crypto.spec.SecretKeySpec (key.getBytes() ,"DES" );
Cipher decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, deskey);
return decryptCipher.doFinal(arrB);
}
}
package com.ypsoft.base.utils;
/**
* 创建于2008-11-25
*/
import java.util.UUID;
public class PasswordEncodeDecode {
/** 公钥 */
final static String PUBLIC_KEY = "xjhyrjgs";
/**
* 对用户密码加密
* @param userName 用户名
* @param password 用户密码
* @return String 加密后的密码
* @throws Exception
*/
public static String passwordEncode(String userName, String password)
throws Exception {
String key = StringEncryptDecrypt.encrypt(userName, PUBLIC_KEY);
System.out.println("第一次加密密匙"+key);
if (key.length() > 8) {
return StringEncryptDecrypt.encrypt(password, key.substring(0, 8));
} else {
return StringEncryptDecrypt.encrypt(password, key);
}
}
/**
* 对用户密码解密
* @param userName 用户名
* @param password 加密的用户密码
* @return String 解密的用户密码
* @throws Exception
*/
public static String passwordDecode(String userName, String password)
throws Exception {
String key = StringEncryptDecrypt.encrypt(userName, PUBLIC_KEY);
if (key.length() > 8) {
return StringEncryptDecrypt.decrypt(password, key.substring(0, 8));
} else {
return StringEncryptDecrypt.decrypt(password, key);
}
}
}
package com.ypsoft.base.utils;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class Test3DES {
public static void main(String[] args)throws Exception {
System.out.println("+++++++++++++++++++++++ 测试使用3DES加密解密 ++++++++++++++++++++");
System.out.println();
Scanner sc = new Scanner(System.in);
System.out.println("-------------> 开始进行加密操作 <-------------");
System.out.println("请输入用户名:");
String key1 = sc.nextLine();
System.out.println("请输入密码:");
String value1 = sc.nextLine();
System.out.println();
PasswordEncodeDecode pd=new PasswordEncodeDecode();
System.out.println("数据加密成功,加密结果为:" + pd.passwordEncode(key1, value1));
System.out.println();
System.out.println();
while(true) {
System.out.println("请选择解密or退出?1[解密],2[退出]:");
String select = sc.nextLine();
if("1".equals(select)){
System.out.println("-------------> 您选择了【解密】操作 <-------------");
System.out.println("请输入用户名:");
String key2 = sc.nextLine();
while(!key1.equals(key2)){
System.out.println("该key不存在,请重新输入key值:");
key2 = sc.nextLine();
}
System.out.println("请输入待解密的字符串:");
String value2 = sc.nextLine();
System.out.println();
System.out.println("调用原始密钥算解密结果为:" + pd.passwordDecode(key2, value2));
System.out.println();
}else if("2".equals(select)){
break;
}else{
System.out.println("指令错误!请重新输入,1[解密],2[退出]:");
}
}
System.out.println("----------测试使用3DES加密解密结束-----------");
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace UserLoginUI.Encrypt
{
#region Version Info
/*========================================================================
* 【本类功能概述】
*
* 作者: 时间:2015/5/12 10:59:12
* 文件名:StringEncryptDecrypt
* 版本:V1.0.1
* 说明:字符串加密解密类
* 修改者: 时间:
* 修改说明:
* ========================================================================
*/
#endregion
public class StringEncryptDecrypt
{
///
/// 字符串DES加密函数
///
/// 被加密字符串
/// 密钥
/// 加密后字符串
public static string Encode(string str, string key)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(str);
des.Mode = CipherMode.ECB;
des.Key = ASCIIEncoding.Default.GetBytes(key);
des.IV = ASCIIEncoding.Default.GetBytes(key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString().ToLower();
}
catch (Exception) { return "xxxx"; }
}
///
/// 字符串DES解密函数
///
/// 被解密字符串
/// 密钥
/// 解密后字符串
public static string Decode(string str, string key)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
byte[] inputByteArray = new byte[str.Length / 2];
for (int x = 0; x < str.Length / 2; x++)
{
int i = (Convert.ToInt32(str.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.Default.GetBytes(key);
des.IV = ASCIIEncoding.Default.GetBytes(key);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
catch (Exception) { return ""; }
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace UserLoginUI.Encrypt
{
#region Version Info
/*========================================================================
* 【本类功能概述】
*
* 作者: 时间:2015/5/12 10:59:03
* 文件名:PasswordEncodeDecode
* 版本:V1.0.1
* 说明:用户名、密码加密、解密操作类
* 修改者: 时间:
* 修改说明:
* ========================================================================
*/
#endregion
public class PasswordEncodeDecode
{
///
/// 公钥
///
static String PUBLIC_KEY = "xjhyrjgs";
///
/// 对用户密码加密
///
/// 用户名
/// 用户密码
/// 加密后的密码
public static String passwordEncode(String userName, String password)
{
String key = StringEncryptDecrypt.Encode(userName, PUBLIC_KEY);
if (key.Length > 8)
{
return StringEncryptDecrypt.Encode(password, key.Substring(0, 8).ToLower());
}
else
{
return StringEncryptDecrypt.Encode(password, key);
}
}
///
/// 对用户密码解密
///
/// 用户名
/// 加密的用户密码
/// 解密的用户密码
public static String passwordDecode(String userName, String password)
{
String key = StringEncryptDecrypt.Encode(userName, PUBLIC_KEY);
if (key.Length > 8)
{
return StringEncryptDecrypt.Decode(password, key.Substring(0, 8));
}
else
{
return StringEncryptDecrypt.Decode(password, key);
}
}
}
}