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

📄 ideatest.java

📁 MPI for java for Distributed Programming
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************                                                                         **             Java Grande Forum Benchmark Suite - MPJ Version 1.0         **                                                                         **                            produced by                                  **                                                                         **                  Java Grande Benchmarking Project                       **                                                                         **                                at                                       **                                                                         **                Edinburgh Parallel Computing Centre                      **                                                                         * *                email: epcc-javagrande@epcc.ed.ac.uk                     **                                                                         **                  Original version of this code by                       **                 Gabriel Zachmann (zach@igd.fhg.de)                      **                                                                         **      This version copyright (c) The University of Edinburgh, 2001.      **                         All rights reserved.                            **                                                                         ***************************************************************************//*** Class IDEATest** This test performs IDEA encryption then decryption. IDEA stands* for International Data Encryption Algorithm. The test is based* on code presented in Applied Cryptography by Bruce Schnier,* which was based on code developed by Xuejia Lai and James L.* Massey.**/package jgf_mpj_benchmarks.section2.crypt;import java.util.*;import jgf_mpj_benchmarks.jgfutil.*; import mpi.*;class IDEATest{// Declare class data. Byte buffer plain1 holds the original// data for encryption, crypt1 holds the encrypted data, and// plain2 holds the decrypted data, which should match plain1// byte for byte.int array_rows; int p_array_rows;int ref_p_array_rows;int rem_p_array_rows;byte [] plain1 = null;       // Buffer for plaintext data.byte [] crypt1 = null;       // Buffer for encrypted data.byte [] plain2 = null;       // Buffer for decrypted data.byte [] p_plain1 = null;byte [] p_crypt1 = null;byte [] p_plain2 = null;short [] userkey;     // Key for encryption/decryption.int [] Z;             // Encryption subkey (userkey derived).int [] DK;            // Decryption subkey (userkey derived).void Do() throws MPIException{  // temporary array for MPI  int m_length;  MPI.COMM_WORLD.Barrier();  // Start the stopwatch.         if(JGFCryptBench.rank==0) {    JGFInstrumentor.startTimer("Section2:Crypt:Kernel"); 		  }  // broadcast information  if(JGFCryptBench.rank==0) {    for (int i = 0; i < p_array_rows; i++) {     p_plain1[i] = plain1[i];     }    for(int k=1;k<JGFCryptBench.nprocess;k++){     if(k==JGFCryptBench.nprocess-1) {       m_length = rem_p_array_rows;     } else {       m_length = p_array_rows;     }     MPI.COMM_WORLD.Ssend(plain1,(p_array_rows*k),m_length,MPI.BYTE,k,k);    }  } else {    MPI.COMM_WORLD.Recv(p_plain1,0,p_array_rows,MPI.BYTE,0,JGFCryptBench.rank);  }  cipher_idea(p_plain1, p_crypt1, Z);     // Encrypt plain1.  cipher_idea(p_crypt1, p_plain2, DK);    // Decrypt.  MPI.COMM_WORLD.Barrier();  if(JGFCryptBench.rank==0) {    for(int k=0; k<p_array_rows;k++){       plain2[k] = p_plain2[k];    }    for(int k=1;k<JGFCryptBench.nprocess;k++) {      MPI.COMM_WORLD.Recv(plain2,(p_array_rows*k),p_array_rows,MPI.BYTE,k,k);    }  } else {    MPI.COMM_WORLD.Ssend(p_plain2,0,p_array_rows,MPI.BYTE,0,JGFCryptBench.rank);  }  MPI.COMM_WORLD.Barrier();  // Stop the stopwatch.  if(JGFCryptBench.rank==0) {    JGFInstrumentor.stopTimer("Section2:Crypt:Kernel");  }}/** buildTestData** Builds the data used for the test -- each time the test is run.*/void buildTestData(){    // Create three byte arrays that will be used (and reused) for    // encryption/decryption operations.    if(JGFCryptBench.rank==0) {      plain1 = new byte [array_rows];      crypt1 = new byte [array_rows];      plain2 = new byte [array_rows];    }     p_plain1 = new byte [p_array_rows];    p_crypt1 = new byte [p_array_rows];    p_plain2 = new byte [p_array_rows];    Random rndnum = new Random(136506717L);  // Create random number generator.    // Allocate three arrays to hold keys: userkey is the 128-bit key.    // Z is the set of 16-bit encryption subkeys derived from userkey,    // while DK is the set of 16-bit decryption subkeys also derived    // from userkey. NOTE: The 16-bit values are stored here in    // 32-bit int arrays so that the values may be used in calculations    // as if they are unsigned. Each 64-bit block of plaintext goes    // through eight processing rounds involving six of the subkeys    // then a final output transform with four of the keys; (8 * 6)    // + 4 = 52 subkeys.    userkey = new short [8];  // User key has 8 16-bit shorts.    Z = new int [52];         // Encryption subkey (user key derived).    DK = new int [52];        // Decryption subkey (user key derived).    // Generate user key randomly; eight 16-bit values in an array.    for (int i = 0; i < 8; i++)    {        // Again, the random number function returns int. Converting        // to a short type preserves the bit pattern in the lower 16        // bits of the int and discards the rest.      userkey[i] = (short) rndnum.nextInt();    }    // Compute encryption and decryption subkeys.    calcEncryptKey();    calcDecryptKey();    // Fill plain1 with "text."    // do on process 0 for reference     if(JGFCryptBench.rank==0) {          for (int i = 0; i < array_rows; i++)      {        plain1[i] = (byte) i;         // Converting to a byte        // type preserves the bit pattern in the lower 8 bits of the        // int and discards the rest.      }    }}/** calcEncryptKey** Builds the 52 16-bit encryption subkeys Z[] from the user key and* stores in 32-bit int array. The routing corrects an error in the* source code in the Schnier book. Basically, the sense of the 7-* and 9-bit shifts are reversed. It still works reversed, but would* encrypted code would not decrypt with someone else's IDEA code.*/private void calcEncryptKey(){    int j;                       // Utility variable.    for (int i = 0; i < 52; i++) // Zero out the 52-int Z array.        Z[i] = 0;    for (int i = 0; i < 8; i++)  // First 8 subkeys are userkey itself.    {        Z[i] = userkey[i] & 0xffff;     // Convert "unsigned"                                        // short to int.    }    // Each set of 8 subkeys thereafter is derived from left rotating    // the whole 128-bit key 25 bits to left (once between each set of    // eight keys and then before the last four). Instead of actually    // rotating the whole key, this routine just grabs the 16 bits    // that are 25 bits to the right of the corresponding subkey    // eight positions below the current subkey. That 16-bit extent    // straddles two array members, so bits are shifted left in one    // member and right (with zero fill) in the other. For the last    // two subkeys in any group of eight, those 16 bits start to    // wrap around to the first two members of the previous eight.    for (int i = 8; i < 52; i++)    {        j = i % 8;        if (j < 6)        {            Z[i] = ((Z[i -7]>>>9) | (Z[i-6]<<7)) // Shift and combine.                    & 0xFFFF;                    // Just 16 bits.            continue;                            // Next iteration.        }        if (j == 6)    // Wrap to beginning for second chunk.        {            Z[i] = ((Z[i -7]>>>9) | (Z[i-14]<<7))                    & 0xFFFF;            continue;        }         // j == 7 so wrap to beginning for both chunks.        Z[i] = ((Z[i -15]>>>9) | (Z[i-14]<<7))                    & 0xFFFF;    }}/** calcDecryptKey** Builds the 52 16-bit encryption subkeys DK[] from the encryption-* subkeys Z[]. DK[] is a 32-bit int array holding 16-bit values as* unsigned.*/private void calcDecryptKey(){    int j, k;                 // Index counters.    int t1, t2, t3;           // Temps to hold decrypt subkeys.    t1 = inv(Z[0]);           // Multiplicative inverse (mod x10001).    t2 = - Z[1] & 0xffff;     // Additive inverse, 2nd encrypt subkey.    t3 = - Z[2] & 0xffff;     // Additive inverse, 3rd encrypt subkey.    DK[51] = inv(Z[3]);       // Multiplicative inverse (mod x10001).    DK[50] = t3;    DK[49] = t2;    DK[48] = t1;    j = 47;                   // Indices into temp and encrypt arrays.    k = 4;    for (int i = 0; i < 7; i++)    {

⌨️ 快捷键说明

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