DES加密算法在java和C#中相同结果的代码事例

先看一下java代码:

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package test;

import java.security.SecureRandom;

import javax.crypto.Cipher;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESKeySpec;

public class Des {

private static final String PASSWORD_CRYPT_KEY = "abcdefgh";

private final static String DES = "DES";

public static void main(String[] args){

 

System.out.println("加密:"+encrypt("123456"));

 

}

 

public static byte[] encrypt(byte[] src, byte[] key) throws Exception {

SecureRandom sr = new SecureRandom();

DESKeySpec dks = new DESKeySpec(key);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

SecretKey securekey = keyFactory.generateSecret(dks);

Cipher cipher = Cipher.getInstance(DES);

cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

return cipher.doFinal(src);

}

 

public static byte[] decrypt(byte[] src, byte[] key) throws Exception {

SecureRandom sr = new SecureRandom();

DESKeySpec dks = new DESKeySpec(key);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);

SecretKey securekey = keyFactory.generateSecret(dks);

Cipher cipher = Cipher.getInstance(DES);

cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

return cipher.doFinal(src);

}

/**

** 密码解密

*/

public final static String decrypt(String data) {

try {

return new String(decrypt(hexStringToBytes(data),PASSWORD_CRYPT_KEY.getBytes()));

} catch (Exception e) {

}

return null;

}

/**

** 密码加密

*/

public final static String encrypt(String password) {

try {

return bytesToHexString(encrypt(password.getBytes(), PASSWORD_CRYPT_KEY.getBytes()));

} catch (Exception e) {

}

return null;

}

 

public static String bytesToHexString(byte[] src){

StringBuilder stringBuilder = new StringBuilder("");

if (src == null || src.length <= 0) {

return null;

}

for (int i = 0; i < src.length; i++) {

int v = src[i] & 0xFF;

String hv = Integer.toHexString(v);

if (hv.length() < 2) {

stringBuilder.append(0);

}

stringBuilder.append(hv);

}

return stringBuilder.toString().toUpperCase();

}

 

public static byte[] hexStringToBytes(String hexString) {

if (hexString == null || hexString.equals("")) {

return null;

}

hexString = hexString.toUpperCase();

int length = hexString.length() / 2;

char[] hexChars = hexString.toCharArray();

byte[] d = new byte[length];

for (int i = 0; i < length; i++) {

int pos = i * 2;

d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));

}

return d;

}

private static byte charToByte(char c) {

return (byte) "0123456789ABCDEF".indexOf(c);

}

 

}

上面java代码执行的结果如下:

run:

加密:B074DDFFD7B162E9

成功构建 (总时间: 1 秒)

下面在看一下c#的代码:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Security.Cryptography;

using System.IO;

namespace Temp

{

class DES

{

private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

public static string EncryptDES(string encryptString, string encryptKey)

{

try

{

byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));

byte[] rgbIV = Keys;

byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();

dCSP.Mode = CipherMode.ECB;

MemoryStream mStream = new MemoryStream();

CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);

cStream.Write(inputByteArray, 0, inputByteArray.Length);

cStream.FlushFinalBlock();

return BytesToHexString(mStream.ToArray());

}

catch

{

return encryptString;

}

}

public static string BytesToHexString(byte[] bytes)

{

StringBuilder returnStr = new StringBuilder();

if (bytes != null || bytes.Length == 0)

{

for (int i = 0; i < bytes.Length; i++)

{

returnStr.Append(bytes[i].ToString("X2"));

}

}

return returnStr.ToString();

}

 

}

}

上面代码中要想C#的加密结果与java中的代码结果一致最关键是dCSP.Mode = CipherMode.ECB;这一行代码。

最后运行的效果如下:

DES加密算法在java和C#中相同结果的代码事例_第1张图片

 

你可能感兴趣的:(java)