<div style="font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);">Hi Pycryptors,</div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);">I got some requests from my clients, his requests are xml formatted encrypted by AES with Java.</div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);">I decide to use pycrypto to decrypt these xml requests. But i am not familiar with Java.</div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);">Someone can help? Thanks a lot. Java codes as follows,</div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both;"><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">Encrypt.java</span></font></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);">============</div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">import java.io.UnsupportedEncodingException;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">import java.security.SecureRandom;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">import javax.crypto.Cipher;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">import javax.crypto.KeyGenerator;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">import javax.crypto.SecretKey;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">import javax.crypto.spec.SecretKeySpec;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">public class Encrypt {</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;"><span class="Apple-tab-span" style="white-space:pre">   </span>public password = "123456";</span></font></div><div style="clear: both;"><span class="Apple-tab-span" style="white-space: pre; line-height: 19px;"><font face="宋体" size="2">   </font></span></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    public Encrypt(String password) {</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;"><span class="Apple-tab-span" style="white-space:pre">          </span>super();</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;"><span class="Apple-tab-span" style="white-space:pre">            </span>this.password = password;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;"><span class="Apple-tab-span" style="white-space:pre">   </span>}</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;"><span class="Apple-tab-span" style="white-space:pre">   </span>/** </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * 加密 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     *  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * @param content 需要加密的内容 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * @param password  加密密码 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * @return </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     */  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    public static byte[] encrypt(String content, String password) {  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            try {             </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            <span class="Apple-tab-span" style="white-space:pre">  </span></span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    KeyGenerator kgen = KeyGenerator.getInstance("AES"); //AES密钥生成器</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    //128是密钥的长度  SecureRandom是随机生成数  但是我们需要摄入确定的值</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    kgen.init(128, new SecureRandom(password.getBytes()));//初始化密钥</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    SecretKey secretKey = kgen.generateKey(); //分组秘密密钥(并为其提供类型安全)</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    byte[] enCodeFormat = secretKey.getEncoded();  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); //对产生的密钥再进行封装 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    Cipher cipher = Cipher.getInstance("AES");// 创建密码器   </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    byte[] byteContent = content.getBytes("utf-8");  //获得原文utf-8编码格式的字节数</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    cipher.init(Cipher.ENCRYPT_MODE, key);// 加密初始化   Cipher.ENCRYPT_MODE为加密</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    byte[] result = cipher.doFinal(byteContent);  //密码器执行加密 并生成加密字节数组</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    return result; // 加密   </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            } catch (Exception e) {  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    e.printStackTrace();  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            } </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            return null;  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    }  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    /**解密 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * @param content  待解密内容 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * @param password 解密密钥 </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     * @return </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     */  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    public static String decrypt(byte[] content, String password) {  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            try {  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     KeyGenerator kgen = KeyGenerator.getInstance("AES");  //AES密钥生成器</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     //128是密钥的长度  SecureRandom是随机生成数  但是我们需要摄入确定的值</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     kgen.init(128, new SecureRandom(password.getBytes()));  //初始化密钥</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     SecretKey secretKey = kgen.generateKey();  //分组秘密密钥(并为其提供类型安全)</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     byte[] enCodeFormat = secretKey.getEncoded();  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");  //对产生的密钥再进行封装             </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                     Cipher cipher = Cipher.getInstance("AES");// 创建密码器   </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    cipher.init(Cipher.DECRYPT_MODE, key);// 解密初始化   Cipher.DECRYPT_MODE为解密</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    byte[] result = cipher.doFinal(content);  //密码器执行解密密 并生成解密密字节数组</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    return new String(result); // 解密   </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            } catch (Exception e) {  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">                    e.printStackTrace();  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            } </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">            return null;  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    }  </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    </span></font></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    public static String parseByte2HexStr(byte buf[]){</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre"> </span>StringBuffer sb=new StringBuffer();</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">   </span>for(int i=0;i<buf.length;i++){</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">             </span>String hex=Integer.toHexString(buf[i]&0xFF);</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">              </span>if(hex.length()==1){</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                  </span>hex='0' + hex;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                </span>}</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">             </span>sb.append(hex.toUpperCase());</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">         </span>}</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">     </span>return sb.toString();</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    }</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    </span></font></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><br></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    public static byte[] parseHexStr2Byte(String hexStr){</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre"> </span>if(hexStr.length()<1)</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">              </span>return null;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">  </span>byte[]result=new byte[hexStr.length()/2];</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">     </span>for(int i=0;i<hexStr.length()/2;i++){</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">              </span>int high=Integer.parseInt(hexStr.substring(i*2,i*2+1),16);</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">            </span>int low=Integer.parseInt(hexStr.substring(i*2+1,i*2+2),16);</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">           </span>result[i]=(byte)(high*16+low);</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                </span>}</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">     </span>return result;</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">        </span>}</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">     </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    public static void main(String args[]) throws UnsupportedEncodingException {</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">       </span>String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">    </span>"<request>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">  </span>  "<company>1</company>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">          </span>"<requestId>2</requestId>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">             </span>"<Create>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                   </span>"<CreateHost>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                               </span>"<userId>3</userId>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                           </span>"<transactionId>4</transactionId>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                             </span>"<timestamp>5</timestamp>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                             </span>"<core>6</core>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                               </span>"<memory>7</memory>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                           </span>"<os>4</os>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                           </span>"<groupName>5</groupName>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                             </span>"<hostSpecId>5</hostSpecId>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                           </span>"<path>6</path>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                       </span>"</CreateHost>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                      </span>"<CreateIp>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                         </span>"<transactionId>6</transactionId>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                             </span>"<netSpeed>6</netSpeed>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                               </span>"<ip>7</ip>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                   </span>"</CreateIp >"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                       </span>"<CreateDisk>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                               </span>"<transactionId>7</transactionId>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                             </span>"<disk>7</disk>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">                       </span>"</CreateDisk>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">              </span>"</Create>"+</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre">  </span>"</request>";</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    <span class="Apple-tab-span" style="white-space:pre"> </span>System.out.println("content:"+content);</span></font></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><span style="font-size: small;">    </span><span class="Apple-tab-span" style="font-size: small; white-space: pre;">    </span><span style="font-size: small;">System.out.println("encrypted:"+(parseByte2HexStr(Encrypt.encrypt(content, "123456"))));</span></div><div style="clear: both; font-size: 13px; font-family: 宋体; color: rgb(0, 0, 0);"><span style="font-size: small;">    </span><span class="Apple-tab-span" style="font-size: small; white-space: pre;">      </span><span style="font-size: small;">System.out.println("decrypted:"+Encrypt.decrypt(parseHexStr2Byte(parseByte2HexStr(Encrypt.encrypt(content, "123456"))),"123456"));</span></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    }</span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">    </span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;"><br></span></font></div><div style="clear: both;"><font face="宋体" size="2"><span style="line-height: 19px;">}</span></font></div><div style="color: rgb(0, 0, 0); font-family: 宋体; font-size: 13px;"><br></div></div>