⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 destest.java

📁 DES算法的JAVA实现源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
	{(byte)0xC1,(byte)0x97,(byte)0xF5,(byte)0x58,	 (byte)0x74,(byte)0x8A,(byte)0x20,(byte)0xE7},	{(byte)0x43,(byte)0x34,(byte)0xCF,(byte)0xDA,	 (byte)0x22,(byte)0xC4,(byte)0x86,(byte)0xC8},	{(byte)0x08,(byte)0xD7,(byte)0xB4,(byte)0xFB,	 (byte)0x62,(byte)0x9D,(byte)0x08,(byte)0x85}};  public static byte cbc_key[] = {    (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,    (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef};  public static byte cbc2_key[] = {    (byte)0xf0,(byte)0xe1,(byte)0xd2,(byte)0xc3,    (byte)0xb4,(byte)0xa5,(byte)0x96,(byte)0x87};  public static byte cbc3_key[] = {    (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,    (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};  public static byte cbc_iv[] = {    (byte)0xfe,(byte)0xdc,(byte)0xba,(byte)0x98,    (byte)0x76,(byte)0x54,(byte)0x32,(byte)0x10};  static byte cbc_data[] = new byte [40];  static String cbc_string = "7654321 Now is the time for ";  public static byte cbc_ok[] = {	(byte)0xcc,(byte)0xd1,(byte)0x73,(byte)0xff,	(byte)0xab,(byte)0x20,(byte)0x39,(byte)0xf4,	(byte)0xac,(byte)0xd8,(byte)0xae,(byte)0xfd,	(byte)0xdf,(byte)0xd8,(byte)0xa1,(byte)0xeb,	(byte)0x46,(byte)0x8e,(byte)0x91,(byte)0x15,	(byte)0x78,(byte)0x88,(byte)0xba,(byte)0x68,	(byte)0x1d,(byte)0x26,(byte)0x93,(byte)0x97,	(byte)0xf7,(byte)0xfe,(byte)0x62,(byte)0xb4};  public static byte cbc3_ok[] = {	(byte)0x3F,(byte)0xE3,(byte)0x01,(byte)0xC9,	(byte)0x62,(byte)0xAC,(byte)0x01,(byte)0xD0,	(byte)0x22,(byte)0x13,(byte)0x76,(byte)0x3C,	(byte)0x1C,(byte)0xBD,(byte)0x4C,(byte)0xDC,	(byte)0x79,(byte)0x96,(byte)0x57,(byte)0xC0,	(byte)0x64,(byte)0xEC,(byte)0xF5,(byte)0xD4,	(byte)0x1C,(byte)0x67,(byte)0x38,(byte)0x12,	(byte)0xCF,(byte)0xDE,(byte)0x96,(byte)0x75};  public static byte pcbc_ok[] = {	(byte)0xcc,(byte)0xd1,(byte)0x73,(byte)0xff,	(byte)0xab,(byte)0x20,(byte)0x39,(byte)0xf4,	(byte)0x6d,(byte)0xec,(byte)0xb4,(byte)0x70,	(byte)0xa0,(byte)0xe5,(byte)0x6b,(byte)0x15,	(byte)0xae,(byte)0xa6,(byte)0xbf,(byte)0x61,	(byte)0xed,(byte)0x7d,(byte)0x9c,(byte)0x9f,	(byte)0xf7,(byte)0x17,(byte)0x46,(byte)0x3b,	(byte)0x8a,(byte)0xb3,(byte)0xcc,(byte)0x88};  public static byte cfb_key[] = {    (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,    (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef};  public static byte cfb_iv[] = {    (byte)0x12,(byte)0x34,(byte)0x56,(byte)0x78,    (byte)0x90,(byte)0xab,(byte)0xcd,(byte)0xef};  public static byte cfb_buf1[] = new byte [40];  public static byte cfb_buf2[] = new byte [40];  public static byte cfb_tmp[] = new byte [8];  public static byte plain[] = {	(byte)0x4e,(byte)0x6f,(byte)0x77,(byte)0x20,	(byte)0x69,(byte)0x73,(byte)0x20,(byte)0x74,	(byte)0x68,(byte)0x65,(byte)0x20,(byte)0x74,	(byte)0x69,(byte)0x6d,(byte)0x65,(byte)0x20,	(byte)0x66,(byte)0x6f,(byte)0x72,(byte)0x20,	(byte)0x61,(byte)0x6c,(byte)0x6c,(byte)0x20	};  public static byte cfb_cipher8[]= {	(byte)0xf3,(byte)0x1f,(byte)0xda,(byte)0x07,	(byte)0x01,(byte)0x14,(byte)0x62,(byte)0xee,	(byte)0x18,(byte)0x7f,(byte)0x43,(byte)0xd8,	(byte)0x0a,(byte)0x7c,(byte)0xd9,(byte)0xb5,	(byte)0xb0,(byte)0xd2,(byte)0x90,(byte)0xda,	(byte)0x6e,(byte)0x5b,(byte)0x9a,(byte)0x87 };  public static byte cfb_cipher16[] = {	(byte)0xF3,(byte)0x09,(byte)0x87,(byte)0x87,	(byte)0x7F,(byte)0x57,(byte)0xF7,(byte)0x3C,	(byte)0x36,(byte)0xB6,(byte)0xDB,(byte)0x70,	(byte)0xD8,(byte)0xD5,(byte)0x34,(byte)0x19,	(byte)0xD3,(byte)0x86,(byte)0xB2,(byte)0x23,	(byte)0xB7,(byte)0xB2,(byte)0xAD,(byte)0x1B };  public static byte cfb_cipher32[] = {	(byte)0xF3,(byte)0x09,(byte)0x62,(byte)0x49,	(byte)0xA4,(byte)0xDF,(byte)0xA4,(byte)0x9F,	(byte)0x33,(byte)0xDC,(byte)0x7B,(byte)0xAD,	(byte)0x4C,(byte)0xC8,(byte)0x9F,(byte)0x64,	(byte)0xE4,(byte)0x53,(byte)0xE5,(byte)0xEC,	(byte)0x67,(byte)0x20,(byte)0xDA,(byte)0xB6 };  public static byte cfb_cipher48[] = {	(byte)0xF3,(byte)0x09,(byte)0x62,(byte)0x49,	(byte)0xC7,(byte)0xF4,(byte)0x30,(byte)0xB5,	(byte)0x15,(byte)0xEC,(byte)0xBB,(byte)0x85,	(byte)0x97,(byte)0x5A,(byte)0x13,(byte)0x8C,	(byte)0x68,(byte)0x60,(byte)0xE2,(byte)0x38,	(byte)0x34,(byte)0x3C,(byte)0xDC,(byte)0x1F };  public static byte cfb_cipher64[] = {	(byte)0xF3,(byte)0x09,(byte)0x62,(byte)0x49,	(byte)0xC7,(byte)0xF4,(byte)0x6E,(byte)0x51,	(byte)0xA6,(byte)0x9E,(byte)0x83,(byte)0x9B,	(byte)0x1A,(byte)0x92,(byte)0xF7,(byte)0x84,	(byte)0x03,(byte)0x46,(byte)0x71,(byte)0x33,	(byte)0x89,(byte)0x8E,(byte)0xA6,(byte)0x22 };  public static byte ofb_key[] = {    (byte)0x01,(byte)0x23,(byte)0x45,(byte)0x67,    (byte)0x89,(byte)0xab,(byte)0xcd,(byte)0xef};  public static byte ofb_iv[] = {    (byte)0x12,(byte)0x34,(byte)0x56,(byte)0x78,    (byte)0x90,(byte)0xab,(byte)0xcd,(byte)0xef};  public static byte ofb_buf1[] = new byte[24];  public static byte ofb_buf2[] = new byte[24];  public static byte ofb_tmp[] = new byte[8];  public static byte ofb_cipher[] = {	(byte)0xf3,(byte)0x09,(byte)0x62,(byte)0x49,	(byte)0xc7,(byte)0xf4,(byte)0x6e,(byte)0x51,	(byte)0x35,(byte)0xf2,(byte)0x4a,(byte)0x24,	(byte)0x2e,(byte)0xeb,(byte)0x3d,(byte)0x3f,	(byte)0x3d,(byte)0x6d,(byte)0x5b,(byte)0xe3,	(byte)0x25,(byte)0x5a,(byte)0xf8,(byte)0xc3	};  static int cbc_cksum_ret = 0xB462FEF7;  public static byte cbc_cksum_data[] = {    (byte)0x1D,(byte)0x26,(byte)0x93,(byte)0x97,    (byte)0xf7,(byte)0xfe,(byte)0x62,(byte)0xb4};  public static void memcpy(byte [] out, byte [] in, int len) {    System.arraycopy( in, 0, out, 0, len);  }  public static void memset(byte [] out, byte c, int len) {    int i;    for(i = 0; i < len; i++) {      out[i] = c;    }  }  public static boolean memcmp(byte [] out, byte [] in, int len) {    int i;    for(i = 0; i < len; i++) {      if(out[i] != in[i]) {	return true;      }    }    return false;  }  public static void printhex(byte [] array) {    if((array.length % 8) != 0) {      System.out.print("array not multiple of 8 bytes\n");      return;    }    int i;    for( i = 0; i < array.length / 8; i++) {      int j;      for( j = 0; j < 8; j++) {        byte hival = (byte)((array[(i*8) + j]&0xf0) >>> 4);        byte loval = (byte)(array[(i*8) + j] & 0xf);        char ch;        System.out.print("0x");        if(hival < 10) {          ch = (char)((byte)'0' + hival);        } else {          ch = (char)((byte)'A' + hival - 10);        }        System.out.print(ch);        if(loval < 10) {          ch = (char)((byte)'0' + loval);        } else {          ch = (char)((byte)'A' + loval - 10);        }        System.out.print(ch);        System.out.print(" ");      }      System.out.println();    }  }    public static boolean cfb_test(int bits, byte [] cfb_cipher) {    boolean err = false;    Des des = new Des(new DesKey(cfb_key, false));    memcpy(cfb_tmp, cfb_iv, cfb_iv.length);    des.cfb_encrypt(plain, 0, plain.length, 		    cfb_buf1, 0,		    bits,		    cfb_tmp,		    Des.ENCRYPT);    if (memcmp(cfb_cipher,cfb_buf1, plain.length) != false) {      err = true;      System.out.println("cfb_encrypt encrypt error");      printhex(cfb_buf1);    }    memcpy(cfb_tmp, cfb_iv, cfb_iv.length);    des.cfb_encrypt(cfb_buf1, 0, plain.length, 		    cfb_buf2, 0, 		    bits,		    cfb_tmp,		    Des.DECRYPT);    if (memcmp(plain, cfb_buf2, plain.length) != false) {	err = true;	System.out.println("cfb_encrypt decrypt error");		System.out.println("plain buf = ");	printhex(plain);	System.out.println("cfb_buf2 = ");	printhex(cfb_buf2);      }    return(err);  }  public static boolean cfb64_test(byte [] cfb_cipher) {    Des des = new Des(new DesKey(cfb_key, false));    boolean err = false;    int [] n = new int [ 1 ];        memcpy(cfb_tmp,cfb_iv, cfb_iv.length);    n[0] = 0;    des.cfb64_encrypt(plain, 0, 12, 		      cfb_buf1, 0,		      cfb_tmp,		      n,		      Des.ENCRYPT);    des.cfb64_encrypt(plain, 12, plain.length - 12,		      cfb_buf1, 12,		      cfb_tmp,		      n,		      Des.ENCRYPT);    if (memcmp(cfb_cipher,cfb_buf1, plain.length) != false) {      err = true;      System.out.println("cfb_encrypt encrypt error");      printhex(cfb_cipher);      printhex(cfb_buf1);      return err;    }    memcpy(cfb_tmp, cfb_iv, cfb_iv.length);    n[0] = 0;    des.cfb64_encrypt(cfb_buf1, 0, 17,		      cfb_buf2, 0,		      cfb_tmp,		      n,		      Des.DECRYPT);    des.cfb64_encrypt(cfb_buf1, 17, plain.length - 17,		      cfb_buf2, 17,		      cfb_tmp,		      n,		      Des.DECRYPT);    if (memcmp(plain,cfb_buf2,plain.length) != false) {      err = true;      System.out.println("cfb_encrypt decrypt error");      printhex(plain);      printhex(cfb_buf2);    }    return(err);  }  public static boolean ede_cfb64_test(byte [] cfb_cipher) {    DesKey ks = new DesKey(cfb_key, false);    TripleDes des3 = new TripleDes(ks, ks, ks);    boolean err = false;    int i;    int [] ref_to_n = new int [1];        memcpy(cfb_tmp, cfb_iv, cfb_iv.length);    ref_to_n[0] = 0;    des3.cfb64_encrypt(plain,0, 12,		       cfb_buf1, 0,		       cfb_tmp,		       ref_to_n,Des.ENCRYPT);    des3.cfb64_encrypt(plain, 12, plain.length - 12,		       cfb_buf1, 12,		       cfb_tmp,		       ref_to_n,Des.ENCRYPT);    if (memcmp(cfb_cipher,cfb_buf1, plain.length) != false) {	err= true;	System.out.println("ede_cfb_encrypt encrypt error");	printhex(cfb_buf1);    }    memcpy(cfb_tmp, cfb_iv, cfb_iv.length);    ref_to_n[0] = 0;    des3.cfb64_encrypt(cfb_buf1, 0, 17,		       cfb_buf2, 0,		       cfb_tmp,		       ref_to_n,Des.DECRYPT);    des3.cfb64_encrypt(cfb_buf1, 17, plain.length - 17,		       cfb_buf2, 17,		       cfb_tmp,		       ref_to_n,Des.DECRYPT);    if (memcmp(plain,cfb_buf2, plain.length) != false) {	err = true;	System.out.println("ede_cfb_encrypt decrypt error");	printhex(cfb_buf2);    }    return(err);  }  public static final int NUM_TESTS = 34;  public static void main(String[] args) {    // Setup cbc_data    memset(cbc_data, (byte)0, 40);    cbc_string.getBytes(0, cbc_string.length(), cbc_data, 0);    int i;    int err;    byte [] in = new byte [8];    byte [] out = new byte [8];    byte [] outin = new byte [8];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -