H3C-cams DES加密算法研究总结

更新时间:2023-09-18 14:17:01 阅读量: 幼儿教育 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

H3C加密算法研究总结

1、 使用的密码算法为DES+BASE64 (DES使用默认的加密密钥“liuan814”) 2、 从IMC-PLAT.jar中提取出的加密和解密代码片段 CommonUtils.class中的: @ExternalInterface

private static final byte[] j = \ private static Cipher k; private static Cipher l;

public static byte[] encryptData(byte[] paramArrayOfByte) {

if (k == null) { try {

DESKeySpec localDESKeySpec = new DESKeySpec(j);

SecretKeyFactory localSecretKeyFactory = SecretKeyFactory.getInstance(\ SecretKey localSecretKey = localSecretKeyFactory.generateSecret(localDESKeySpec); k = Cipher.getInstance(\ k.init(1, localSecretKey);

} catch (Exception localException1) { a.error(null, localException1); return null; }

}

byte[] arrayOfByte = null; try {

arrayOfByte = k.doFinal(paramArrayOfByte); } catch (Exception localException2) { a.error(null, localException2); return null; }

if (arrayOfByte != null) {

return Base64.encodeBase64(arrayOfByte); }

return null; }

@ExternalInterface

public static byte[] decryptData(byte[] paramArrayOfByte) {

if (l == null) { try {

DESKeySpec localDESKeySpec = new DESKeySpec(j);

SecretKeyFactory localSecretKeyFactory = SecretKeyFactory.getInstance(\ SecretKey localSecretKey = localSecretKeyFactory.generateSecret(localDESKeySpec); l = Cipher.getInstance(\ l.init(2, localSecretKey);

} catch (Throwable localThrowable) { return null; }

}

byte[] arrayOfByte = Base64.decodeBase64(paramArrayOfByte); if (arrayOfByte == null) { return null; }

try {

return l.doFinal(arrayOfByte); } catch (Exception localException) { a.error(null, localException); }return null; }

提取出的代码放到eclipse中加工后进行测试。这里需要说明的是,需要把两个文件imc-plat.jar和从网上下载和Cipher加密算法相关的commons-codec-1.8.jar放到jre的相关目录中,我的是:C:\\Program Files\\Java\\jre7\\lib\\ext,在运行的时候会有一些奇怪的错误,解决方式如图。

可用的java代码:desjjm.java package com.h3c.imc.common;

import javax.crypto.spec.DESKeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import org.apache.commons.codec.binary.*;

public class desjjm {

private static final byte[] j = \private static Cipher k; private static Cipher l;

//加密算法

public static byte[] encryptData(byte[] paramArrayOfByte) {

if (k == null) { try {

DESKeySpec localDESKeySpec = new DESKeySpec(j);

SecretKeyFactory localSecretKeyFactory = SecretKeyFactory.getInstance(\ SecretKey localSecretKey = localSecretKeyFactory.generateSecret(localDESKeySpec); k = Cipher.getInstance(\

k.init(1, localSecretKey);

} catch (Exception localException1) { return null; } }

byte[] arrayOfByte = null; try {

arrayOfByte = k.doFinal(paramArrayOfByte); } catch (Exception localException2) { //a.error(null, localException2); return null; }

if (arrayOfByte != null) {

return Base64.encodeBase64(arrayOfByte); }

return null; }

//解密算法

public static byte[] decryptData(byte[] paramArrayOfByte) {

if (l == null) { try {

DESKeySpec localDESKeySpec = new DESKeySpec(j);

SecretKeyFactory localSecretKeyFactory = SecretKeyFactory.getInstance(\ SecretKey localSecretKey = localSecretKeyFactory.generateSecret(localDESKeySpec); l = Cipher.getInstance(\ l.init(2, localSecretKey);

} catch (Throwable localThrowable) { return null; } }

byte[] arrayOfByte = Base64.decodeBase64(paramArrayOfByte); if (arrayOfByte == null) { return null; }

try

{

return l.doFinal(arrayOfByte); } catch (Exception localException) { }return null; }

public static void main(String[] args) { //演示加密算法 String mwpass=\ byte[] jmpass; jmpass=encryptData(mwpass.getBytes()); String dispjmpass; dispjmpass = new String(jmpass); System.out.println(dispjmpass); //演示解密算法 byte[] encryptedpass;//已加密字符串 byte[] realpass;//实际密码二进制串 String realpassStr;//实际密码字符串 String encryptedpassStr=\ encryptedpass = encryptedpassStr.getBytes(); realpass=decryptData(encryptedpass); realpassStr=new String(realpass); System.out.println(realpassStr); //Base64算法测试 //加密 String str= \为要加密的字符串 byte[] b = Base64.encodeBase64(str.getBytes()); System.out.println(new String(b)); //解密 str = \为要解密的字符串 b = Base64.decodeBase64(str.getBytes()); System.out.println(new String(b)); } }

从网上找到的独立的DES和BASE64的JAVA实现代码。 DES.java

package com.h3c.imc.common;

public class DES { // 声明常量字节数组 private static final int[] IP = {

58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56, 48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37, 29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7 }; // 64

private static final int[] IP_1 = {

40, 8, 48, 16, 56, 24, 64, 32, 39, 7, 47, 15, 55, 23, 63, 31, 38, 6, 46, 14, 54, 22, 62, 30, 37, 5, 45, 13, 53, 21, 61, 29, 36, 4, 44, 12, 52, 20, 60, 28, 35, 3, 43, 11, 51, 19, 59, 27, 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; // 64

private static final int[] PC_1 = {

57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7,

62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4 }; // 56

private static final int[] PC_2 = {

14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32 }; // 48

private static final int[] E = {

32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 }; // 48

private static final int[] P = {

16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 }; // 32

private static final int[][][] S_Box = {

{

{ 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7 }, { 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8 }, { 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0 }, { 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13 } },

{ // S_Box[1]

{ 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10 }, { 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5 }, { 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15 }, { 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9 } },

{ // S_Box[2]

{ 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8 }, { 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1 }, { 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7 }, { 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12 } },

{ // S_Box[3]

{ 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15 }, { 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9 }, { 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4 },

{ 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14 } },

{ // S_Box[4]

{ 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9 }, { 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6 }, { 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14 }, { 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3 } },

{ // S_Box[5]

{ 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11 }, { 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8 }, { 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6 }, { 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13 } },

{ // S_Box[6]

{ 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1 }, { 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6 }, { 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2 }, { 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12 } },

{ // S_Box[7]

{ 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7 },

{ 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2 }, { 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8 }, { 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11 } } // S_Box[8] };

private static final int[] LeftMove = {

1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 }; // 左移位置列表

private byte[] UnitDes(byte[] des_key, byte[] des_data, int flag) { // 检测输入参数格式是否正确,错误直接返回空值(null) if ((des_key.length != 8) || (des_data.length != 8) || ((flag != 1) && (flag != 0))) {

throw new RuntimeException(\); }

int flags = flag; // 二进制加密密钥 int[] keydata = new int[64]; // 二进制加密数据

int[] encryptdata = new int[64]; // 加密操作完成后的字节数组 byte[] EncryptCode = new byte[8]; // 密钥初试化成二维数组 int[][] KeyArray = new int[16][48]; // 将密钥字节数组转换成二进制字节数组 keydata = ReadDataToBirnaryIntArray(des_key); // 将加密数据字节数组转换成二进制字节数组 encryptdata = ReadDataToBirnaryIntArray(des_data); // 初试化密钥为二维密钥数组 KeyInitialize(keydata, KeyArray); // 执行加密解密操作

EncryptCode = Encrypt(encryptdata, flags, KeyArray); return EncryptCode; }

// 初试化密钥数组

private void KeyInitialize(int[] key, int[][] keyarray) { int i; int j;

int[] K0 = new int[56];

// 特别注意:xxx[IP[i]-1]等类似变换 for (i = 0; i < 56; i++) {

K0[i] = key[PC_1[i] - 1]; // 密钥进行PC-1变换 }

for (i = 0; i < 16; i++) {

LeftBitMove(K0, LeftMove[i]); // 特别注意:xxx[IP[i]-1]等类似变换 for (j = 0; j < 48; j++) {

keyarray[i][j] = K0[PC_2[j] - 1]; // 生成子密钥keyarray[i][j] } } }

// 执行加密解密操作

private byte[] Encrypt(int[] timeData, int flag, int[][] keyarray) { int i;

byte[] encrypt = new byte[8]; int flags = flag; int[] M = new int[64]; int[] MIP_1 = new int[64];

// 特别注意:xxx[IP[i]-1]等类似变换

for (i = 0; i < 64; i++) {

M[i] = timeData[IP[i] - 1]; // 明文IP变换 }

if (flags == 1) { // 加密 for (i = 0; i < 16; i++) {

LoopF(M, i, flags, keyarray); }

} else if (flags == 0) { // 解密 for (i = 15; i > -1; i--) {

LoopF(M, i, flags, keyarray); } }

for (i = 0; i < 64; i++) {

MIP_1[i] = M[IP_1[i] - 1]; // 进行IP-1运算 }

GetEncryptResultOfByteArray(MIP_1, encrypt);

// 返回加密数据 return encrypt; }

private int[] ReadDataToBirnaryIntArray(byte[] intdata) { int i; int j;

// 将数据转换为二进制数,存储到数组 int[] IntDa = new int[8]; for (i = 0; i < 8; i++) { IntDa[i] = intdata[i]; if (IntDa[i] < 0) { IntDa[i] += 256; IntDa[i] %= 256; } }

int[] IntVa = new int[64]; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) {

IntVa[((i * 8) + 7) - j] = IntDa[i] % 2; IntDa[i] = IntDa[i] / 2; } }

return IntVa; }

private void LeftBitMove(int[] k, int offset) { int i;

// 循环移位操作函数 int[] c0 = new int[28]; int[] d0 = new int[28]; int[] c1 = new int[28]; int[] d1 = new int[28]; for (i = 0; i < 28; i++) { c0[i] = k[i]; d0[i] = k[i + 28]; }

if (offset == 1) {

for (i = 0; i < 27; i++) { // 循环左移一位 c1[i] = c0[i + 1]; d1[i] = d0[i + 1]; }

c1[27] = c0[0]; d1[27] = d0[0]; } else if (offset == 2) {

for (i = 0; i < 26; i++) { // 循环左移两位 c1[i] = c0[i + 2];

go

-- select dbo.DecryptDES(N'+GeLDT6kAxZlm2pnFX8X4w==',N'88888888'); RECONFIGURE

3、 关于C#中和JAVA中DES加密结果不一致的问题 http://bbs.csdn.net/topics/70460072

http://ikoo.info/posts/java-decrypt-csharp-encrypt-data-by-des/(这篇解释得相当完美)

C# RSACryptoServiceProvider加密解密签名验签和DESCryptoServiceProvider加解密

http://www.soaspx.com/dotnet/csharp/csharp_20100204_2738.html

bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4)); bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2)); bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4); }

return bytes; }

private static byte[] discardNonBase64Bytes(byte[] data) { byte[] temp = new byte[data.length]; int bytesCopied = 0;

for (int i = 0; i < data.length; i++) { if (isValidBase64Byte(data[i])) { temp[bytesCopied++] = data[i]; } }

byte[] newData = new byte[bytesCopied];

System.arraycopy(temp, 0, newData, 0, bytesCopied); return newData; }

private static String discardNonBase64Chars(String data) { StringBuffer sb = new StringBuffer(); int length = data.length(); for (int i = 0; i < length; i++) {

if (isValidBase64Byte((byte) (data.charAt(i)))) { sb.append(data.charAt(i)); } }

return sb.toString(); }

private static boolean isValidBase64Byte(byte b) { if (b == '=') { return true;

} else if ((b < 0) || (b >= 128)) { return false;

} else if (decodingTable[b] == -1) { return false; }

return true; }

public static void main(String[] args) { String data = \;

byte[] result = Base64SF.encode(data.getBytes()); System.out.println(data);

System.out.println(new String(result));

System.out.println(new String(Base64SF.decode(new String(result))));

} }

把两个合起来的mydes.java package com.h3c.imc.common;

public class mydes {

private static String genpass(String key,String data) { }

byte[] base64result;

int bytelen = data.getBytes().length;

byte[] result = new byte[(bytelen / 8)*8 + (bytelen % 8)]; byte[] bytekey = key.getBytes(); byte[] bytedata = data.getBytes(); DES des = new DES();

result = des.DesEncrypt(bytekey, bytedata,1); base64result = Base64SF.encode(result); return (new String(base64result));

private static String depass(String key,String data) {

int bytelen = data.getBytes().length;

byte[] result = new byte[(bytelen / 8)*8 + (bytelen % 8)]; byte[] bytekey = key.getBytes(); DES des = new DES();

result = Base64SF.decode(data);

result = des.DesEncrypt(bytekey, result, 0);

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

if (result[i]<32 || result[i]>127) { } }

String outstr; }

public static void main(String[] args) { String key = \; outstr=new String(result);

return outstr.substring(0, outstr.indexOf(\));

result[i]='\\0';

String data = \; //测试加密

String pass = genpass(key,data); System.out.println(pass); //测试解密

String mwpass = depass(key,pass); System.out.println(mwpass); }

附:Base64加解密的MSSQL实现: USE [master] GO

/****** Object: UserDefinedFunction [dbo].[c_GetUTF8Code] Script Date: 06/06/2013 16:32:35 ******/ SET ANSI_NULLS ON GO

SET QUOTED_IDENTIFIER ON GO

ALTER FUNCTION [dbo].[c_GetUTF8Code] (

@char Nchar )

RETURNS int AS

--UTF8转码 BEGIN

Declare @Code int

Select @Code=Cast(Unicode(@char) as int) Declare @Utf8Code int Set @Utf8Code=0

}

int d1; int d2;

switch (modulus) {

case 0: /* nothing left to do */ break; case 1:

d1 = data[data.length - 1] & 0xff; b1 = (d1 >>> 2) & 0x3f; b2 = (d1 << 4) & 0x3f;

bytes[bytes.length - 4] = encodingTable[b1]; bytes[bytes.length - 3] = encodingTable[b2]; bytes[bytes.length - 2] = (byte) '='; bytes[bytes.length - 1] = (byte) '='; break; case 2:

d1 = data[data.length - 2] & 0xff; d2 = data[data.length - 1] & 0xff; b1 = (d1 >>> 2) & 0x3f;

b2 = ((d1 << 4) | (d2 >>> 4)) & 0x3f; b3 = (d2 << 2) & 0x3f;

bytes[bytes.length - 4] = encodingTable[b1]; bytes[bytes.length - 3] = encodingTable[b2];

bytes[bytes.length - 2] = encodingTable[b3]; bytes[bytes.length - 1] = (byte) '='; break; }

return bytes; }

public static byte[] decode(byte[] data) { byte[] bytes; byte b1; byte b2; byte b3; byte b4;

data = discardNonBase64Bytes(data); if (data[data.length - 2] == '=') {

bytes = new byte[(((data.length / 4) - 1) * 3) + 1]; } else if (data[data.length - 1] == '=') {

bytes = new byte[(((data.length / 4) - 1) * 3) + 2]; } else {

bytes = new byte[((data.length / 4) * 3)]; }

for (int i = 0, j = 0; i < (data.length - 4); i += 4, j += 3) { b1 = decodingTable[data[i]];

b2 = decodingTable[data[i + 1]]; b3 = decodingTable[data[i + 2]]; b4 = decodingTable[data[i + 3]]; bytes[j] = (byte) ((b1 << 2) | (b2 >> 4)); bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2)); bytes[j + 2] = (byte) ((b3 << 6) | b4); }

if (data[data.length - 2] == '=') {

b1 = decodingTable[data[data.length - 4]]; b2 = decodingTable[data[data.length - 3]];

bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4)); } else if (data[data.length - 1] == '=') {

b1 = decodingTable[data[data.length - 4]]; b2 = decodingTable[data[data.length - 3]]; b3 = decodingTable[data[data.length - 2]];

bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4)); bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2)); } else {

b1 = decodingTable[data[data.length - 4]]; b2 = decodingTable[data[data.length - 3]]; b3 = decodingTable[data[data.length - 2]]; b4 = decodingTable[data[data.length - 1]];

bytes[bytes.length - 3] = (byte) ((b1 << 2) | (b2 >> 4)); bytes[bytes.length - 2] = (byte) ((b2 << 4) | (b3 >> 2)); bytes[bytes.length - 1] = (byte) ((b3 << 6) | b4); }

