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#中相同结果的代码事例

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值