php+c+通用加密,PHP、C#、通用的DES加密详解

PHP

class JoDES {

private static $_instance = NULL;

/**

* @return JoDES

*/

public static function share() {

if (is_null(self::$_instance)) {

self::$_instance = new JoDES();

}

return self::$_instance;

}

/**

* 加密

* @param string $str 要处理的字符串

* @param string $key 加密Key,为8个字节长度

* @return string

*/

public function encode($str, $key) {

$size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);

$str = $this->pkcs5Pad($str, $size);

$aaa = mcrypt_cbc(MCRYPT_DES, $key, $str, MCRYPT_ENCRYPT, $key);

$ret = base64_encode($aaa);

return $ret;

}

/**

* 解密

* @param string $str 要处理的字符串

* @param string $key 解密Key,为8个字节长度

* @return string

*/

public function decode($str, $key) {

$strBin = base64_decode($str);

$str = mcrypt_cbc(MCRYPT_DES, $key, $strBin, MCRYPT_DECRYPT, $key);

$str = $this->pkcs5Unpad($str);

return $str;

}

function hex2bin($hexData) {

$binData = "";

for ($i = 0; $i 

$binData .= chr(hexdec(substr($hexData, $i, 2)));

}

return $binData;

}

function pkcs5Pad($text, $blocksize) {

$pad = $blocksize - (strlen($text) % $blocksize);

return $text . str_repeat(chr($pad), $pad);

}

function pkcs5Unpad($text) {

$pad = ord($text {strlen($text) - 1});

if ($pad > strlen($text))

return false;

if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)

return false;

return substr($text, 0, - 1 * $pad);

}

}

C#

public class MyDes

{

/// 

/// DES加密方法

/// 

/// 明文

/// 密钥

/// 向量

/// 密文

public static string Encode(string source, string _DESKey)

{

StringBuilder sb = new StringBuilder();

using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())

{

byte[] key = ASCIIEncoding.ASCII.GetBytes(_DESKey);

byte[] iv = ASCIIEncoding.ASCII.GetBytes(_DESKey);

byte[] dataByteArray = Encoding.UTF8.GetBytes(source);

des.Mode = System.Security.Cryptography.CipherMode.CBC;

des.Key = key;

des.IV = iv;

string encrypt = "";

using (MemoryStream ms = new MemoryStream())

using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))

{

cs.Write(dataByteArray, 0, dataByteArray.Length);

cs.FlushFinalBlock();

encrypt = Convert.ToBase64String(ms.ToArray());

}

return encrypt;

}

}

/// 

/// 进行DES解密。

/// 

/// 要解密的base64串

/// 密钥,且必须为8位。

/// 已解密的字符串。

public static string Decode(string source, string sKey)

{

byte[] inputByteArray = System.Convert.FromBase64String(source);//Encoding.UTF8.GetBytes(source);

using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())

{

des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);

des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);

System.IO.MemoryStream ms = new System.IO.MemoryStream();

using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))

{

cs.Write(inputByteArray, 0, inputByteArray.Length);

cs.FlushFinalBlock();

cs.Close();

}

string str = Encoding.UTF8.GetString(ms.ToArray());

ms.Close();

return str;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值