diff options
Diffstat (limited to 'pipermail/pycrypto/2012q4/000644.html')
-rw-r--r-- | pipermail/pycrypto/2012q4/000644.html | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/pipermail/pycrypto/2012q4/000644.html b/pipermail/pycrypto/2012q4/000644.html new file mode 100644 index 0000000..b42f905 --- /dev/null +++ b/pipermail/pycrypto/2012q4/000644.html @@ -0,0 +1,252 @@ +<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +<HTML> + <HEAD> + <TITLE> [pycrypto] Ask help for java2python rewrite for AES decrypto + </TITLE> + <LINK REL="Index" HREF="index.html" > + <LINK REL="made" HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20Ask%20help%20for%20java2python%20rewrite%20for%20AES%20decrypto&In-Reply-To=%3C8287917590106489675%40unknownmsgid%3E"> + <META NAME="robots" CONTENT="index,nofollow"> + <style type="text/css"> + pre { + white-space: pre-wrap; /* css-2.1, curent FF, Opera, Safari */ + } + </style> + <META http-equiv="Content-Type" content="text/html; charset=us-ascii"> + <LINK REL="Previous" HREF="000643.html"> + + </HEAD> + <BODY BGCOLOR="#ffffff"> + <H1>[pycrypto] Ask help for java2python rewrite for AES decrypto</H1> + <B>Joel Edwards</B> + <A HREF="mailto:pycrypto%40lists.dlitz.net?Subject=Re%3A%20%5Bpycrypto%5D%20Ask%20help%20for%20java2python%20rewrite%20for%20AES%20decrypto&In-Reply-To=%3C8287917590106489675%40unknownmsgid%3E" + TITLE="[pycrypto] Ask help for java2python rewrite for AES decrypto">joeledwards at gmail.com + </A><BR> + <I>Fri Dec 28 04:16:42 PST 2012</I> + <P><UL> + <LI>Previous message: <A HREF="000643.html">[pycrypto] Ask help for java2python rewrite for AES decrypto +</A></li> + + <LI> <B>Messages sorted by:</B> + <a href="date.html#644">[ date ]</a> + <a href="thread.html#644">[ thread ]</a> + <a href="subject.html#644">[ subject ]</a> + <a href="author.html#644">[ author ]</a> + </LI> + </UL> + <HR> +<!--beginarticle--> +<PRE>There is a good implementation available on the AES Crypt website. + +Java version overview +<A HREF="http://www.aescrypt.com/java_aes_crypt.html">http://www.aescrypt.com/java_aes_crypt.html</A> + +Download page +<A HREF="https://www.aescrypt.com/download/">https://www.aescrypt.com/download/</A> + +A lot of the code is designed around AES Crypt's file format, but as long +as you evaluate that first, you should be able to get a good idea of how to +set things up correctly. + +Keep in mind that only 128-bit encryption is available with the standard +Java lib, and AES Crypt is written for 256-bit encryption. So you will need +to either adapt their work to a 128-bit key size, or (assuming no +restrictions apply) download the "Java Cryptography Extension (JCE) +Unlimited Strength Jurisdiction Policy Files" from +Oracle<<A HREF="http://www.oracle.com/technetwork/java/javase/downloads/index.html">http://www.oracle.com/technetwork/java/javase/downloads/index.html</A>> +. + +Let me know if you have additional questions. You may contact me directly +if you wish, seeing as this is a pycrypto group. + +Joel (Via Mobile) + +On Dec 28, 2012, at 2:16, "steve.yi" <<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">steve.yi at 139.com</A>> wrote: + + +Hi Pycryptors, + +I got some requests from my clients, his requests are xml formatted +encrypted by AES with Java. + +I decide to use pycrypto to decrypt these xml requests. But i am not +familiar with Java. + +Someone can help? Thanks a lot. Java codes as follows, + + +Encrypt.java +============ + + +import java.io.UnsupportedEncodingException; +import java.security.SecureRandom; + +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; + +public class Encrypt { +public password = "123456"; + public Encrypt(String password) { +super(); +this.password = password; +} +/** + * 加密 + * + * @param content 需要加密的内容 + * @param password 加密密码 + * @return + */ + public static byte[] encrypt(String content, String password) { + try { + + KeyGenerator kgen = KeyGenerator.getInstance("AES"); +//AES密钥生成器 + //128是密钥的长度 SecureRandom是随机生成数 但是我们需要摄入确定的值 + kgen.init(128, new +SecureRandom(password.getBytes()));//初始化密钥 + SecretKey secretKey = kgen.generateKey(); +//分组秘密密钥(并为其提供类型安全) + byte[] enCodeFormat = secretKey.getEncoded(); + SecretKeySpec key = new SecretKeySpec(enCodeFormat, +"AES"); //对产生的密钥再进行封装 + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 + byte[] byteContent = content.getBytes("utf-8"); + //获得原文utf-8编码格式的字节数 + cipher.init(Cipher.ENCRYPT_MODE, key);// 加密初始化 +Cipher.ENCRYPT_MODE为加密 + byte[] result = cipher.doFinal(byteContent); //密码器执行加密 +并生成加密字节数组 + return result; // 加密 + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + /**解密 + * @param content 待解密内容 + * @param password 解密密钥 + * @return + */ + public static String decrypt(byte[] content, String password) { + try { + KeyGenerator kgen = KeyGenerator.getInstance("AES"); + //AES密钥生成器 + //128是密钥的长度 SecureRandom是随机生成数 但是我们需要摄入确定的值 + kgen.init(128, new SecureRandom(password.getBytes())); + //初始化密钥 + SecretKey secretKey = kgen.generateKey(); + //分组秘密密钥(并为其提供类型安全) + byte[] enCodeFormat = secretKey.getEncoded(); + SecretKeySpec key = new SecretKeySpec(enCodeFormat, +"AES"); //对产生的密钥再进行封装 + Cipher cipher = Cipher.getInstance("AES");// 创建密码器 + cipher.init(Cipher.DECRYPT_MODE, key);// 解密初始化 +Cipher.DECRYPT_MODE为解密 + byte[] result = cipher.doFinal(content); //密码器执行解密密 +并生成解密密字节数组 + return new String(result); // 解密 + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + + public static String parseByte2HexStr(byte buf[]){ + StringBuffer sb=new StringBuffer(); + for(int i=0;i<buf.length;i++){ + String hex=Integer.toHexString(buf[i]&0xFF); + if(hex.length()==1){ + hex='0' + hex; + } + sb.append(hex.toUpperCase()); + } + return sb.toString(); + } + + + public static byte[] parseHexStr2Byte(String hexStr){ + if(hexStr.length()<1) + return null; + byte[]result=new byte[hexStr.length()/2]; + for(int i=0;i<hexStr.length()/2;i++){ + int high=Integer.parseInt(hexStr.substring(i*2,i*2+1),16); + int low=Integer.parseInt(hexStr.substring(i*2+1,i*2+2),16); + result[i]=(byte)(high*16+low); + } + return result; + } + + public static void main(String args[]) throws +UnsupportedEncodingException { + String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+ + "<request>"+ + "<company>1</company>"+ + "<requestId>2</requestId>"+ + "<Create>"+ + "<CreateHost>"+ + "<userId>3</userId>"+ + "<transactionId>4</transactionId>"+ + "<timestamp>5</timestamp>"+ + "<core>6</core>"+ + "<memory>7</memory>"+ + "<os>4</os>"+ + "<groupName>5</groupName>"+ + "<hostSpecId>5</hostSpecId>"+ + "<path>6</path>"+ + "</CreateHost>"+ + "<CreateIp>"+ + "<transactionId>6</transactionId>"+ + "<netSpeed>6</netSpeed>"+ + "<ip>7</ip>"+ + "</CreateIp >"+ + "<CreateDisk>"+ + "<transactionId>7</transactionId>"+ + "<disk>7</disk>"+ + "</CreateDisk>"+ + "</Create>"+ + "</request>"; + System.out.println("content:"+content); + System.out.println("encrypted:"+(parseByte2HexStr(Encrypt.encrypt(content, +"123456")))); + System.out.println("decrypted:"+Encrypt.decrypt(parseHexStr2Byte(parseByte2HexStr(Encrypt.encrypt(content, +"123456"))),"123456")); + } + + +} + +_______________________________________________ +pycrypto mailing list +<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">pycrypto at lists.dlitz.net</A> +<A HREF="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto</A> +-------------- next part -------------- +An HTML attachment was scrubbed... +URL: <<A HREF="http://lists.dlitz.net/pipermail/pycrypto/attachments/20121228/941a20fe/attachment-0001.html">http://lists.dlitz.net/pipermail/pycrypto/attachments/20121228/941a20fe/attachment-0001.html</A>> +</PRE> + + + + +<!--endarticle--> + <HR> + <P><UL> + <!--threads--> + <LI>Previous message: <A HREF="000643.html">[pycrypto] Ask help for java2python rewrite for AES decrypto +</A></li> + + <LI> <B>Messages sorted by:</B> + <a href="date.html#644">[ date ]</a> + <a href="thread.html#644">[ thread ]</a> + <a href="subject.html#644">[ subject ]</a> + <a href="author.html#644">[ author ]</a> + </LI> + </UL> + +<hr> +<a href="http://lists.dlitz.net/cgi-bin/mailman/listinfo/pycrypto">More information about the pycrypto +mailing list</a><br> +</body></html> |