加到300分,求在JAVA下的“PBEWithMD5AndDES”加密算法,到C#下如何实现?(二)

busymj 2012-02-14 11:24:43

加到300分

输入字串:这是一个测试
输出字串:09IdjpQQAPgniEaO/skp9gy8+LkD42fZ

输入字串:123456
输出字串:OEnU8DEcgsE=

以下是JAVA的源代码,想求一下在C#使用哪种函数能实现同样的加密算法。

一、http://topic.csdn.net/u/20120212/20/077d891b-d861-417d-8df0-aa973eb46bbf.html

二、本帖

三、




//07
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

//09
import java.security.MessageDigest;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

//BASE64
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class pubwin2009
{


// key1 姓名身份证加解密 07/09
public static String Rcode(String message,int type)throws Exception
{
//String Key="123!@#qweQWE/.,?><abc"; //07
String Key="q1!2@3#we>WacE/.Q,?<b"; //09
byte _fldint[] = { -87, -101, -56, 50, 86, 53, -29, 3};
byte bytesrc[];
if (type==1)
bytesrc =message.getBytes("UTF8");
else
bytesrc =(new BASE64Decoder()).decodeBuffer(message);
PBEParameterSpec pbeparameterspec = new PBEParameterSpec(_fldint,19);
PBEKeySpec pbekeyspec = new PBEKeySpec(Key.toCharArray());
SecretKeyFactory secretkeyfactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
javax.crypto.SecretKey secretkey = secretkeyfactory.generateSecret(pbekeyspec);
Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(type, secretkey, pbeparameterspec);
byte[] retByte = cipher.doFinal(bytesrc);
if (type==1)
return new BASE64Encoder().encode(retByte);
else
return new String(retByte,"UTF8");

}
//09会员密码 db.properties 加解密
public static String decrypt(String message,int type) throws Exception {
String keye = "hi%$so78"; //MD5
String keyb = "12up56^&"; //IvParameterSpec
byte bytesrc[];
if (type==1)
bytesrc =message.getBytes();
else
bytesrc =(new BASE64Decoder()).decodeBuffer(message);
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(keye.getBytes());
DESKeySpec desKeySpec = new DESKeySpec(md5.digest());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(keyb.getBytes("UTF-8"));
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(type, secretKey, iv);
byte[] retByte = cipher.doFinal(bytesrc);
if (type==1)
return new BASE64Encoder().encode(retByte);
else
return new String(retByte);
}

public static void main(String args[]) throws Exception
{
System.out.println( "\n姓名身份证加密:这是一个测试 -> " +Rcode("这是一个测试",1));
System.out.println( "姓名身份证解密:09IdjpQQAPgniEaO/skp9gy8+LkD42fZ -> " +Rcode("09IdjpQQAPgniEaO/skp9gy8+LkD42fZ",2)+"\n");

System.out.println( "09会员密码加解:123456 -> " +decrypt("123456",1));
System.out.println( "09会员密码解密:OEnU8DEcgsE= -> " +decrypt("OEnU8DEcgsE=",2));

System.exit(0);
}

}



...全文
445 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
busymj 2012-02-16
  • 打赏
  • 举报
回复
测试通过了,现在问题解决了一半。

请2楼与9楼的朋友到另两个帖子里留言接分。

另外,密码加密与解密和问题是否也能帮解决一下,我另加100分。谢谢了。

JAVA源码中的“decrypt”函数。

输入字串:123456
输出字串:OEnU8DEcgsE=


[Quote=引用 9 楼 jshi123 的回复:]

用2楼给的链接就可以
C# code