return bytes; }

public static byte[] decode(String data) { byte[] bytes; byte b1; byte b2; byte b3; byte b4;

data = discardNonBase64Chars(data); if (data.charAt(data.length() - 2) == '=') {

bytes = new byte[(((data.length() / 4) - 1) * 3) + 1]; } else if (data.charAt(data.length() - 1) == '=') { bytes = new byte[(((data.length() / 4) - 1) * 3) + 2]; } else {

bytes = new byte[((data.length() / 4) * 3)]; }

for (int i = 0, j = 0; i < (data.length() - 4); i += 4, j += 3) { b1 = decodingTable[data.charAt(i)];

b2 = decodingTable[data.charAt(i + 1)]; b3 = decodingTable[data.charAt(i + 2)]; b4 = decodingTable[data.charAt(i + 3)]; bytes[j] = (byte) ((b1 << 2) | (b2 >> 4)); bytes[j + 1] = (byte) ((b2 << 4) | (b3 >> 2)); bytes[j + 2] = (byte) ((b3 << 6) | b4); }

if (data.charAt(data.length() - 2) == '=') {

b1 = decodingTable[data.charAt(data.length() - 4)]; b2 = decodingTable[data.charAt(data.length() - 3)]; bytes[bytes.length - 1] = (byte) ((b1 << 2) | (b2 >> 4)); } else if (data.charAt(data.length() - 1) == '=') {

b1 = decodingTable[data.charAt(data.length() - 4)]; b2 = decodingTable[data.charAt(data.length() - 3)]; b3 = decodingTable[data.charAt(data.length() - 2)]; bytes[bytes.length - 2] = (byte) ((b1 << 2) | (b2 >> 4)); bytes[bytes.length - 1] = (byte) ((b2 << 4) | (b3 >> 2)); } else {

b1 = decodingTable[data.charAt(data.length() - 4)]; b2 = decodingTable[data.charAt(data.length() - 3)]; b3 = decodingTable[data.charAt(data.length() - 2)]; b4 = decodingTable[data.charAt(data.length() - 1)];

本文来源:https://www.bwwdw.com/article/5bah.html

Top