Browsing all articles from 九月, 2009
27
2

C#加密.Java解密(DES 返回非base64 可以url传递)

Author Jessica     Category Java     Tags , ,

工作需要 需要C#给一个字符串加密 然后通过url传递给Java Java进行解密.

网上这种例子好多 但是C#返回的都是base64..但是base64编码在URL中传递又不太合适.

所以我自己把算法修改了一下 已经测试可以使用..:)

为了和C#统一 所以我在Java中的向量直接用的是key 你也可以换一下 两者统一即可.

最后又打包下载.

C#代码

  1. using System;  
  2. using System.Security;  
  3. using System.Security.Cryptography;  
  4. using System.IO;  
  5. using System.Text;  
  6. using System.Threading;  
  7. namespace DES  
  8. {  
  9.     /// <summary>  
  10.     /// DES 加密  
  11.     /// </summary>  
  12.     public class DES  
  13.     {  
  14.         public DES()  
  15.         {  
  16.         }  
  17.         //密钥  
  18.         private const string sKey = "ABCDEFGH";  
  19.  
  20.         public static string Encrypt(string pToEncrypt)  
  21.         {  
  22.             using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())  
  23.             {  
  24.                 byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);  
  25.                 des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);  
  26.                 des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);  
  27.                 System.IO.MemoryStream ms = new System.IO.MemoryStream();  
  28.                 using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))  
  29.                 {  
  30.                     cs.Write(inputByteArray, 0, inputByteArray.Length);  
  31.                     cs.FlushFinalBlock();  
  32.                     cs.Close();  
  33.                 }  
  34.                 string str = HexStringFromByteArray(ms.ToArray());  
  35.                 ms.Close();  
  36.                 return str;  
  37.             }  
  38.         }  
  39.         public static string HexStringFromByteArray(byte[] bytes)  
  40.         {  
  41.             string s = "";  
  42.  
  43.             foreach (byte b in bytes)  
  44.             {  
  45.                 s += string.Format("{0:X2}", b);  
  46.             }  
  47.  
  48.             return s;  
  49.         }  
  50.  
  51.         public static void Main(string[] args)  
  52.         {  
  53.             //  
  54.             // TODO: Add code to start application here  
  55.             //  
  56.  
  57.             Console.WriteLine (DES.Encrypt("500200088"));  
  58.         }  
  59.  
  60.     }  

Java代码

  1. import javax.crypto.Cipher;  
  2. import javax.crypto.SecretKey;  
  3. import javax.crypto.SecretKeyFactory;  
  4. import javax.crypto.spec.DESKeySpec;  
  5. import javax.crypto.spec.IvParameterSpec;  
  6.  
  7. public class DES {  
  8.     /** 加密、解密key. */    
  9.     private static final String PASSWORD_CRYPT_KEY = "ABCDEFGH";     
  10.     
  11.     /** 加密算法 */    
  12.     private final static String ALGORITHM = "DES/CBC/PKCS5Padding";     
  13.          
  14.     /**     
  15.      * 对数据进行DES加密.    
  16.      * @param data 待进行DES加密的数据    
  17.      * @return 返回经过DES加密后的数据    
  18.      * @throws Exception    
  19.      */    
  20.     public final static String decrypt(String data) throws Exception {     
  21.         return new String(decrypt(hex2byte(data.getBytes()),     
  22.                 PASSWORD_CRYPT_KEY.getBytes()));     
  23.     }     
  24.     
  25.     /**     
  26.      * 对用DES加密过的数据进行解密.    
  27.      * @param data DES加密数据    
  28.      * @return 返回解密后的数据    
  29.      * @throws Exception    
  30.      */    
  31.     public final static String encrypt(String data) throws Exception  {     
  32.         return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY     
  33.                 .getBytes()));     
  34.     }     
  35.          
  36.     /**     
  37.      * 用指定的key对数据进行DES加密.    
  38.      * @param data 待加密的数据    
  39.      * @param key DES加密的key    
  40.      * @return 返回DES加密后的数据    
  41.      * @throws Exception    
  42.      */    
  43.     private static byte[] encrypt(byte[] data, byte[] key) throws Exception {      
  44.         // 从原始密匙数据创建DESKeySpec对象     
  45.         DESKeySpec dks = new DESKeySpec(key);     
  46.         // 创建一个密匙工厂,然后用它把DESKeySpec转换成     
  47.         // 一个SecretKey对象     
  48.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");     
  49.         SecretKey securekey = keyFactory.generateSecret(dks);  
  50.         //向量  
  51.         IvParameterSpec iv = new IvParameterSpec(key);  
  52.         // Cipher对象实际完成加密操作     
  53.         Cipher cipher = Cipher.getInstance(ALGORITHM);     
  54.         // 用密匙初始化Cipher对象     
  55.         cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);     
  56.         // 现在,获取数据并加密     
  57.         // 正式执行加密操作     
  58.         return cipher.doFinal(data);     
  59.     }  
  60.     
  61.     /** *//**    
  62.      * 用指定的key对数据进行DES解密.    
  63.      * @param data 待解密的数据    
  64.      * @param key DES解密的key    
  65.     * @return 返回DES解密后的数据    
  66.      * @throws Exception    
  67.      */    
  68.     private static byte[] decrypt(byte[] data, byte[] key) throws Exception {     
  69.         // 从原始密匙数据创建一个DESKeySpec对象     
  70.         DESKeySpec dks = new DESKeySpec(key);     
  71.         // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成     
  72.         // 一个SecretKey对象     
  73.         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");     
  74.         SecretKey securekey = keyFactory.generateSecret(dks);  
  75.         //向量  
  76.         IvParameterSpec iv = new IvParameterSpec(key);  
  77.         // Cipher对象实际完成解密操作     
  78.         Cipher cipher = Cipher.getInstance(ALGORITHM);     
  79.         // 用密匙初始化Cipher对象     
  80.         cipher.init(Cipher.DECRYPT_MODE, securekey, iv);     
  81.         // 现在,获取数据并解密     
  82.         // 正式执行解密操作     
  83.         return cipher.doFinal(data);     
  84.     }  
  85.       
  86.     public static byte[] hex2byte(byte[] b) {     
  87.         if ((b.length % 2) != 0)     
  88.             throw new IllegalArgumentException("长度不是偶数");     
  89.         byte[] b2 = new byte[b.length / 2];     
  90.         for (int n = 0; n < b.length; n += 2) {     
  91.             String item = new String(b, n, 2);     
  92.             b2[n / 2] = (byte) Integer.parseInt(item, 16);     
  93.         }     
  94.         return b2;     
  95.     }     
  96.     
  97.     public static String byte2hex(byte[] b) {     
  98.         String hs = "";     
  99.         String stmp = "";     
  100.     
  101.         for (int n = 0; n < b.length; n++) {     
  102.             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));     
  103.             if (stmp.length() == 1)     
  104.                 hs = hs + "0" + stmp;     
  105.             else    
  106.                 hs = hs + stmp;     
  107.         }     
  108.         return hs.toUpperCase();     
  109.     }  
  110. }  

 

