用Java加密,解密数据电源

时间:2016-03-21 11:06:39

标签: java encryption cryptography

我目前正在开发一个解决方案,我用Java加密密码(在Jboss服务器中运行),然后发送到Data Power并在DataPower中解密。我能够用Java加密密码。我对DataPower不太了解。我已经获得了在datapower中解密的代码,但不知道如何将密钥发送到datapower。任何人都可以帮助我。代码如下, 加密代码(Java) 包装测试;

import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.util.ArrayList;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;



public class Test {


  private static final String ALGO = "AES";

  public static String encrypt(String data,String secretKey) throws Exception {
    Key key = generateKey(secretKey);
    byte[] ivAES = {(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22,(byte)0x22};
    IvParameterSpec ivspec = new IvParameterSpec(ivAES);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key, ivspec);
    byte[] encVal = cipher.doFinal(data.getBytes());
    byte[] finalByte=copyBytes(encVal, ivAES);
    String encryptedValue = new BASE64Encoder().encode(finalByte);
    return encryptedValue;
  }

  public static String decrypt(String encryptedData,String secretKey) throws Exception {
    Key key = generateKey(secretKey);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
    ArrayList<byte[]> al = retreiveIv(decordedValue);
    byte[] data = al.get(0);
    byte[] iv = al.get(1);
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));

    byte[] decValue = cipher.doFinal(data);
    String decryptedValue = new String(decValue);
    return decryptedValue;
  }

  private static Key generateKey(String secretKey) throws Exception {
    Key key = new SecretKeySpec(secretKey.getBytes(), ALGO);
    return key;
  }
  private static byte[] copyBytes(byte[] encVal, byte[] iv) throws Exception {
       ByteArrayOutputStream os = new ByteArrayOutputStream();
       os.write(iv);
       os.write(encVal);
       return os.toByteArray();
    }
  private static ArrayList<byte[]> retreiveIv(byte[] combineByte) {
       ByteArrayOutputStream iv = new ByteArrayOutputStream(16);
       ByteArrayOutputStream data = new ByteArrayOutputStream();

       data.write(combineByte, combineByte.length - 16, 16);
       iv.write(combineByte, 0, combineByte.length - 16);

       ArrayList<byte[]> al = new ArrayList<byte[]>();
       al.add(data.toByteArray());
       al.add(iv.toByteArray());

       return al;
    }

  public static void main(String[] args) throws Exception{


      System.out.println(AESUtil.decrypt(AESUtil.encrypt("Hello", "AVH4TU8AC99dhL2l"), "AVH4TU8AC99dhL2l"));

}


}

解密(DataPower)

<xsl:variable name="algorithm">http://www.w3.org/2001/04/xmlenc#aes128-cbc</xsl:variable>

<xsl:variable name="decryptOut"><xsl:value-of select="dp:decrypt-data($algorithm,'name:danadp',$valueToDecrypt)"/></xsl:variable>     
Decrypted Value: <xsl:copy-of select="$decryptOut"/>

但是,我没有得到如何将我们在Java代码中生成的KeyPower的Key对象共享以进行加密。

如果有人能帮助我的话会很棒。

1 个答案:

答案 0 :(得分:0)

创建一个名为&#39; danadp&#39;的共享密钥对象。并将密钥复制到文件中并导入到运行此代码的DataPower域中的cert:/文件夹中。

P.S记住在密钥前加上&#34; 0x&#34;如果密钥是十六进制。