你得到的是temp和temp2的地址,当然不相同啦

解决方案 »

  1.   

    加密解密时,如果我将加密的明文转成String,在由String转成byte,就会有时出错有时正确,
    出错javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
    at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
    at javax.crypto.Cipher.doFinal(DashoA6275)
    而byte[] clearByte=c1.doFinal(crypteByte); 里crypteByte替换成直接加密后的
    cipherByte就没出现这个错误 public static void main(String args[])
      {
        try
        {
          String estr="test";
         test tt=new test();
          tt.run();
        }
        catch(Exception e){
          e.printStackTrace();
        }
      }
      public  void run() {
    //添加新安全算法,如果用JCE就要把它添加进去
        Security.addProvider(new com.sun.crypto.provider.SunJCE());
        String Algorithm="DES"; //定义 加密算法,可用 DES,DESede,Blowfish
        String myinfo="你好";
        try {
          //生成密钥
          KeyGenerator keygen = KeyGenerator.getInstance(Algorithm);
          SecretKey deskey = keygen.generateKey();
          System.out.println(deskey);
          //加密      System.out.println("加密前的信息:"+myinfo);
          Cipher c1 = Cipher.getInstance(Algorithm);
          c1.init(Cipher.ENCRYPT_MODE,deskey);
          byte[] cipherByte=c1.doFinal(myinfo.getBytes());
          String estr=new String(cipherByte);
          System.out.println("加密后的串:"+estr);
          //解密
          byte[] crypteByte=estr.getBytes();
          System.out.println("解密后的串:"+new String(crypteByte));
          c1 = Cipher.getInstance(Algorithm);
          c1.init(Cipher.DECRYPT_MODE,deskey);
          byte[] clearByte=c1.doFinal(crypteByte);      System.out.println("解密后的信息:"+(new String(clearByte)));    }
        catch (java.security.NoSuchAlgorithmException e1) {e1.printStackTrace();}
        catch (javax.crypto.NoSuchPaddingException e2) {e2.printStackTrace();}
        catch (java.lang.Exception e3) {e3.printStackTrace();}
      }