[急急急]关于 .net java的sha1算法 和3des加密问题

liu_192 2015-01-30 10:09:36
.net需要和java一样
下面是java代码

参数加密方式
Digest = Base64(SHA1 (SysID + “$”+ TimeStamp))
URLEncoding(SPID + “$”+ Base64(Encrypt(ReturnURL + “$”+ TimeStamp + “$”+ Digest)))

package com.encrypt;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.*;

public class Cryptogram {

private static byte[] defaultIV = {1,2,3,4,5,6,7,8};

private static byte chr2hex(String chr) {
if (chr.equals("0")) {
return 0x00;
} else if (chr.equals("1")) {
return 0x01;
} else if (chr.equals("2")) {
return 0x02;
} else if (chr.equals("3")) {
return 0x03;
} else if (chr.equals("4")) {
return 0x04;
} else if (chr.equals("5")) {
return 0x05;
} else if (chr.equals("6")) {
return 0x06;
} else if (chr.equals("7")) {
return 0x07;
} else if (chr.equals("8")) {
return 0x08;
} else if (chr.equals("9")) {
return 0x09;
} else if (chr.equals("A")) {
return 0x0a;
} else if (chr.equals("B")) {
return 0x0b;
} else if (chr.equals("C")) {
return 0x0c;
} else if (chr.equals("D")) {
return 0x0d;
} else if (chr.equals("E")) {
return 0x0e;
} else if (chr.equals("F")) {
return 0x0f;
}
return 0x00;
}

public static byte[] HexStringToByteArray(String s) {
byte[] buf = new byte[s.length() / 2];
for (int i = 0; i < buf.length; i++) {
buf[i] = (byte) (chr2hex(s.substring(i * 2, i * 2 + 1)) * 0x10 + chr2hex(s
.substring(i * 2 + 1, i * 2 + 2)));
}
return buf;
}

/**
* Encrypt the data by the key.
* @param OriSource
* @return strResult
* @throws Exception
*/
public static String encryptByKey(String OriSource, String key) throws Exception {

String strResult = "";
try {

Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede");

IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);

byte[] testSrc = OriSource.getBytes();
byte[] encoded = c3des.doFinal(testSrc);

strResult = Base64Encrypt.getBASE64_byte(encoded);

} catch (Exception e) {
strResult="";
System.out.println("Encrypt failure!!!");
}

return strResult;
}

/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String decryptByKey(String encryptedData, String key) throws Exception {

String strResult = "";
try {

Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede");

IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.DECRYPT_MODE, myKey, ivspec);

byte[] s= Base64Encrypt.getByteArrFromBase64(encryptedData);
byte[] encoded = c3des.doFinal(s);
strResult = new String(encoded);

} catch (Exception e) {
strResult="";
System.out.println("Decrypt failure!!!");
}

return strResult;
}

/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String getBase64HashString(String str) throws Exception{

byte[] testSrc = str.getBytes();
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(testSrc);
byte[] digesta = alga.digest();
return Base64Encrypt.getBASE64_byte(digesta);
}


/**
* Decrypt the encrypted data with the key.
* @param strData
* @return strResult
* @throws Exception
*/
public static String getAuthenicator(String sourceStr,String key) throws Exception{

String strResult = "";
try {

Cipher c3des = Cipher.getInstance("DESede/CBC/PKCS5Padding");
SecretKeySpec myKey = new SecretKeySpec(HexStringToByteArray(key),
"DESede");

IvParameterSpec ivspec = new IvParameterSpec(defaultIV);
c3des.init(Cipher.ENCRYPT_MODE, myKey, ivspec);


byte[] testSrc = sourceStr.getBytes();
MessageDigest alga = MessageDigest.getInstance("SHA-1");
alga.update(testSrc);
byte[] digesta = alga.digest();

byte[] encoded = c3des.doFinal(digesta);
strResult = Base64Encrypt.getBASE64_byte(encoded);

} catch (Exception e) {
strResult="";
System.out.println("Decrypt failure!!!" + e.getMessage());
}

return strResult;
}


public static void main(String args[]) throws Exception {
String SysID = "0005";
String TimeStamp = 时间戳,从1970年1月1日0时开始的毫秒数;
String ReturnURL = "http://vnet.cn/passportinterface/test2.aspx";
String Key = "86A659D3035B51B1B66DF3139F1AEC33F6651334F1E65170";

try{

//Get Digest.
String Digest = getBase64HashString(SysID + TimeStamp + ReturnURL);
System.out.println("The Base64HashString data :" + Digest);

//Get 3DES data.
String EncryptStr = encryptByKey(ReturnURL + “$”+ TimeStamp + “$”+Digest,Key);

System.out.println("The Encrypted data :" + EncryptStr);

String DecryptStr = decryptByKey(EncryptStr,Key);

System.out.println("The Decrypted data :" + DecryptStr);


String A = "guo";
A = getAuthenicator(A,Key);
System.out.print("The Encrypted data :" + A);

}
catch(Exception Ex){
Ex.printStackTrace();
}
}
}





package com.encrypt;


public class Base64Encrypt {
public static String getBASE64(String s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s.getBytes());
}

public static String getBASE64_byte(byte[] s) {
if (s == null)
return null;
return (new sun.misc.BASE64Encoder()).encode(s);
}

public static byte[] getByteArrFromBase64(String s) throws Exception{
if (s == null)
return null;
return (new sun.misc.BASE64Decoder()).decodeBuffer(s);
}
}
...全文
197 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
请贴出解决代码,造福后来人
liu_192 2015-09-24
  • 打赏
  • 举报
回复
早已经解决了

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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