gift密码算法的java实现(免费完整代码)
轻量级gift算法的java完整实现,可测试。直接上代码:
package gift;public class GIFT {private byte[] S= {0x01,0x0a,0x04,0x0c,0x06,0x0f,0x03,0x09,0x02,0x0d,0x0b,0x07,0x05,0x00,0x08,0x0e};private byte[] InvS= {0x0d,0x00,0x08,0x06,0x02,0x0c,0x04,0x0b,0x0e,0x07,0x01,0x0a,0x03,0x09,0x0f,0x05};private byte[] RCON= {0x01,0x03,0x07,0x0f,0x1f,0x3e,0x3d,0x3b,0x37,0x2f,0x1e,0x3c,0x39,0x33,0x27,0x0e,0x1d,0x3a,0x35,0x2b,0x16,0x2c,0x18,0x30,0x21,0x02,0x05,0x0b};private byte[] plain=null;private byte[] cipher=null;private char[] key=null;private char[] ikey=null;public GIFT(char[] key) {setKey(key);setPlain("");setCipher("");}public GIFT(String key) {setKey(key);setPlain("");setCipher("");}private void geneKey() {this.ikey=new char[56];char[] lkey=this.key;char[] nkey=new char[8];for(int i=0;i<28;i++) {this.ikey[2*i]=lkey[1];this.ikey[2*i+1]=lkey[0];System.arraycopy(lkey, 2, nkey, 0, 6);nkey[6]=(char) (lkey[0]>>12|lkey[0]<<4);nkey[7]=(char) (lkey[1]>>2|lkey[0]<<14);lkey=nkey;}}private byte[] XORIkey(byte[] bin,int round) {char U=this.ikey[2*round];char V=this.ikey[2*round+1];byte rcon=RCON[round];byte[] result=new byte[8];System.arraycopy(bin, 0, result, 0, 8);for(int i=0;i<16;i++) {byte k=(byte) (((U>>i)&0x1)<<(1+4*(i%2)));byte l=(byte) (((V>>i)&0x1)<<(4*(i%2)));result[i/2]=(byte) (result[i/2]^(k|l));}result[0]=(byte) (result[0]^(((rcon&0x01)<<3)|((rcon&0x02)<<6)));result[1]=(byte) (result[1]^(((rcon&0x04)<<1)|((rcon&0x08)<<4)));result[2]=(byte) (result[2]^(((rcon&0x10)>>1)|((rcon&0x20)<<2)));result[7]=(byte) (result[7]^0x80);return result;}private byte[] InvXORIkey(byte[] bin,int round) {char U=this.ikey[2*round];char V=this.ikey[2*round+1];byte rcon=RCON[round];byte[] result=new byte[8];System.arraycopy(bin, 0, result, 0, 8);result[0]=(byte) (result[0]^(((rcon&0x01)<<3)|((rcon&0x02)<<6)));result[1]=(byte) (result[1]^(((rcon&0x04)<<1)|((rcon&0x08)<<4)));result[2]=(byte) (result[2]^(((rcon&0x10)>>1)|((rcon&0x20)<<2)));result[7]=(byte) (result[7]^0x80);for(int i=0;i<16;i++) {byte k=(byte) (((U>>i)&0x1)<<(1+4*(i%2)));byte l=(byte) (((V>>i)&0x1)<<(4*(i%2)));result[i/2]=(byte) (result[i/2]^(k|l));}return result;}private byte[] S(byte[] bin) {byte[] in=byteSplit(bin);byte[] sin= {(byte) ((S[in[0]]<<4)^S[in[1]]),(byte) ((S[in[2]]<<4)^S[in[3]]),(byte) ((S[in[4]]<<4)^S[in[5]]),(byte) ((S[in[6]]<<4)^S[in[7]]),(byte) ((S[in[8]]<<4)^S[in[9]]),(byte) ((S[in[10]]<<4)^S[in[11]]),(byte) ((S[in[12]]<<4)^S[in[13]]),(byte) ((S[in[14]]<<4)^S[in[15]])};return sin;}private byte[] InvS(byte[] bin) {byte[] in=byteSplit(bin);byte[] sin= {(byte) ((InvS[in[0]]<<4)^InvS[in[1]]),(byte) ((InvS[in[2]]<<4)^InvS[in[3]]),(byte) ((InvS[in[4]]<<4)^InvS[in[5]]),(byte) ((InvS[in[6]]<<4)^InvS[in[7]]),(byte) ((InvS[in[8]]<<4)^InvS[in[9]]),(byte) ((InvS[in[10]]<<4)^InvS[in[11]]),(byte) ((InvS[in[12]]<<4)^InvS[in[13]]),(byte) ((InvS[in[14]]<<4)^InvS[in[15]])};return sin;}private byte[] byteReplace(byte[] sin) {byte[] result=new byte[8];result[0]=(byte) (((sin[0 /8]>>0)&0x1)^(((sin[17/8]>>1)&0x1)<<1)^(((sin[34/8]>>2)&0x1)<<2)^(((sin[51/8]>>3)&0x1)<<3)^(((sin[48/8]>>0)&0x1)<<4)^(((sin[1 /8]>>1)&0x1)<<5)^(((sin[18/8]>>2)&0x1)<<6)^(((sin[35/8]>>3)&0x1)<<7));result[1]=(byte) (((sin[32/8]>>0)&0x1)^(((sin[49/8]>>1)&0x1)<<1)^(((sin[2 /8]>>2)&0x1)<<2)^(((sin[19/8]>>3)&0x1)<<3)^(((sin[16/8]>>0)&0x1)<<4)^(((sin[33/8]>>1)&0x1)<<5)^(((sin[50/8]>>2)&0x1)<<6)^(((sin[3 /8]>>3)&0x1)<<7));result[2]=(byte) (((sin[4 /8]>>4)&0x1)^(((sin[21/8]>>5)&0x1)<<1)^(((sin[38/8]>>6)&0x1)<<2)^(((sin[55/8]>>7)&0x1)<<3)^(((sin[52/8]>>4)&0x1)<<4)^(((sin[5 /8]>>5)&0x1)<<5)^(((sin[22/8]>>6)&0x1)<<6)^(((sin[39/8]>>7)&0x1)<<7));result[3]=(byte) (((sin[36/8]>>4)&0x1)^(((sin[53/8]>>5)&0x1)<<1)^(((sin[6 /8]>>6)&0x1)<<2)^(((sin[23/8]>>7)&0x1)<<3)^(((sin[20/8]>>4)&0x1)<<4)^(((sin[37/8]>>5)&0x1)<<5)^(((sin[54/8]>>6)&0x1)<<6)^(((sin[7 /8]>>7)&0x1)<<7));result[4]=(byte) (((sin[8 /8]>>0)&0x1)^(((sin[25/8]>>1)&0x1)<<1)^(((sin[42/8]>>2)&0x1)<<2)^(((sin[59/8]>>3)&0x1)<<3)^(((sin[56/8]>>0)&0x1)<<4)^(((sin[9 /8]>>1)&0x1)<<5)^(((sin[26/8]>>2)&0x1)<<6)^(((sin[43/8]>>3)&0x1)<<7));result[5]=(byte) (((sin[40/8]>>0)&0x1)^(((sin[57/8]>>1)&0x1)<<1)^(((sin[10/8]>>2)&0x1)<<2)^(((sin[27/8]>>3)&0x1)<<3)^(((sin[24/8]>>0)&0x1)<<4)^(((sin[41/8]>>1)&0x1)<<5)^(((sin[58/8]>>2)&0x1)<<6)^(((sin[11/8]>>3)&0x1)<<7));result[6]=(byte) (((sin[12/8]>>4)&0x1)^(((sin[29/8]>>5)&0x1)<<1)^(((sin[46/8]>>6)&0x1)<<2)^(((sin[63/8]>>7)&0x1)<<3)^(((sin[60/8]>>4)&0x1)<<4)^(((sin[13/8]>>5)&0x1)<<5)^(((sin[30/8]>>6)&0x1)<<6)^(((sin[47/8]>>7)&0x1)<<7));result[7]=(byte) (((sin[44/8]>>4)&0x1)^(((sin[61/8]>>5)&0x1)<<1)^(((sin[14/8]>>6)&0x1)<<2)^(((sin[31/8]>>7)&0x1)<<3)^(((sin[28/8]>>4)&0x1)<<4)^(((sin[45/8]>>5)&0x1)<<5)^(((sin[62/8]>>6)&0x1)<<6)^(((sin[15/8]>>7)&0x1)<<7));return result;}private byte[] InvbyteReplace(byte[] sin) {byte[] result=new byte[8];result[0]=(byte) (((sin[0 /8]>>0)&0x1)^(((sin[5 /8]>>5)&0x1)<<1)^(((sin[10/8]>>2)&0x1)<<2)^(((sin[15/8]>>7)&0x1)<<3)^(((sin[16/8]>>0)&0x1)<<4)^(((sin[21/8]>>5)&0x1)<<5)^(((sin[26/8]>>2)&0x1)<<6)^(((sin[31/8]>>7)&0x1)<<7));result[1]=(byte) (((sin[32/8]>>0)&0x1)^(((sin[37/8]>>5)&0x1)<<1)^(((sin[42/8]>>2)&0x1)<<2)^(((sin[47/8]>>7)&0x1)<<3)^(((sin[48/8]>>0)&0x1)<<4)^(((sin[53/8]>>5)&0x1)<<5)^(((sin[58/8]>>2)&0x1)<<6)^(((sin[63/8]>>7)&0x1)<<7));result[2]=(byte) (((sin[12/8]>>4)&0x1)^(((sin[1 /8]>>1)&0x1)<<1)^(((sin[6 /8]>>6)&0x1)<<2)^(((sin[11/8]>>3)&0x1)<<3)^(((sin[28/8]>>4)&0x1)<<4)^(((sin[17/8]>>1)&0x1)<<5)^(((sin[22/8]>>6)&0x1)<<6)^(((sin[27/8]>>3)&0x1)<<7));result[3]=(byte) (((sin[44/8]>>4)&0x1)^(((sin[33/8]>>1)&0x1)<<1)^(((sin[38/8]>>6)&0x1)<<2)^(((sin[43/8]>>3)&0x1)<<3)^(((sin[60/8]>>4)&0x1)<<4)^(((sin[49/8]>>1)&0x1)<<5)^(((sin[54/8]>>6)&0x1)<<6)^(((sin[59/8]>>3)&0x1)<<7));result[4]=(byte) (((sin[8 /8]>>0)&0x1)^(((sin[13/8]>>5)&0x1)<<1)^(((sin[2 /8]>>2)&0x1)<<2)^(((sin[7 /8]>>7)&0x1)<<3)^(((sin[24/8]>>0)&0x1)<<4)^(((sin[29/8]>>5)&0x1)<<5)^(((sin[18/8]>>2)&0x1)<<6)^(((sin[23/8]>>7)&0x1)<<7));result[5]=(byte) (((sin[40/8]>>0)&0x1)^(((sin[45/8]>>5)&0x1)<<1)^(((sin[34/8]>>2)&0x1)<<2)^(((sin[39/8]>>7)&0x1)<<3)^(((sin[56/8]>>0)&0x1)<<4)^(((sin[61/8]>>5)&0x1)<<5)^(((sin[50/8]>>2)&0x1)<<6)^(((sin[55/8]>>7)&0x1)<<7));result[6]=(byte) (((sin[4 /8]>>4)&0x1)^(((sin[9 /8]>>1)&0x1)<<1)^(((sin[14/8]>>6)&0x1)<<2)^(((sin[3 /8]>>3)&0x1)<<3)^(((sin[20/8]>>4)&0x1)<<4)^(((sin[25/8]>>1)&0x1)<<5)^(((sin[30/8]>>6)&0x1)<<6)^(((sin[19/8]>>3)&0x1)<<7));result[7]=(byte) (((sin[36/8]>>4)&0x1)^(((sin[41/8]>>1)&0x1)<<1)^(((sin[46/8]>>6)&0x1)<<2)^(((sin[35/8]>>3)&0x1)<<3)^(((sin[52/8]>>4)&0x1)<<4)^(((sin[57/8]>>1)&0x1)<<5)^(((sin[62/8]>>6)&0x1)<<6)^(((sin[51/8]>>3)&0x1)<<7));return result;}public void encrypt() {byte[] inout=null;inout=byteReplace(S(plain));for(int i=0;i<27;i++) {inout=XORIkey(inout,i);inout=byteReplace(S(inout));}cipher=XORIkey(inout,27);}public void encrypt(byte[] plain) {setPlain(plain);encrypt();}public void encrypt(String plain) {setPlain(plain);encrypt();}public void decrypt() {byte[] inout=null;inout=InvXORIkey(cipher,27);for(int i=26;i>=0;i--) {inout=InvS(InvbyteReplace(inout));inout=InvXORIkey(inout,i);}plain=InvS(InvbyteReplace(inout));}public void decrypt(byte[] cipher) {setCipher(cipher);decrypt();}public void decrypt(String cipher) {setCipher(cipher);decrypt();}private byte[] byteSplit(byte[] byteArray) {byte[] result=new byte[byteArray.length*2];for(int i=0;i>4);result[2*i+1]=(byte) (byteArray[i]&0x0f);}return result;}public void showIkey() {System.out.print("IKey:\n U: ");for(int i=0;i<28;i++) {System.out.print(String.format("%02x ",(byte)this.ikey[2*i+1]));}System.out.print("\n V: ");for(int i=0;i<28;i++) {System.out.print(String.format("%02x ",(byte)this.ikey[2*i]));}System.out.println();}public void showInfo() {showIkey();showKey();showPlain();showCipher();}public void showKey() {int i;System.out.print("Key:\n Hex:");for(i=0;i<8;i++)System.out.print(String.format("%02x ",(byte)this.key[i]));System.out.print("\n CH:");String strKey=new String(this.key);System.out.println(strKey);}public void showPlain() {int i;System.out.print("Plaintext:\n Hex:");for(i=0;i<8;i++)System.out.print(String.format("%02x ",this.plain[i]));System.out.print("\n CH:");String strPlaintext=new String(this.plain);System.out.println(strPlaintext);}public void showCipher() {int i;System.out.print("Ciphertext:\n Hex:");for(i=0;i<8;i++)System.out.print(String.format("%02x ",this.cipher[i]));System.out.println();}public byte[] getPlain() {return plain;}public void setPlain(byte[] plain) {this.plain=new byte[8];int n=plain.length>8?8:plain.length;for(int i=0;i8?8:cipher.length;for(int i=0;i8?8:key.length;for(int i=0;i
测试代码如下:
package gift;public class testGift {public static void main(String[] args) {String key="gift key";String plain="testgift";GIFT gift=new GIFT(key);gift.encrypt(plain);gift.showCipher();gift.decrypt();gift.showPlain();}
}
测试结果如下:
Ciphertext:
Hex:f1 15 45 73 13 21 6d 21
Plaintext:
Hex:74 65 73 74 67 69 66 74
CH:testgift
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
