/*****************************************************************************/ /* IDEA Encryption Algorithm */ /*****************************************************************************/ /* */ /* IDEA (International Data Encryption Algorithm) is a block encryption */ /* algorithm whose development results from a co-operation between the */ /* Swiss Federal Institute of Technology Zurich (ETHZ) and Ascom Tech Ltd. */ /* IDEA encrypts or decrypts 64-bit data blocks, using symmetric 128-bit */ /* keys. The 128-bit keys are furthermore expanded to 52 16-bit subkeys. */ /* */ /* For detailed technical information on IDEA contact: */ /* */ /* Ascom Systec Ltd. E-Mail: [email protected] */ /* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */ /* CH-5506 Maegenwil */ /* Switzerland */ /* */ /* Patent rights of Ascom Systec Ltd. granted in Europe, Japan and the US. */ /* All other rights reserved. */ /* */ /* For detailed patent information on IDEA contact: */ /* */ /* Ascom Systec Ltd. E-Mail: [email protected] */ /* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */ /* CH-5506 Maegenwil */ /* Switzerland */ /* */ /*****************************************************************************/ /* */ /* Author: Alain Beuchat/Daniel Zimmermann */ /* Release: 2.1 */ /* */ /*****************************************************************************/ #ifndef _ideaplus_h_ #define _ideaplus_h_#define UNIX#ifdef WIN32 #include <windows.h> #else #include <netinet/in.h> #endif#include "c_ext_fc.h" #include "c_fct.h"typedef unsigned short uint16; /* 16-bit word */ typedef unsigned int uint32; /* 32-bit word */ typedef int int32; /* 32-bit int */#define IDEA_ROUNDS 8 /* Number of IDEA rounds */ #define IDEA_SK_NUM (6 * IDEA_ROUNDS + 4) /* Number of IDEA subkeys */typedef uint16 idea_block_t[4]; /* in/output IDEA data block */ typedef uint16 idea_key_t[8]; /* local key type */ typedef uint16 idea_subkeys_t[IDEA_SK_NUM]; /* local IDEA subkeys type */extern _VOID_ idea_encrypt_subkeys C_EXT_ARG((idea_key_t,idea_subkeys_t)); extern _VOID_ idea_decrypt_subkeys C_EXT_ARG((idea_subkeys_t,idea_subkeys_t)); extern _VOID_ idea_cipher C_EXT_ARG((idea_block_t,idea_block_t,idea_subkeys_t));int ideaEncrypt(char*, char*, char*, int, int); int ideaDecrypt(char*, char*, char*, int); extern void changeToNetData(unsigned short *, int); extern void changeToHostData(unsigned short *, int);#endif /* !_ideaplus_h_ */ //-------------------------------------ideaplus.h头文件---------------------------
/*****************************************************************************/ /* IDEA Encryption Algorithm */ /*****************************************************************************/ /* */ /* IDEA (International Data Encryption Algorithm) is a block encryption */ /* algorithm whose development results from a co-operation between the */ /* Swiss Federal Institute of Technology Zurich (ETHZ) and Ascom Tech Ltd. */ /* IDEA encrypts or decrypts 64-bit data blocks, using symmetric 128-bit */ /* keys. The 128-bit keys are furthermore expanded to 52 16-bit subkeys. */ /* */ /* For detailed technical information on IDEA contact: */ /* */ /* Ascom Systec Ltd. E-Mail: [email protected] */ /* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */ /* CH-5506 Maegenwil */ /* Switzerland */ /* */ /* Patent rights of Ascom Systec Ltd. granted in Europe, Japan and the US. */ /* All other rights reserved. */ /* */ /* For detailed patent information on IDEA contact: */ /* */ /* Ascom Systec Ltd. E-Mail: [email protected] */ /* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */ /* CH-5506 Maegenwil */ /* Switzerland */ /* */ /*****************************************************************************/ /* */ /* Author: Alain Beuchat/Daniel Zimmermann */ /* Release: 2.1 */ /* */ /*****************************************************************************/ #include "ideaplus.h" #include <string.h>#define LSW16(y) ((y) & 0xffff) /* low significant 16-bit */ #define MSW16(y) ((y >> 16) & 0xffff) /* most significant 16-bit */ #define MUL_MOD (uint32)(((uint32)1 << 16) | 1) /* 2**16 + 1 */
public class IDEA {public IDEA() { }public byte[] ideaEncrypt(byte[] bytekey, byte[] inputBytes, boolean flag) { byte[] encryptCode = new byte[8]; // 分解子密钥 int[] key = get_subkey(flag, bytekey); // 进行加密操作 encrypt(key, inputBytes, encryptCode); // 返回加密数据 return encryptCode; }private int bytesToInt(byte[] inBytes, int startPos) { return ((inBytes[startPos] << 8) & 0xff00) + (inBytes[startPos + 1] & 0xff); }private void intToBytes(int inputInt, byte[] outBytes, int startPos) { outBytes[startPos] = (byte) (inputInt >>> 8); outBytes[startPos + 1] = (byte) inputInt; }private int x_multiply_y(int x, int y) { if (x == 0) { x = 0x10001 - y; } else if (y == 0) { x = 0x10001 - x; } else { int tmp = x * y; y = tmp & 0xffff; x = tmp >>> 16; x = (y - x) + ((y < x) ? 1 : 0); } return x & 0xffff; }private void encrypt(int[] key, byte[] inbytes, byte[] outbytes) { int k = 0; int a = bytesToInt(inbytes, 0); int b = bytesToInt(inbytes, 2); int c = bytesToInt(inbytes, 4); int d = bytesToInt(inbytes, 6);for (int i = 0; i < 8; i++) { a = x_multiply_y(a, key[k++]); b += key[k++]; b &= 0xffff; c += key[k++]; c &= 0xffff; d = x_multiply_y(d, key[k++]);int tmp1 = b; int tmp2 = c; c ^= a; b ^= d; c = x_multiply_y(c, key[k++]); b += c; b &= 0xffff; b = x_multiply_y(b, key[k++]); c += b; c &= 0xffff; a ^= b; d ^= c; b ^= tmp2; c ^= tmp1; }intToBytes(x_multiply_y(a, key[k++]), outbytes, 0); intToBytes(c + key[k++], outbytes, 2); intToBytes(b + key[k++], outbytes, 4); intToBytes(x_multiply_y(d, key[k]), outbytes, 6); }private int[] encrypt_subkey(byte[] byteKey) { int[] key = new int[52];if (byteKey.length < 16) { byte[] tmpkey = new byte[16]; System.arraycopy(byteKey, 0, tmpkey, tmpkey.length - byteKey.length, byteKey.length); byteKey = tmpkey; }for (int i = 0; i < 8; i++) { key[i] = bytesToInt(byteKey, i * 2); } for (int j = 8; j < 52; j++) { if ((j & 0x7) < 6) { key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 6] >> 7)) & 0xffff; } else if ((j & 0x7) == 6) { key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff; } else { key[j] = (((key[j - 15] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff; } }return key; }private int fun_a(int a) { if (a < 2) { return a; } int b = 1; int c = 0x10001 / a; for (int i = 0x10001 % a; i != 1;) { int d = a / i; a %= i; b = (b + (c * d)) & 0xffff; if (a == 1) { return b; } d = i / a; i %= a; c = (c + (b * d)) & 0xffff; } return (1 - c) & 0xffff; }private int fun_b(int b) { return (0 - b) & 0xffff; }private int[] uncrypt_subkey(int[] key) { int dec = 52; int asc = 0; int[] unkey = new int[52]; int aa = fun_a(key[asc++]); int bb = fun_b(key[asc++]); int cc = fun_b(key[asc++]); int dd = fun_a(key[asc++]); unkey[--dec] = dd; unkey[--dec] = cc; unkey[--dec] = bb; unkey[--dec] = aa;for (int k1 = 1; k1 < 8; k1++) { aa = key[asc++]; bb = key[asc++]; unkey[--dec] = bb; unkey[--dec] = aa; aa = fun_a(key[asc++]); bb = fun_b(key[asc++]); cc = fun_b(key[asc++]); dd = fun_a(key[asc++]); unkey[--dec] = dd; unkey[--dec] = bb; unkey[--dec] = cc; unkey[--dec] = aa; }aa = key[asc++]; bb = key[asc++]; unkey[--dec] = bb; unkey[--dec] = aa; aa = fun_a(key[asc++]); bb = fun_b(key[asc++]); cc = fun_b(key[asc++]); dd = fun_a(key[asc]); unkey[--dec] = dd; unkey[--dec] = cc; unkey[--dec] = bb; unkey[--dec] = aa;return unkey; }private int[] get_subkey(boolean flag, byte[] bytekey) { if (flag) { return encrypt_subkey(bytekey); } else { return uncrypt_subkey(encrypt_subkey(bytekey)); } }public static void main(String[] args) { IDEA idea = new IDEA(); byte[] key = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44, (byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x88, (byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc, (byte) 0xdd, (byte) 0xee, (byte) 0xff, (byte) 0x00 }; byte[] a = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78, (byte) 0x21, (byte) 0x43, (byte) 0x65, (byte) 0x87 }; byte[] b = idea.ideaEncrypt(key, a, true); byte[] c = idea.ideaEncrypt(key, b, false); for (int i = 0; i < 8; i++) { System.out.println("--------------"); System.out.println((int) a[i]); System.out.println((int) c[i]); System.out.println("--------------"); } } }
int i ;
int tmp,x;
int zz[]=new int[6];
for ( i = 0 ; i < 48 ; i += 6) { /*进行8轮循环*/
for(int j=0,box=i;j<6;j++,box++){
zz[j]=key1[box];
}
x = handle_data(data11,zz);
tmp = data11[1]; /*交换中间两个*/
data11[1] = data11[2];
data11[2] = tmp;
}
tmp = data11[1]; /*最后一轮不交换*/
data11[1] = data11[2];
data11[2] = tmp;
data11[0] = MUL(data11[0],key1[48]);
data11[1] =(char)((data11[1] + key1[49])%0x10000);
data11[2] =(char)((data11[2] + key1[50])%0x10000);
data11[3] = MUL(data11[3],key1[51]);
} 2.解密过程的实现 void key_decryExp(int outkey[])/*解密密钥的变逆处理*/
{ int tmpkey[] = new int[52] ;
int i;
for ( i = 0 ; i < 52 ; i++) {
tmpkey[i] = outkey[ wz_spkey[i] ] ;/*换位*/
}
for ( i = 0 ; i < 52 ; i++) {
outkey[i] = tmpkey[i];
}
for ( i = 0 ; i < 18 ; i++) {
outkey[wz_spaddrever[i]] = (char)(65536-outkey[wz_spaddrever[i]]) ;/*替换成加法逆*/
}
for ( i = 0 ; i < 18 ; i++){
outkey[wz_spmulrevr[i]] =(char)(mulInv(outkey[wz_spmulrevr[i]] ));/*替换成乘法逆*/
}
}
/* IDEA Encryption Algorithm */
/*****************************************************************************/
/* */
/* IDEA (International Data Encryption Algorithm) is a block encryption */
/* algorithm whose development results from a co-operation between the */
/* Swiss Federal Institute of Technology Zurich (ETHZ) and Ascom Tech Ltd. */
/* IDEA encrypts or decrypts 64-bit data blocks, using symmetric 128-bit */
/* keys. The 128-bit keys are furthermore expanded to 52 16-bit subkeys. */
/* */
/* For detailed technical information on IDEA contact: */
/* */
/* Ascom Systec Ltd. E-Mail: [email protected] */
/* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */
/* CH-5506 Maegenwil */
/* Switzerland */
/* */
/* Patent rights of Ascom Systec Ltd. granted in Europe, Japan and the US. */
/* All other rights reserved. */
/* */
/* For detailed patent information on IDEA contact: */
/* */
/* Ascom Systec Ltd. E-Mail: [email protected] */
/* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */
/* CH-5506 Maegenwil */
/* Switzerland */
/* */
/*****************************************************************************/
/* */
/* Author: Alain Beuchat/Daniel Zimmermann */
/* Release: 2.1 */
/* */
/*****************************************************************************/
#ifndef _ideaplus_h_
#define _ideaplus_h_#define UNIX#ifdef WIN32
#include <windows.h>
#else
#include <netinet/in.h>
#endif#include "c_ext_fc.h"
#include "c_fct.h"typedef unsigned short uint16; /* 16-bit word */
typedef unsigned int uint32; /* 32-bit word */
typedef int int32; /* 32-bit int */#define IDEA_ROUNDS 8 /* Number of IDEA rounds */
#define IDEA_SK_NUM (6 * IDEA_ROUNDS + 4) /* Number of IDEA subkeys */typedef uint16 idea_block_t[4]; /* in/output IDEA data block */
typedef uint16 idea_key_t[8]; /* local key type */
typedef uint16 idea_subkeys_t[IDEA_SK_NUM]; /* local IDEA subkeys type */extern _VOID_ idea_encrypt_subkeys C_EXT_ARG((idea_key_t,idea_subkeys_t));
extern _VOID_ idea_decrypt_subkeys C_EXT_ARG((idea_subkeys_t,idea_subkeys_t));
extern _VOID_ idea_cipher C_EXT_ARG((idea_block_t,idea_block_t,idea_subkeys_t));int ideaEncrypt(char*, char*, char*, int, int);
int ideaDecrypt(char*, char*, char*, int);
extern void changeToNetData(unsigned short *, int);
extern void changeToHostData(unsigned short *, int);#endif /* !_ideaplus_h_ */
//-------------------------------------ideaplus.h头文件---------------------------
/* IDEA Encryption Algorithm */
/*****************************************************************************/
/* */
/* IDEA (International Data Encryption Algorithm) is a block encryption */
/* algorithm whose development results from a co-operation between the */
/* Swiss Federal Institute of Technology Zurich (ETHZ) and Ascom Tech Ltd. */
/* IDEA encrypts or decrypts 64-bit data blocks, using symmetric 128-bit */
/* keys. The 128-bit keys are furthermore expanded to 52 16-bit subkeys. */
/* */
/* For detailed technical information on IDEA contact: */
/* */
/* Ascom Systec Ltd. E-Mail: [email protected] */
/* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */
/* CH-5506 Maegenwil */
/* Switzerland */
/* */
/* Patent rights of Ascom Systec Ltd. granted in Europe, Japan and the US. */
/* All other rights reserved. */
/* */
/* For detailed patent information on IDEA contact: */
/* */
/* Ascom Systec Ltd. E-Mail: [email protected] */
/* Gewerbepark http://WWW.ASCOM.COM/INFOSEC */
/* CH-5506 Maegenwil */
/* Switzerland */
/* */
/*****************************************************************************/
/* */
/* Author: Alain Beuchat/Daniel Zimmermann */
/* Release: 2.1 */
/* */
/*****************************************************************************/
#include "ideaplus.h"
#include <string.h>#define LSW16(y) ((y) & 0xffff) /* low significant 16-bit */
#define MSW16(y) ((y >> 16) & 0xffff) /* most significant 16-bit */
#define MUL_MOD (uint32)(((uint32)1 << 16) | 1) /* 2**16 + 1 */
}public byte[] ideaEncrypt(byte[] bytekey, byte[] inputBytes, boolean flag) {
byte[] encryptCode = new byte[8];
// 分解子密钥
int[] key = get_subkey(flag, bytekey);
// 进行加密操作
encrypt(key, inputBytes, encryptCode);
// 返回加密数据
return encryptCode;
}private int bytesToInt(byte[] inBytes, int startPos) {
return ((inBytes[startPos] << 8) & 0xff00)
+ (inBytes[startPos + 1] & 0xff);
}private void intToBytes(int inputInt, byte[] outBytes, int startPos) {
outBytes[startPos] = (byte) (inputInt >>> 8);
outBytes[startPos + 1] = (byte) inputInt;
}private int x_multiply_y(int x, int y) {
if (x == 0) {
x = 0x10001 - y;
} else if (y == 0) {
x = 0x10001 - x;
} else {
int tmp = x * y;
y = tmp & 0xffff;
x = tmp >>> 16;
x = (y - x) + ((y < x) ? 1 : 0);
}
return x & 0xffff;
}private void encrypt(int[] key, byte[] inbytes, byte[] outbytes) {
int k = 0;
int a = bytesToInt(inbytes, 0);
int b = bytesToInt(inbytes, 2);
int c = bytesToInt(inbytes, 4);
int d = bytesToInt(inbytes, 6);for (int i = 0; i < 8; i++) {
a = x_multiply_y(a, key[k++]);
b += key[k++];
b &= 0xffff;
c += key[k++];
c &= 0xffff;
d = x_multiply_y(d, key[k++]);int tmp1 = b;
int tmp2 = c;
c ^= a;
b ^= d;
c = x_multiply_y(c, key[k++]);
b += c;
b &= 0xffff;
b = x_multiply_y(b, key[k++]);
c += b;
c &= 0xffff;
a ^= b;
d ^= c;
b ^= tmp2;
c ^= tmp1;
}intToBytes(x_multiply_y(a, key[k++]), outbytes, 0);
intToBytes(c + key[k++], outbytes, 2);
intToBytes(b + key[k++], outbytes, 4);
intToBytes(x_multiply_y(d, key[k]), outbytes, 6);
}private int[] encrypt_subkey(byte[] byteKey) {
int[] key = new int[52];if (byteKey.length < 16) {
byte[] tmpkey = new byte[16];
System.arraycopy(byteKey, 0, tmpkey,
tmpkey.length - byteKey.length, byteKey.length);
byteKey = tmpkey;
}for (int i = 0; i < 8; i++) {
key[i] = bytesToInt(byteKey, i * 2);
}
for (int j = 8; j < 52; j++) {
if ((j & 0x7) < 6) {
key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 6] >> 7)) & 0xffff;
} else if ((j & 0x7) == 6) {
key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff;
} else {
key[j] = (((key[j - 15] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff;
}
}return key;
}private int fun_a(int a) {
if (a < 2) {
return a;
}
int b = 1;
int c = 0x10001 / a;
for (int i = 0x10001 % a; i != 1;) {
int d = a / i;
a %= i;
b = (b + (c * d)) & 0xffff;
if (a == 1) {
return b;
}
d = i / a;
i %= a;
c = (c + (b * d)) & 0xffff;
}
return (1 - c) & 0xffff;
}private int fun_b(int b) {
return (0 - b) & 0xffff;
}private int[] uncrypt_subkey(int[] key) {
int dec = 52;
int asc = 0;
int[] unkey = new int[52];
int aa = fun_a(key[asc++]);
int bb = fun_b(key[asc++]);
int cc = fun_b(key[asc++]);
int dd = fun_a(key[asc++]);
unkey[--dec] = dd;
unkey[--dec] = cc;
unkey[--dec] = bb;
unkey[--dec] = aa;for (int k1 = 1; k1 < 8; k1++) {
aa = key[asc++];
bb = key[asc++];
unkey[--dec] = bb;
unkey[--dec] = aa;
aa = fun_a(key[asc++]);
bb = fun_b(key[asc++]);
cc = fun_b(key[asc++]);
dd = fun_a(key[asc++]);
unkey[--dec] = dd;
unkey[--dec] = bb;
unkey[--dec] = cc;
unkey[--dec] = aa;
}aa = key[asc++];
bb = key[asc++];
unkey[--dec] = bb;
unkey[--dec] = aa;
aa = fun_a(key[asc++]);
bb = fun_b(key[asc++]);
cc = fun_b(key[asc++]);
dd = fun_a(key[asc]);
unkey[--dec] = dd;
unkey[--dec] = cc;
unkey[--dec] = bb;
unkey[--dec] = aa;return unkey;
}private int[] get_subkey(boolean flag, byte[] bytekey) {
if (flag) {
return encrypt_subkey(bytekey);
} else {
return uncrypt_subkey(encrypt_subkey(bytekey));
}
}public static void main(String[] args) {
IDEA idea = new IDEA();
byte[] key = { (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
(byte) 0x55, (byte) 0x66, (byte) 0x77, (byte) 0x88,
(byte) 0x99, (byte) 0xaa, (byte) 0xbb, (byte) 0xcc,
(byte) 0xdd, (byte) 0xee, (byte) 0xff, (byte) 0x00 };
byte[] a = { (byte) 0x12, (byte) 0x34, (byte) 0x56, (byte) 0x78,
(byte) 0x21, (byte) 0x43, (byte) 0x65, (byte) 0x87 };
byte[] b = idea.ideaEncrypt(key, a, true);
byte[] c = idea.ideaEncrypt(key, b, false);
for (int i = 0; i < 8; i++) {
System.out.println("--------------");
System.out.println((int) a[i]);
System.out.println((int) c[i]);
System.out.println("--------------");
}
}
}