/*
* To change this tempate, choose Toos | Tempates
* and open the tempate in the editor.
*/
package des_math;
/**
*
* @author Michael
*/
public class DES implements DES_Constant {
private int []KnL = new int[32];
public int TripleDES(short []key1,short []key2,short []key3,short[]data,int blocks) {
this.encrypt(key1, data, blocks);
this.decrypt(key2, data, blocks);
this.encrypt(key3, data, blocks);
return 1;
}
public int encrypt ( short []key, short[] data, int blocks )
{
if ((data==null)||(blocks<1))
return 0;
deskey ( key, true );
des ( data, data, blocks);
return 1;
}
public int decrypt ( short []key, short[] data, int blocks )
{
if ((data==null)||(blocks<1))
return 0;
deskey ( key, false );
des ( data, data, blocks);
return 1;
}
public int yencrypt ( short []key, short []data, int size )
{
if ((data==null) || (size<1))
return 0;
// The last char of data is bitwise complemented and filled the rest
// buffer.If size is 16, it will extend to 24,and 17 still 24.
//char lastChar = *(data+size-1);
short lastChar = data[size-1];
int blocks = size/8+1;
//::memset(data+size, ~lastChar, blocks*8-size);
for(int i=0; i<(blocks*8-size); i++) {
data[size+i] = (short)~lastChar;
}
deskey( key, false );
return encrypt ( data, data, blocks);
}
public int ydecrypt ( short []key, short[] data, int blocks, int []size )
{
if ( (data==null) || (blocks<1) )
return 0;
deskey ( key, false );
// if ( decrypt ( data, data, blocks) == null )
// return 0;
if ( size[0] != 0 )
{
int pos = blocks*8-1;
short endChar = data[pos];
while ((pos>0)&&(data[pos]==endChar))
pos--;
if ( data[pos] != ~endChar )
return 0;
size[0] = pos+1;
}
return 1;
}
//void DES::des ( unsigned char* in, unsigned char* out, int blocks )
protected void des(short []in, short []out, int blocks)
{
int iIndex=0, oIndex=0;
for (int i = 0; i < blocks; i++,iIndex+=8,oIndex+=8) {
short []tmp_in = new short[8];
System.arraycopy(in, iIndex,tmp_in, 0, 8);
short []tmp_out = new short[8];
System.arraycopy(out, oIndex,tmp_out, 0, 8);
des_block(tmp_in,tmp_out);
System.arraycopy(tmp_in, 0, in, iIndex, 8);
System.arraycopy(tmp_out, 0, out, oIndex, 8);
}
}
//void DES::des_block(unsigned char *in, unsigned char *out)
protected void des_block(short []in, short []out)
{
int []work = new int[2];
// in is the input bytes for encryption
this.scrunch(in, work);
this.desfunc(work, KnL);
this.unscrun(work, out);
};
//void DES::deskey(unsigned char key[8], Mode md)
protected void deskey(short []key, boolean md) //true = encrypt, false = decrypt
{
//register int ii, j, l, m, n;
//unsigned char pc1m[56], pcr[56];
//unsigned long kn[32];
int ii,j,l,m,n;
short []pc1m = new short[56];
short []pcr = new short[56];
int []kn = new int[32];
for (j = 0; j < 56; j++) {
l = pc1[j];
m = l & 07;
pc1m[j] = ((key[l >> 3] & bytebit[m])>0) ? (short)1: (short)0;
}
for (ii = 0; ii < 16; ii++) {
if (md == false)
m = (15 - ii) << 1;
else
m = ii << 1;
n = m + 1;
kn[m] = kn[n] = 0;
for (j = 0; j < 28; j++) {
l = j + totrot[ii];
if (l < 28)
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l - 28];
}
for (j = 28; j < 56; j++) {
l = j + totrot[ii];
if (l < 56)
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l - 28];
}
for (j = 0; j < 24; j++) {
if (pcr[pc2[j]] > 0)
kn[m] |= bigbyte[j];
if (pcr[pc2[j+24]] > 0)
kn[n] |= bigbyte[j];
}
}
this.cookey(kn);
return;
};
protected void cookey(int []raw1)
{
int raw0;
int []dough = new int[32];
int i, doughtIndex=0, raw1Index=0;
//for (i = 0; i < 16; i++, raw1++) {
for (i = 0; i < 16; i++, raw1Index++) {
//raw0 = raw1++;
raw0 = raw1[raw1Index++];
//*cook = (*raw0 & 0x00fc0000L) << 6;
dough[doughtIndex] = (raw0 & 0x00fc0000) << 6;
//*cook |= (*raw0 & 0x00000fc0L) << 10;
dough[doughtIndex] = dough[doughtIndex] | ((raw0 & 0x00000fc0) << 10);
//*cook |= (*raw1 & 0x00fc0000L) >> 10;
dough[doughtIndex] = dough[doughtIndex] | ((raw1[raw1Index] & 0x00fc0000) >>> 10);
//*cook++ |= (*raw1 & 0x00000fc0L) >> 6;
dough[doughtIndex] = dough[doughtIndex] | ((raw1[raw1Index] & 0x00000fc0) >>> 6);doughtIndex++;
//*cook = (*raw0 & 0x0003f000L) << 12;
dough[doughtIndex] = (raw0 & 0x0003f000) << 12;
//*cook |= (*raw0 & 0x0000003fL) << 16;
dough[doughtIndex] = dough[doughtIndex] | ((raw0 & 0x0000003f) << 16);
//*cook |= (*raw1 & 0x0003f000L) >> 4;
dough[doughtIndex] = dough[doughtIndex] | ((raw1[raw1Index] & 0x0003f000) >>> 4);
//*cook++ |= (*raw1 & 0x0000003fL);
dough[doughtIndex] = dough[doughtIndex] | (raw1[raw1Index] & 0x0000003f); doughtIndex++;
}
this.usekey(dough);
return;
}
private void usekey(int []from)
{
int index = 0;
while (index < this.KnL.length) {
this.KnL[index] = from[index];
index++;
}
return;
}
//Original defintion - void DES::unscrun(register unsigned long *outof, register unsigned char *into) {
protected void unscrun( int []outof, short []into ) {
int oIndex = 0, iIndex = 0;
//*into++ = (*outof >> 24) & 0xffL;
into[iIndex] = (short)((outof[oIndex] >>> 24) & 0xff); iIndex++;
//*into++ = (*outof >> 16) & 0xffL;
into[iIndex] = (short)((outof[oIndex] >>> 16) & 0xff); iIndex++;
//*into++ = (*outof >> 8) & 0xffL;
into[iIndex] = (short)((outof[oIndex] >>> 8) & 0xff); iIndex++;
// *into++ = *outof++ & 0xffL;
into[iIndex] = (short)(outof[oIndex] & 0xff); iIndex++; oIndex++;
// *into++ = (*outof >> 24) & 0xffL;
into[iIndex] = (short)((outof[oIndex] >>> 24) & 0xff); iIndex++;
// *into++ = (*outof >> 16) & 0xffL;
into[iIndex] = (short)((outof[oIndex] >>> 16) & 0xff); iIndex++;
// *into++ = (*outof >> 8) & 0xffL;
into[iIndex] = (short)((outof[oIndex] >>> 8) & 0xff); iIndex++;
// *into = *outof & 0xffL;
into[iIndex] = (short)(outof[oIndex] & 0xff);
return;
}
//The original method is void (unsigned char *outof, unsigned long *into )
//As Java doesn't have pointer, so we change argument 2 to an array
protected void scrunch( short []outof, int []into ) {
int oIndex = 0, iIndex = 0;
//into = (*outof++ & 0xffL) << 24;
into[iIndex] = (outof[oIndex] & 0xff) << 24; oIndex++;
//*into |= (*outof++ & 0xffL) << 16;
into[iIndex] = into[iIndex] | ((outof[oIndex] & 0xff) << 16); oIndex++;
//*into |= (*outof++ & 0xffL) << 8;
into[iIndex] = into[iIndex] | ((outof[oIndex] & 0xff) << 8); oIndex++;
//*into++ |= (*outof++ & 0xffL);
into[iIndex] =into[iIndex] | (outof[oIndex] & 0xff); iIndex++; oIndex++;
//*into = (*outof++ & 0xffL) << 24;
into[iIndex] =(outof[oIndex] & 0xff) << 24; oIndex++;
//*into |= (*outof++ & 0xffL) << 16;
into[iIndex] =into[iIndex] | ((outof[oIndex] & 0xff)<<16); oIndex++;
//*into |= (*outof++ & 0xffL) << 8;
into[iI
没有合适的资源?快使用搜索试试~ 我知道了~
可用于J2ME的DES/3DES算法,附带源码.
共2个文件
java:2个
4星 · 超过85%的资源 需积分: 10 83 下载量 102 浏览量
2010-08-13
12:25:24
上传
评论
收藏 4KB ZIP 举报
温馨提示
在J2SE中,应用DES/3DES算法是非常简单的事情,javax.crypto包已经给我们提供全部所需要的功能. 但是在J2ME中,由于没有crypto包所以需要自己编写DES算法程序. 这里的代码实际上是从一个C++的算法翻译过来的.是一个纯JAVA应用,不基于任何javax.* 类.理论上可以应用于任何平台的Java系统. 经过测试,其计算结果与J2SE的javax.crypto.Cipher类计算结果完成相同. 使用举例: DES des = new DES(); des.encrypt(key1,data,1) //进行一轮DES加密 des.TripleDES(key1,key2,key3,data,1); //进行一轮3DES加密
资源推荐
资源详情
资源评论
收起资源包目录
DES_4_J2ME.zip (2个子文件)
DES_Constant.java 8KB
DES.java 11KB
共 2 条
- 1
资源评论
- zz_poppet52013-06-06需要好好研究一下才能使用,费了好半天也没看懂short数组为什么非得是8位。
- 点金手2013-03-10哈哈,找了个高人帮我把这个程序稍微改了下,可以用了,赞一个
- vestiges2014-08-13非常好,正用着呢,感谢楼主分享!!
- HHPP_EX2015-06-30用着不错,受用了
- ancienthu2012-11-02资源是挺好的,但是只是des的 并不是3DES的
yinyuanqings
- 粉丝: 20
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功