read more

20
4

PHP分步骤进程类

Author Jessica     Category PHP     Tags ,

我们在平时的工作中 很多会遇到这种情况 比如

一次性会往数据库中导入N多数据 但是我们又没有数据库的直接操作权限 只能通过PHP来导入

尤其是程序部署 或者 程序升级的时候..

这时候我们就通过一个巧妙的办法..分步骤的导入就可以了..我特意写了一个类..给大家凑合用着试试

使用方法(还有很多参数 具体看源码吧 没有任何技术难度)

  1. <?php  
  2.  
  3. /**  
  4.  * 回调函数 执行你的操作  
  5.  *   
  6.  * @param <type> $now 当前的执行的步数  
  7.  */ 
  8. function test($now) {  
  9.     //do what you want!!  
  10. }  
  11.  
  12. include('SkiyoProcess.class.php');  
  13.  
  14. $sp = new SkiyoProcess();  
  15. $sp->process('test');  //你的回调函数 

 

在线演示http://demo.skiyo.cn/skiyoprocess

read more

10
2

document.ready

实现jQuery的document.ready功能

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>document.ready</title> 
  6. <script type="text/javascript"> 
  7. (function () {  
  8.     var ie  = !!(window.attachEvent && !window.opera);  
  9.     var wk  = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);  
  10.     var fn  = [];  
  11.     var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };  
  12.     var d   = document;  
  13.     d.ready = function (f) {  
  14.         if (!ie && !wk && d.addEventListener)  
  15.             return d.addEventListener('DOMContentLoaded', f, false);  
  16.         if (fn.push(f) > 1) return;  
  17.         if (ie)  
  18.             (function () {  
  19.                 try { d.documentElement.doScroll('left'); run(); }  
  20.                 catch (err) { setTimeout(arguments.callee, 0); }  
  21.             })();  
  22.         else if (wk)  
  23.             var t = setInterval(function () {  
  24.                 if (/^(loaded|complete)$/.test(d.readyState))  
  25.                     clearInterval(t), run();  
  26.             }, 0);  
  27.     };  
  28. })();  
  29. document.ready(function(){  
  30.     document.getElementById('test').innerHTML = 'document.ready test!';  //找到  
  31. });  
  32. alert(document.getElementById('test')); //null 没找到  
  33. </script> 
  34. </head> 
  35.  
  36. <body> 
  37. <div id="test"></div> 
  38. </body> 
  39. </html> 

演示地址(右键源码):http://www.skiyo.cn/demo/js/document.ready.html

7
5

RuntimeMaker

Author Jessica     Category PHP     Tags ,

无聊写出来玩玩的 .你的目录里的文件必须是纯的PHP类 不可以是HTML代码混写

以后再写一个Punny RuntimeMaker 你的项目完成了 一运行这个 把所有的代码就加载到一个文件中

经过我的测试 确实有助于提高速度..在PHP中 include_once的代价是蛮大的..

不管是不是鸡肋  大家拿去玩玩把 也没有什么技术含量..:)

read more

1
3

我扣的一套邮件系统模板:)[修正版]

Author Jessica     Category CSS     Tags

还有个登录页面噢..

修正了IE6下无法打开的问题(该死的编码)

把上面那个邮件上面有个易的图标替换掉了..:)

read more

分类目录

最近文章

最近评论

文章索引模板

标签

.net AJAX button Comet CSS Discuz! DIV+CSS Flash Form Google HTML编辑器 IE8 Java JavaScript jQuery JSP md5 MySQLReback Oracle PHP php-fpm PNG Punny SkiyoTabs tab TagCloud Vista Web2.0 Windows7 上传 加密 变量 图标 本站原创 模板 模板引擎 源码 登录 短网址 石家庄 算法 编译 面向对象 魔术方法

链接表