Browsing all articles from
九月, 2009
C#加密.Java解密(DES 返回非base64 可以url传递)
工作需要 需要C#给一个字符串加密 然后通过url传递给Java Java进行解密.
网上这种例子好多 但是C#返回的都是base64..但是base64编码在URL中传递又不太合适.
所以我自己把算法修改了一下 已经测试可以使用..:)
为了和C#统一 所以我在Java中的向量直接用的是key 你也可以换一下 两者统一即可.
最后又打包下载.
C#代码
- using System;
- using System.Security;
- using System.Security.Cryptography;
- using System.IO;
- using System.Text;
- using System.Threading;
- namespace DES
- {
- /// <summary>
- /// DES 加密
- /// </summary>
- public class DES
- {
- public DES()
- {
- }
- //密钥
- private const string sKey = "ABCDEFGH";
- public static string Encrypt(string pToEncrypt)
- {
- using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
- {
- byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
- 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.CreateEncryptor(), CryptoStreamMode.Write))
- {
- cs.Write(inputByteArray, 0, inputByteArray.Length);
- cs.FlushFinalBlock();
- cs.Close();
- }
- string str = HexStringFromByteArray(ms.ToArray());
- ms.Close();
- return str;
- }
- }
- public static string HexStringFromByteArray(byte[] bytes)
- {
- string s = "";
- foreach (byte b in bytes)
- {
- s += string.Format("{0:X2}", b);
- }
- return s;
- }
- public static void Main(string[] args)
- {
- //
- // TODO: Add code to start application here
- //
- Console.WriteLine (DES.Encrypt("500200088"));
- }
- }
- }
Java代码
- import javax.crypto.Cipher;
- import javax.crypto.SecretKey;
- import javax.crypto.SecretKeyFactory;
- import javax.crypto.spec.DESKeySpec;
- import javax.crypto.spec.IvParameterSpec;
- public class DES {
- /** 加密、解密key. */
- private static final String PASSWORD_CRYPT_KEY = "ABCDEFGH";
- /** 加密算法 */
- private final static String ALGORITHM = "DES/CBC/PKCS5Padding";
- /**
- * 对数据进行DES加密.
- * @param data 待进行DES加密的数据
- * @return 返回经过DES加密后的数据
- * @throws Exception
- */
- public final static String decrypt(String data) throws Exception {
- return new String(decrypt(hex2byte(data.getBytes()),
- PASSWORD_CRYPT_KEY.getBytes()));
- }
- /**
- * 对用DES加密过的数据进行解密.
- * @param data DES加密数据
- * @return 返回解密后的数据
- * @throws Exception
- */
- public final static String encrypt(String data) throws Exception {
- return byte2hex(encrypt(data.getBytes(), PASSWORD_CRYPT_KEY
- .getBytes()));
- }
- /**
- * 用指定的key对数据进行DES加密.
- * @param data 待加密的数据
- * @param key DES加密的key
- * @return 返回DES加密后的数据
- * @throws Exception
- */
- private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
- // 从原始密匙数据创建DESKeySpec对象
- DESKeySpec dks = new DESKeySpec(key);
- // 创建一个密匙工厂,然后用它把DESKeySpec转换成
- // 一个SecretKey对象
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- SecretKey securekey = keyFactory.generateSecret(dks);
- //向量
- IvParameterSpec iv = new IvParameterSpec(key);
- // Cipher对象实际完成加密操作
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- // 用密匙初始化Cipher对象
- cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
- // 现在,获取数据并加密
- // 正式执行加密操作
- return cipher.doFinal(data);
- }
- /** *//**
- * 用指定的key对数据进行DES解密.
- * @param data 待解密的数据
- * @param key DES解密的key
- * @return 返回DES解密后的数据
- * @throws Exception
- */
- private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
- // 从原始密匙数据创建一个DESKeySpec对象
- DESKeySpec dks = new DESKeySpec(key);
- // 创建一个密匙工厂,然后用它把DESKeySpec对象转换成
- // 一个SecretKey对象
- SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- SecretKey securekey = keyFactory.generateSecret(dks);
- //向量
- IvParameterSpec iv = new IvParameterSpec(key);
- // Cipher对象实际完成解密操作
- Cipher cipher = Cipher.getInstance(ALGORITHM);
- // 用密匙初始化Cipher对象
- cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
- // 现在,获取数据并解密
- // 正式执行解密操作
- return cipher.doFinal(data);
- }
- public static byte[] hex2byte(byte[] b) {
- if ((b.length % 2) != 0)
- throw new IllegalArgumentException("长度不是偶数");
- byte[] b2 = new byte[b.length / 2];
- for (int n = 0; n < b.length; n += 2) {
- String item = new String(b, n, 2);
- b2[n / 2] = (byte) Integer.parseInt(item, 16);
- }
- return b2;
- }
public static String byte2hex(byte[] b) { - String hs = "";
- String stmp = "";
- for (int n = 0; n < b.length; n++) {
- stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
- if (stmp.length() == 1)
- hs = hs + "0" + stmp;
- else
- hs = hs + stmp;
- }
- return hs.toUpperCase();
- }
- }
PHP分步骤进程类
我们在平时的工作中 很多会遇到这种情况 比如
一次性会往数据库中导入N多数据 但是我们又没有数据库的直接操作权限 只能通过PHP来导入
尤其是程序部署 或者 程序升级的时候..
这时候我们就通过一个巧妙的办法..分步骤的导入就可以了..我特意写了一个类..给大家凑合用着试试
使用方法(还有很多参数 具体看源码吧 没有任何技术难度)
- <?php
- /**
- * 回调函数 执行你的操作
- *
- * @param <type> $now 当前的执行的步数
- */
- function test($now) {
- //do what you want!!
- }
- include('SkiyoProcess.class.php');
- $sp = new SkiyoProcess();
- $sp->process('test'); //你的回调函数
document.ready
实现jQuery的document.ready功能
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>document.ready</title>
- <script type="text/javascript">
- (function () {
- var ie = !!(window.attachEvent && !window.opera);
- var wk = /webkit\/(\d+)/i.test(navigator.userAgent) && (RegExp.$1 < 525);
- var fn = [];
- var run = function () { for (var i = 0; i < fn.length; i++) fn[i](); };
- var d = document;
- d.ready = function (f) {
- if (!ie && !wk && d.addEventListener)
- return d.addEventListener('DOMContentLoaded', f, false);
- if (fn.push(f) > 1) return;
- if (ie)
- (function () {
- try { d.documentElement.doScroll('left'); run(); }
- catch (err) { setTimeout(arguments.callee, 0); }
- })();
- else if (wk)
- var t = setInterval(function () {
- if (/^(loaded|complete)$/.test(d.readyState))
- clearInterval(t), run();
- }, 0);
- };
- })();
- document.ready(function(){
- document.getElementById('test').innerHTML = 'document.ready test!'; //找到
- });
- alert(document.getElementById('test')); //null 没找到
- </script>
- </head>
- <body>
- <div id="test"></div>
- </body>
- </html>
RuntimeMaker
无聊写出来玩玩的 .你的目录里的文件必须是纯的PHP类 不可以是HTML代码混写
以后再写一个Punny RuntimeMaker 你的项目完成了 一运行这个 把所有的代码就加载到一个文件中
经过我的测试 确实有助于提高速度..在PHP中 include_once的代价是蛮大的..
不管是不是鸡肋 大家拿去玩玩把 也没有什么技术含量..:)
分类目录
- ActionScript (2)
- CSS (25)
- Java (3)
- JavaScript (41)
- PHP (105)
- 心情杂谈 (34)
- 收集整理 (77)
- 本站原创 (55)
最近文章
- 将google ssl设置为IE8的默认搜索引擎..
- 我们来做一个会呼吸的菜单吧!!
- 在编译php-fpm0.6的时候需要注意的一些问题
- 使用PHP将大文件导入到数据库中..
- 关于用PHP调用WebService中参数为complexType的问题
- 神奇的两次按位非运算符
- 百路推免费短网址服务..首创”收藏夹获取短网址”..
- 哥学社正式上线..
- jQuery中getJSON跨域原理详解
- Web辅助工具条(原名:河蟹工具条CrabBar)0.1发布
- 腾讯微博PC端发图教程
- goo.gl URL Shortener for WordPress
- PHP上传进度条深度解析
- Google短网址(goo.gl)服务类
- TinyURL.class.php
最近评论
- 匿名 在 一个PHP+AJAX留言板的完整例子.非常简单! 上的评论
- pfeng 在 将google ssl设置为IE8的默认搜索引擎.. 上的评论
- pfeng 在 将google ssl设置为IE8的默认搜索引擎.. 上的评论
- 北戴河旅游住宿 在 PHPer的历练 上的评论
- konakona 在 将google ssl设置为IE8的默认搜索引擎.. 上的评论
- 宁静致远 在 PHPer的历练 上的评论
- Corsair_Boss 在 强人作品 – jQuery1.2.6源码分析 上的评论
- fanglor 在 PHPer的历练 上的评论
- fanglor 在 百路推免费短网址服务..首创”收藏夹获取短网址”.. 上的评论
- 匿名 在 Web辅助工具条(原名:河蟹工具条CrabBar)0.1发布 上的评论
文章索引模板
- 2010年七月 (3)
- 2010年六月 (4)
- 2010年五月 (2)
- 2010年四月 (9)
- 2010年三月 (12)
- 2010年二月 (1)
- 2010年一月 (3)
- 2009年十二月 (2)
- 2009年十一月 (3)
- 2009年十月 (3)
- 2009年九月 (5)
- 2009年八月 (4)
- 2009年七月 (6)
- 2009年六月 (8)
- 2009年五月 (8)
- 2009年四月 (16)
- 2009年三月 (19)
- 2009年二月 (22)
- 2009年一月 (20)
- 2008年十二月 (38)
- 2008年十一月 (22)
- 2008年十月 (7)
- 2008年九月 (3)
- 2008年八月 (24)
标签
.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
上传
加密
变量
图标
本站原创
模板
模板引擎
源码
登录
短网址
石家庄
算法
类
编译
面向对象
魔术方法

Jessica

