Java和C#使用DES加密算法的代码不一样,比如,C#的是key和IV,而Java的是salt和key,真的搞不清楚是怎么回事。DES算法不是语言无关性的吗?怎么样统一两者,使得C#加密的字符串Java能解密,反之亦然。请高手们不吝赐教,最好能给能给一些例子源代码。谢谢!!!
解决方案 »
- ExecuteReader()查询不到第一条数据
- 如何直接读取dataset的某个字段数据?
- 微软有没有想过这个问题?
- 如何把html格式文本或者图像之类的存入数据库
- 有关DataGrid里边显示标题的问题
- 关于路书
- c#listview怎么实现记住密码登陆进去后,再次登录仍然选中,不选,则再次登陆时就不选中
- 现完成了一套插件式构架的程序,用VS2005打包,能不能做到安装时选择了那些插件,才把相应插件的DLL安装进客户机去?
- 请问能否在aspx页面中指定具体要引用的dll的路径?
- byte[]变量中的值怎么转化到string中,反之呢?
- dddddddddfffffffffff
- 请问如何在XmlDocument中存放byte[]数据
private static byte [ ] DESIV = new byte [ ] {70 , 0x35 , 50 , 0x42 , 0x31 , 0x38 , 0x36 , 70} //加密方法
/// <summary>
///
/// </summary>
/// <param name="pToEncrypt"></param>
/// <returns></returns>
public string Encrypt ( string pToEncrypt )
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ( ) ;
byte [ ] inputByteArray = Encoding.Default.GetBytes ( pToEncrypt ) ;
des.Key = DESKey ;
des.IV = DESIV ;
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 ) ;
}
ret.ToString ( ) ;
return ret.ToString ( ) ;
} ///解密方法
/// <summary>
///
/// </summary>
/// <param name="pToDecrypt"></param>
/// <returns></returns>
public string Decrypt ( string pToDecrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider ( ) ;
byte [ ] inputByteArray = new byte[pToDecrypt.Length / 2] ;
for ( int x = 0 ; x < pToDecrypt.Length / 2 ; x++ )
{
int i = ( Convert.ToInt32 ( pToDecrypt.Substring ( x * 2 , 2 ) , 16 ) ) ;
inputByteArray [ x ] = ( byte ) i ;
}
des.Key = DESKey ;
des.IV = DESIV ;
MemoryStream ms = new MemoryStream ( ) ;
CryptoStream cs = new CryptoStream ( ms , des.CreateDecryptor ( ) , CryptoStreamMode.Write ) ;
cs.Write ( inputByteArray , 0 , inputByteArray.Length ) ;
cs.FlushFinalBlock ( ) ;
return Encoding.Default.GetString ( ms.ToArray ( ) ) ;
}
我曾经想过使用C#的MD5算法来替代Java的MD5算法!失败,加密出来的字符串结构一样,但就是不相等!
累啊!放弃了!
public string Enstring(string thisString)
{
string encrypted = "";
byte[] code = ASCIIEncoding.Unicode.GetBytes(thisString);
encrypted = Convert.ToBase64String(des.CreateEncryptor().TransformFinalBlock(code, 0, code.Length));
return encrypted;
} public string Destring(string thisString)
{
string encrypted = "";
byte[] code = Convert.FromBase64String(thisString);
encrypted = ASCIIEncoding.Unicode.GetString(des.CreateDecryptor().TransformFinalBlock(code, 0, code.Length));
return encrypted;
}
累啊!放弃了!
=================
不可能,c#加密的md5和任何一种md5加密工具加密出来的都是一样的,我试过的。
给你一个比较完整的C# DES 加密解密代码
//pToEncrypt为需要加密字符串,sKey为密钥
public string Encrypt(string pToEncrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
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);
}
ret.ToString();
return ret.ToString();
}
//pToDecrypt为需要解密字符串,sKey为密钥
public string Decrypt(string pToDecrypt, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for(int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
//建立加密对象的密钥和偏移量,此值重要,不能修改
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(),CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Security.Cryptography;
using System.IO;namespace DesFile
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.TextBox textBox7;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox textBox8;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}