📄 idea.java
字号:
package idea;public class idea{ private int[][]Z=new int[6][9]; private int[][]K=new int[6][9]; public idea(String cle){ byte[]cle2=new byte[16]; for(int i=0;i<16;i++)cle2[i]=Byte.parseByte(cle.substring((i<<1),((i+1)<<1)), 16); Z=setKeyZ(cle2); K=setKeyK(Z); } public idea(byte[]cle){ super(); Z=setKeyZ(cle); K=setKeyK(Z); } public byte[] chiffre(byte[]plaintext){ Math.random();//nombre >=0 et <1 int x1,x2,x3,x4,kk,t1,t2,a; x1=(plaintext[0] & 0xFF) | ((plaintext[1] & 0xFF) <<8); x2=(plaintext[2] & 0xFF) | ((plaintext[3] & 0xFF) <<8); x3=(plaintext[4] & 0xFF) | ((plaintext[5] & 0xFF) <<8); x4=(plaintext[6] & 0xFF) | ((plaintext[7] & 0xFF) <<8); for(int r=0;r<8;r++){ x1=mul(x1,Z[0][r]); x4=mul(x4,Z[3][r]); x2=(x2 + Z[1][r]) & 65535; x3=(x3 + Z[2][r]) & 65535; kk=mul(Z[4][r],(x1^x3)); t1=mul(Z[5][r],(kk+(x2^x4)) & 65535); t2=(kk + t1) & 65535; x1=x1^t1; x4=x4^t2; a=x2^t2; x2=x3^t1; x3=a; } x1=mul(x1,Z[0][8]); x4=mul(x4,Z[3][8]); a=x2; x2=(x3 + Z[1][8]) & 65535; x3=(a + Z[2][8]) & 65535; byte[]ciphertext=new byte[8]; ciphertext[0]=(byte)(x1 & 255); ciphertext[1]=(byte)(x1 >>> 8); ciphertext[2]=(byte)(x2 & 255); ciphertext[3]=(byte)(x2 >>> 8); ciphertext[4]=(byte)(x3 & 255); ciphertext[5]=(byte)(x3 >>> 8); ciphertext[6]=(byte)(x4 & 255); ciphertext[7]=(byte)(x4 >>> 8); return ciphertext ; } public byte[] dechiffre(byte[] ciphertext){ int x1,x2,x3,x4,kk,t1,t2,a; x1=((ciphertext[0] & 0xFF) | ((ciphertext[1] & 0xFF) <<8)); x2=((ciphertext[2] & 0xFF) | ((ciphertext[3] & 0xFF) <<8)); x3=((ciphertext[4] & 0xFF) | ((ciphertext[5] & 0xFF) <<8)); x4=((ciphertext[6] & 0xFF) | ((ciphertext[7] & 0xFF) <<8)); for(int r=0;r<8;r++){ x1=mul(x1,K[0][r]); x4=mul(x4,K[3][r]); x2=((x2 + K[1][r]) & 65535); x3=((x3 + K[2][r]) & 65535); kk=mul(K[4][r],(x1^x3)); t1=mul(K[5][r],(kk+(x2^x4)) & 65535); t2=((kk + t1) & 65535); x1=(x1^t1); x4=(x4^t2); a=(x2^t2); x2=(x3^t1); x3=a; } x1=mul(x1,K[0][8]); x4=mul(x4,K[3][8]); a=x2; x2=((x3 + K[1][8]) & 65535); x3=((a + K[2][8]) & 65535); byte[]plaintext=new byte[8]; plaintext[0]=(byte)(x1 & 0x000000FF); plaintext[1]=(byte)(x1 >>> 8); plaintext[2]=(byte)(x2 & 0x000000FF); plaintext[3]=(byte)(x2 >>> 8); plaintext[4]=(byte)(x3 & 0x000000FF); plaintext[5]=(byte)(x3 >>> 8); plaintext[6]=(byte)(x4 & 0x000000FF); plaintext[7]=(byte)(x4 >>> 8); return plaintext ; } public int[][] setKeyZ(byte[]cle){ int S[]=new int[54]; int[][]Z=new int[6][9]; for(int i=0;i<8;i++)S[i]=(cle[2*i] & 0xFF) | ((cle[2*i+1] & 0xFF) <<8) ; for(int i=8;i<54;i++){ if ((i & 7) < 6)S[i]=((S[i-7]<<9)^(S[i-6]>>>7)) & 65535; else if((i & 7) == 6)S[i]=((S[i-7]<<9)^(S[i-14]>>>7)) & 65535; else S[i]=((S[i-15]<<9)^(S[i-14]>>>7)) & 65535; } for(int i=0;i<9;i++){ for(int j=0;j<6;j++)Z[j][i]=S[6*i+j]; } return Z; } public int[][] setKeyK(int[][]Z){ int[][]K=new int[6][9]; for(int j=0;j<9;j++){ K[0][8-j]=inv(Z[0][j]); K[3][8-j]=inv(Z[3][j]); if(j==0 || j==8){ K[1][8-j]=(65536 - Z[1][j]) & 65535; K[2][8-j]=(65536 - Z[2][j]) & 65535; }else{ K[1][8-j]=(65536 - Z[2][j]) & 65535; K[2][8-j]=(65536 - Z[1][j]) & 65535; } } for(int j=0;j<8;j++){ K[4][j]=Z[4][7-j]; K[5][j]=Z[5][7-j]; } return K; } public int mul(int a, int b) { int p; int q; if (a == 0) p = 65537- b; else if (b == 0) p = 65537- a; else { q= a * b; p= ((q & 65535) - (q>>>16)); if(p<=0)p=p+65537; } return (p & 65535); } public int inv(int x) { int n1,n2,q,r,b1,b2,t; if(x==0)b2=0; else{ n1=65537; n2=x; b2=1; b1=0; r=1; while(r!=0){ r=n1%n2; q=(n1-r)/n2; if(r==0){ if(b2<0)b2=65537+b2; }else{ n1=n2; n2=r; t=b2; b2=b1-q*b2; b1=t; } } } return b2; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -