算法如下,是用c++写的,现在我java程序中要用到,哪位大侠能帮忙翻译一下,谢谢了char *EncryptPwd(p_sPasswd)char *p_sPasswd;/*char *p_sPasswd[C_PASSWD_LEN];*/{         char c;         int i;         int iLength;         const int iMove = 5; /*后移位数*/         char cBegin = ' '; /* ASCII 32*/         char cEnd = '~';  /*ASCII 126*/         char p_sPasswdtemp[81];         char *sReturn ; /*字符转换为16进制*/         char p_sChar0x[3]; /*字符的16进制*/         char p_sRand[5]; /*4位随机数*/         const int iBase = 127 ; /* 01111111 用于异或转换*/          sReturn=(char*)malloc(200);         memset(sReturn,0,200);         iLength = strlen(p_sPasswd);                  printf("%d\n",iLength);                printf("%s\n",p_sPasswd);                  /* 保存16进制*/         /*strcpy(sReturn , "");*/         for (i=0; i<iLength; i++)         {                   c = p_sPasswd[i];                   c = c ^ iBase;                   sprintf(p_sChar0x, "%02x", c);                   strcat(sReturn, p_sChar0x );         }                                   printf("%s\n",sReturn);                        /* 后移 iMove 5 位*/                     strcpy(p_sPasswdtemp , sReturn);         iLength = strlen(p_sPasswdtemp);                    printf("%d\n",iLength);         printf("%s\n",p_sPasswdtemp);                  if(iLength>0)         {                    strncpy(sReturn,p_sPasswdtemp+iMove,iLength-iMove);                   strncpy(sReturn+iLength-iMove,p_sPasswdtemp,iMove);                   printf("%s\n",sReturn);                                            /* 奇偶互换                   for (i=0; i<(iLength/2); i++)                   {                            c = sReturn[2*i];                            sReturn[2*i]=sReturn[2*i+1];                            sReturn[2*i+1]=c;                   }                           printf("%s\n",sReturn);                         */                   /* 加入随机数*/                   srand( (unsigned)time( NULL ) );                   sprintf(p_sRand, "%04x", rand());                   strcpy(p_sPasswdtemp , sReturn);                   iLength = strlen(p_sPasswdtemp);                   printf("%d\n",iLength);                   printf("%s\n",p_sPasswdtemp);                            strncpy(sReturn, p_sRand,4 );                   strncpy(sReturn+4, p_sPasswdtemp,3 );                   sprintf(p_sRand, "%04x", rand());                   strncpy(sReturn+7, p_sRand,4 );                   strncpy(sReturn+11, p_sPasswdtemp+3,iLength-3 );                   sprintf(p_sRand, "%04x", rand());                   p_sRand[4]=0;                   strncpy(sReturn+8+iLength, p_sRand,5 ); /*含'\0'*/                                      printf("%s\n",sReturn);               }                  iLength = strlen(sReturn);         printf("%d\n",iLength);                  return sReturn;}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【beipanjiyi】截止到2008-07-09 16:04:33的历史汇总数据(不包括此帖):
    发帖的总数量:3                        发帖的总分数:300                      每贴平均分数:100                      
    回帖的总数量:0                        得分贴总数量:0                        回帖的得分率:0%                       
    结贴的总数量:2                        结贴的总分数:200                      
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:1                        未结的总分数:100                      
    结贴的百分比:66.67 %               结分的百分比:66.67 %                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    楼主加油
      

  2.   

    lz的意思是给代码注释一遍?或是要写成dll供java调用?还是用java重写此方法?
      

  3.   

    最好是用java重写此方法,这是一个纯java的web项目
      

  4.   

    } private final byte hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
    '9', 'A', 'B', 'C', 'D', 'E', 'F' }; public String EncryptPwd(byte[] p_sPasswd) {
    byte c;
    int i;
    int iLength;
    final int iMove = 5; /* 后移位数 */
    byte p_sPasswdtemp[] = new byte[81];
    ByteBuffer sReturn; /* 字符转换为16进制 */
    byte p_sRand[] = new byte[5]; /* 4位随机数 */
    final byte iBase = 127; /* 01111111 用于异或转换 */
    sReturn = ByteBuffer.allocate(200);// sReturn=(char*)malloc(200);//分配内存单元
    // memset(sReturn,0,200); //内存单元的初始化,都置为0
    iLength = p_sPasswd.length;
    System.out.println(iLength);// printf("%d\n",iLength);
    System.out.println(p_sPasswd);// printf("%s\n",p_sPasswd);
    /* 保存16进制 */ /* strcpy(sReturn , ""); */
    for (i = 0; i < iLength; i++) {
    c = p_sPasswd[i];
    c = (byte) (c ^ iBase); // sprintf(p_sChar0x, "%02x", c);
    // strcat(sReturn, p_sChar0x );
    byte h = hex[(c & 0xF0) >> 4];
    sReturn.put(h);
    System.out.print(h);
    h = hex[(c & 0x0F)];
    sReturn.put(h);
    System.out.print(h);
    } System.out.println(sReturn);// printf("%s\n",sReturn); /* 后移 iMove 5 位 */ System.arraycopy(sReturn.array(), 0, p_sPasswdtemp, 0, sReturn
    .arrayOffset());// strcpy(p_sPasswdtemp , sReturn);
    iLength = sReturn.arrayOffset(); System.out.println(iLength);// printf("%d\n",iLength); System.out.println(p_sPasswdtemp);// printf("%s\n",p_sPasswdtemp); if (iLength > 0) {
    System.arraycopy(p_sPasswdtemp, iMove, sReturn.array(), 0, iLength
    - iMove);// strncpy(sReturn,p_sPasswdtemp+iMove,iLength-iMove);
    System.arraycopy(sReturn.array(), iLength - iMove, p_sPasswdtemp,
    0, iMove);// strncpy(sReturn+iLength-iMove,p_sPasswdtemp,iMove); System.out.println(new String(sReturn.array()));// printf("%s\n",sReturn); /*
     * 奇偶互换
     * 
     * for (i=0; i <(iLength/2); i++) {
     * 
     * c = sReturn[2*i];
     * 
     * sReturn[2*i]=sReturn[2*i+1];
     * 
     * sReturn[2*i+1]=c; }
     * 
     * printf("%s\n",sReturn);
     * 
     */ /* 加入随机数 */ Random rand = new Random(System.currentTimeMillis());// srand(
    // (unsigned)time(
    // NULL ) ); // sprintf(p_sRand, "%04x", rand());
    byte r[] = new byte[4];
    rand.nextBytes(r);
    for (int t = 0; t < r.length; t++) {
    p_sRand[t] = hex[r[i]];
    } System.arraycopy(sReturn.array(), 0, p_sPasswdtemp, 0, sReturn
    .arrayOffset());// strcpy(p_sPasswdtemp , sReturn); iLength = sReturn.arrayOffset(); System.out.println(iLength);// printf("%d\n",iLength); System.out.println(p_sPasswdtemp);// printf("%s\n",p_sPasswdtemp); System.arraycopy(p_sRand, 0, sReturn.array(), 0, 4);// strncpy(sReturn,p_sRand,4 ); System.arraycopy(p_sPasswdtemp, 0, sReturn.array(), 4, 3);// strncpy(sReturn+4, p_sPasswdtemp,3); // sprintf(p_sRand, "%04x", rand());
    rand.nextBytes(r);
    for (int t = 0; t < r.length; t++) {
    p_sRand[t] = hex[r[i]];
    } System.arraycopy(p_sRand, 0, sReturn.array(), 7, 4);// strncpy(sReturn+7,
    // p_sRand,4 ); System
    .arraycopy(p_sPasswdtemp, 3, sReturn.array(), 11,
    iLength - 3);// strncpy(sReturn+11,
    // p_sPasswdtemp+3,iLength-3 ); // sprintf(p_sRand, "%04x", rand());
    rand.nextBytes(r);
    for (int t = 0; t < r.length; t++) {
    p_sRand[t] = hex[r[i]];
    } p_sRand[4] = 0; System.arraycopy(p_sRand, 0, sReturn.array(), 8 + iLength, 5);// strncpy(sReturn+8+iLength,p_sRand,5);
    // /*
    // 含'\0'
    // */ System.out.println(new String(sReturn.array()));// printf("%s\n",sReturn);
    } String rStr = new String(sReturn.array());
    iLength = rStr.length(); System.out.println(iLength);// printf("%d\n",iLength); return rStr; }
      

  5.   

    5楼的写的很详细,只是我的进制学的差一点,没怎么看懂,不过还是很感谢,下面是我的代码:public class PswUtil {

    public static String EncryptPwd(String sPasswd){
        final int iBase = 127 ; /* 01111111 用于异或转换*/ 
        final int iMove = 5; /*后移位数*/ 
        int i;
        char c;     String p_sRand = new String(); /*4位随机数*/ 
        String sReturn = new String(); /*字符转换为16进制*/ 
        String p_sPasswdtemp = new String();    
        /*保存16进制*/
         for(i = 0; i < sPasswd.length(); i++) 
        {
          String tmp0x = new String();
          c = sPasswd.charAt(i);
          c = (char)((int)c ^ iBase); 
          tmp0x = Integer.toHexString((int)c);
          if(tmp0x.length()==1) tmp0x="0"+tmp0x;
          sReturn = sReturn + tmp0x;
        }                
       
        /* 后移 iMove 5 位*/  
        p_sPasswdtemp = sReturn;  
        
        if(p_sPasswdtemp.length()>0) 
        {
          sReturn = p_sPasswdtemp.substring(iMove, p_sPasswdtemp.length());
          sReturn = sReturn + p_sPasswdtemp.substring(0, iMove);

                

          /* 加入随机数*/
          /* C语言在取随机数时需要先设置随机种子
              time(NULL)返回的是1970.1.1到当前时间的秒数
              srand((unsigned)time(NULL));
           */
          p_sRand = Integer.toHexString((int)(Math.random()*10000));/*我在这里暂时取4位随机数*/
          if(p_sRand.length()!=4){
           int length = 4-p_sRand.length();
           for(int m=0;m<length;m++){
           p_sRand="0"+p_sRand;
           }
          }
          
           p_sPasswdtemp = sReturn;       

          char tmpReturn[];
          tmpReturn = new char[4];

          for(i = 0; i < 4; i++){
           tmpReturn[i] = p_sRand.charAt(i);
          }

          String tmp_sReturn1 = new String(tmpReturn);

          sReturn = tmp_sReturn1;
          sReturn = sReturn + p_sPasswdtemp.substring(0, 3);
          p_sRand = Integer.toHexString((int)(Math.random()*10000));//我在这里暂时取4位随机数
          if(p_sRand.length()!=4){
           int length = 4-p_sRand.length();
           for(int m=0;m<length;m++){
           p_sRand="0"+p_sRand;
           }
                 }
          
           for(i = 0; i < 4; i++){
            tmpReturn[i] = p_sRand.charAt(i);
           }

          String tmp_sReturn2 = new String(tmpReturn);

          sReturn = sReturn + tmp_sReturn2;
          sReturn = sReturn + p_sPasswdtemp.substring(3,p_sPasswdtemp.length());
          p_sRand = Integer.toHexString((int)(Math.random()*10000));//我在这里暂时取4位随机数
          
          if(p_sRand.length()!=4){
           int length = 4-p_sRand.length();
           for(int m=0;m<length;m++){
           p_sRand="0"+p_sRand;
           }
                  }

           for(i = 0; i < 4; i++){
            tmpReturn[i] = p_sRand.charAt(i);
           }

          String tmp_sReturn3 = new String(tmpReturn);
          sReturn = sReturn + tmp_sReturn3;
          
        }    
        
        System.out.println("sReturn:"+sReturn);     return sReturn;
      }

      /**
       * @param args
       */
      public static void main(String[] args) {
        String sNewPwd = new String();
        sNewPwd = EncryptPwd("222222");
      }