static void Main(string[] args)
{
Console.WriteLine("\n姓名身份证加密:这是一个测试 -> " + Rcode("这是一个测试", 1));
Console.WriteLine("姓名身份证解密:09IdjpQQAPgniEaO/skp9gy8+LkD……
[/Quote]
jshi123 2012-02-16
  • 打赏
  • 举报
回复
用2楼给的链接就可以

static void Main(string[] args)
{
Console.WriteLine("\n姓名身份证加密:这是一个测试 -> " + Rcode("这是一个测试", 1));
Console.WriteLine("姓名身份证解密:09IdjpQQAPgniEaO/skp9gy8+LkD42fZ -> " + Rcode("09IdjpQQAPgniEaO/skp9gy8+LkD42fZ", 2) + "\n");
}

public static String Rcode(String message, int type)
{
String key = "q1!2@3#we>WacE/.Q,?<b"; //密码
sbyte[] salt = { -87, -101, -56, 50, 86, 53, -29, 3 }; // 随机盐
int count = 19; // 迭代次数

PKCSKeyGenerator cipher = new PKCSKeyGenerator(key, Array.ConvertAll(salt, a=>(byte)a), count, 1);

if (type == 1)
{
byte[] src = Encoding.UTF8.GetBytes(message);
byte[] result = cipher.Encryptor.TransformFinalBlock(src, 0, src.Length);
return Convert.ToBase64String(result);
}
else
{
byte[] src = Convert.FromBase64String(message);
byte[] result = cipher.Decryptor.TransformFinalBlock(src, 0, src.Length);
return Encoding.UTF8.GetString(result);
}
}

public class PKCSKeyGenerator
{
byte[] key = new byte[8], iv = new byte[8];
DESCryptoServiceProvider des = new DESCryptoServiceProvider();

public byte[] Key { get { return key; } }
public byte[] IV { get { return iv; } }
public ICryptoTransform Encryptor { get { return des.CreateEncryptor(key, iv); } }
public ICryptoTransform Decryptor { get { return des.CreateDecryptor(key, iv); } } // 多加一个Decryptor用于解密

public PKCSKeyGenerator() { }
public PKCSKeyGenerator(String keystring, byte[] salt, int md5iterations, int segments)
{
Generate(keystring, salt, md5iterations, segments);
}

public ICryptoTransform Generate(String keystring, byte[] salt, int md5iterations, int segments)
{
int HASHLENGTH = 16; //MD5 bytes
byte[] keymaterial = new byte[HASHLENGTH * segments]; //to store concatenated Mi hashed results

// --- get secret password bytes ----
byte[] psbytes;
psbytes = Encoding.UTF8.GetBytes(keystring);

// --- concatenate salt and pswd bytes into fixed data array ---
byte[] data00 = new byte[psbytes.Length + salt.Length];
Array.Copy(psbytes, data00, psbytes.Length); //copy the pswd bytes
Array.Copy(salt, 0, data00, psbytes.Length, salt.Length);//concatenate the salt bytes

// ---- do multi-hashing and concatenate results D1, D2 ...
// into keymaterial bytes ----
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = null;
byte[] hashtarget = new byte[HASHLENGTH + data00.Length]; //fixed length initial hashtarget

for (int j = 0; j < segments; j++)
{
// ---- Now hash consecutively for md5iterations times ------
if (j == 0) result = data00; //initialize
else
{
Array.Copy(result, hashtarget, result.Length);
Array.Copy(data00, 0, hashtarget, result.Length, data00.Length);
result = hashtarget;
}

for (int i = 0; i < md5iterations; i++)
result = md5.ComputeHash(result);

Array.Copy(result, 0, keymaterial, j * HASHLENGTH, result.Length); //concatenate to keymaterial
}

Array.Copy(keymaterial, 0, key, 0, 8);
Array.Copy(keymaterial, 8, iv, 0, 8);
return Encryptor;
}
}

nonocast 2012-02-15
  • 打赏
  • 举报
回复
有空帮你写一把,先顶一下呗
busymj 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 nonocast 的回复:]

有空帮你写一把,先顶一下呗
[/Quote]

非常感谢,您只要帮我确定一下全用的函数以及相应参数的位置就可以了,我试了很久都没成功。

谢谢了。。

busymj 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hongcha99 的回复:]

是我来错了吗?这里是.ner非技术区啊
[/Quote]

额。。。是啊,才发现。。。汗。

就这里继续吧,希望有人能帮解决下。
Ny-6000 2012-02-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hongcha99 的回复:]
是我来错了吗?这里是.ner非技术区啊
[/Quote]
hongcha99 2012-02-15
  • 打赏
  • 举报
回复
是我来错了吗?这里是.ner非技术区啊
孟子E章 2012-02-15
  • 打赏
  • 举报
回复
参见Emulating PBEWithMD5AndDES Encryption under .NET
http://www.codeproject.com/Articles/16450/Emulating-PBEWithMD5AndDES-Encryption-under-NET
段传涛 2012-02-15
  • 打赏
  • 举报
回复
为什么 在非技术区。
快乐的小二兔 2012-02-15
  • 打赏
  • 举报
回复
传说非技术区混水分

7,765

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