AESwithJCE http://www.coderanch.com/how-to/content/AES_v1.html
Using AES with Java Technology
By Rags Srinivas
June 2003
In September 2000, the National Institute of Standards and Technology (NIST) approved the Federal Information Processing Standards (FIPS) 197(pdf 279.5 kb), culminating a multi-year effort to replace the out-of-date Data Encryption Standard (DES). As with DES, the new Advanced Encryption Standard (AES) should continue making inroads into private industry. This article looks at using AES in your Java programs.
What Is AES?
AES is a federal standard for private-key or symmetric cryptography. It supports combinations of key and block sizes of 128, 192, and 256. The algorithm chosen for this standard -- called Rijndael -- was invented by two Belgian cryptographers. As part of the evaluation process, the candidate algorithms (including Rijndael) were implemented in the Java language.
AES and Java Technology
The AES standard has been incorporated into several Java technology offerings. Beginning with the Java 2 SDK, Standard Edition (J2SE) v 1.4.0, the Java Cryptography Extension (JCE) was integrated with the SDK and the JRE. Since then, it has no longer been necessary to install the JCE optional package, since support for strong cryptography is now available as part of J2SE. JCE has a provider architecture that enables different providers to be plugged in under a common framework.
Several providers have supported AES in their own clean-room implementations of JCE, or under the existing framework. In Java 2 Platform, Standard Edition v 1.4.2, which is currently under beta, the Sun Provider, referred to as SunJCE, supports AES. Also, the Java Secure Socket Extension (JSSE) supports AES via JCE.
Finally, the Java Card specification version 2.2_01 supports AES with a 128-bit key size.
In this article, we will look primarily at AES support in J2SE, starting with JCE.
Using AES in JCE
Note: The programs listed in this article work only with Java 2 Platform, Standard Edition (J2SE) 1.4.2 installed.
You use AES like any other cipher. To see how, take a look at this JCE sample program. I've modified it to use AES, instead of Blowfish (another symmetric cipher). (I have changed occurrences of Blowfish to AES.)
|
As you can see, the changes I made to accommodate AES are very minimal -- the JCE framework is very general and extensible.
This sample program demonstrates how to use strong cryptography, which is available by default in most installations. This does mean that support for larger key sizes is normally unavailable. To use AES with 192- and 256-bit key sizes, you need unlimited strength cryptography.
Strong Versus Unlimited Strength Cryptography
Due to import-control restrictions imposed by some countries, the jurisdiction policy files shipped with the Java 2 SDK, v 1.4 only permit strong cryptography to be used. An unlimited strength version of these files (that is, with no restrictions on cryptographic strength) is available for download, however.
After installing the unlimited strength version, to use key sizes of 192 and 256 bits, simply provide the required length of the key. The following line of code illustrates how to set the key size to 256 bits:
kgen.init(256); // 128 and 192 bits also available
The JCE examples given here show how to use AES for different key sizes, but they don't touch upon the more intricate issues -- like key management or key exchange algorithms. In practice, you may be more likely to use a protocol like Secure Socket Layer (SSL), which negotiates session keys using public keys that are subsequently used for bulk encryption.
Using AES in JSSE
Java Secure Socket Extension (JSSE) APIs are based around SSL, and the APIs are responsible for key negotiation and subsequent encryption and decryption. For a sample set of programs that illustrate the use of JSSE, see my earlier articles (listed in the See Also section, below). Since some of these articles are somewhat dated, note that the keyStore and trustStore entries will have to replaced by later versions of the corresponding files.
Some of the cipher suites may not be enabled for optimization of startup time. When SSLSockets are first created, no handshaking is done, so that applications can first set their communication preferences: which cipher suites to use, whether the socket should be in client or server mode, and so on. However, security is always provided by the time that application data is sent over the connection.
The getSupportedCipherSuites() method in SSLSocket returns all the possible suites that can be enabled for the connection. You can then use the setEnabledCipherSuites() to enable a subset of the available cipher suites, or to prioritize them. Use getEnabledCipherSuites() to return a list of all the enabled suites that are enabled by default, or that are set by a previous setEnabledCipherSuites() method call.
You can use the getCipherSuite() method in SSLSession to obtain the actual cipher used for the connection.
The following code illustrates the changes I made to the server program. The program uses regular expressions to prioritize AES encryption algorithms, with 256 bits as key size.
|
Here is the corresponding client code:
|
When running the server and the client, the output on the server side should be similar to the following:
|
This output illustrates that the AES 256-bit algorithms are set to higher precedence than the other available algorithms. The cipher suite TLS_RSA_WITH_AES_256_CBC_SHAname constitutes the key negotiation, bulk encryption algorithms, and the key size, which in this case are RSA, AES and 256, respectively. SHA is used in the HMAC construction for integrity protection. By setting some of the debug output (as illustrated in my earlier article), you can get a peek into the inner workings of the different phases of the SSL algorithm.
Note: For performance reasons, it's probably not a good idea to enable all the supported algorithms, but I've included them here for illustration purposes.
Conclusion
The JCE framework is a very powerful and flexible framework for using different cryptographic algorithms. It's based on a provider architecture that enables the same framework to be used for newer cryptographic algorithms. From a developer perspective, this means a higher level of abstraction, and a common set of APIs for newer and different cryptographic algorithms -- without the need to worry about the inner workings of the algorithm.
Some of the other Java security APIs -- such as JSSE -- are implemented on top of JCE, and supplement it to make the different cryptographic algorithms (ciphers, Message Authentication Codes (MACs), and Key Exchange algorithms) available to in a more developer-friendly manner.
See Also
Java World article on Java Security optional packages (reprinted in java.sun.com)
About the Author
Raghavan "Rags" Srinivas is a Java technology evangelist at Sun Microsystems, Inc. He specializes in Java technology and distributed systems. Srinivas has worked on several technology areas, including internals of VMS, UNIX® software, and NT.
AESwithJCE http://www.coderanch.com/how-to/content/AES_v1.html的更多相关文章
- requests的content与text导致lxml的解析问题
title: requests的content与text导致lxml的解析问题 date: 2015-04-29 22:49:31 categories: 经验 tags: [Python,lxml, ...
- Content Security Policy 入门教程
阮一峰文章:Content Security Policy 入门教程
- android 使用Tabhost 发生could not create tab content because could not find view with id 错误
使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...
- 【解决方案】cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One of '{"http://java.sun.com/xml/ns/javaee":run-as, "http://java.sun.com/xml/ns/javaee":security-role-r
[JAVA错误] cvc-complex-type.2.4.a: Invalid content was found starting with element 'init-param'. One o ...
- 注意 AppResLib.dll.*.mui 的生成操作应该为 Content
为 Windows Phone 8 App 添加本地化的时候,发现修改 AppResLib.dll.*.mui 后不仅没有其变化,还发现修改它导致它失效.通过对比代码发现,问题原因是 AppResLi ...
- android Content Provider介绍
ContentProvider(内容提供者)是Android中的四大组件之一.主要用于对外共享数据,也就是通过ContentProvider把应用中的数据共享给其他应用访问,其他应用可以通过Conte ...
- Jsoup问题---获取http协议请求失败 org.jsoup.UnsupportedMimeTypeException: Unhandled content type. Must be text/*, application/xml, or application/xhtml+xml.
Jsoup问题---获取http协议请求失败 1.问题:用Jsoup在获取一些网站的数据时,起初获取很顺利,但是在访问某浪的数据是Jsoup报错,应该是请求头里面的请求类型(ContextType)不 ...
- css content之counter-reset、content-increment
万万没想到,写了快三年前端,有不会用的css,居然还有完全没听过.见过的css属性,而且还是CSS2的内容! 关于counter-reset.content-increment两个属性的详解可以参看张 ...
- DOM解析XML报错:Content is not allowed in prolog
报错内容为: Content is not allowed in prolog. Nested exception: Content is not allowed in prolog. 网上所述总结来 ...
随机推荐
- ES TIPS
1,Testing Analyzers Especially when you are new to Elasticsearch, it is sometimes difficult to under ...
- Ubuntu14.04搭建Caffe(仅CPU)
一直以来都没有写博客的习惯,后来发现以前做的工作如果不注意及时整理和记录往往丢失的很快.对我而言这是一篇具有重要意义的文章,好的习惯要持之以恒,以后的日子我会常驻博客园!由于本人水平有限,智商略低,欢 ...
- [solr] - Facet - autocomplete
Solr的facet可以用于一般性的前缀式自动完成功能,当然,它的主要功能是用于做统计.分类.区间等. 统计.分类.区间功能,可参考下文: http://hongweiyi.com/2013/03/a ...
- LVS+Keepalived负载均衡配置
简介 lvs一般是和keepalived一起组合使用的,虽然也可以单独使用lvs,但配置比较繁琐,且可用性也没有前者高. lvs和keepalived组合使用后,配置lvs的VIP和负载均衡就都在ke ...
- Yii里文件上传的操作方法(图片修改,在详情上展示,批量上传待续...)
$model->img= UploadedFile::getInstance($model,'img');if ($model->validate()) {//$model->img ...
- Codeforces 719E [斐波那契区间操作][矩阵快速幂][线段树区间更新]
/* 题意:给定一个长度为n的序列a. 两种操作: 1.给定区间l r 加上某个数x. 2.查询区间l r sigma(fib(ai)) fib代表斐波那契数列. 思路: 1.矩阵操作,由矩阵快速幂求 ...
- XE6移动开发环境搭建之IOS篇(1):准备安装材料(有图有真相)
网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 1.选择方案 --- ...
- 神奇的C语言
当然下面列出来的几点都是C的基础用法,只不过是这些用法可能平时不会被注意.所以很多东西第一次看到的时候,可能会觉得很怪异,但是细细想想就能很好的理解,也就能更好的清楚C语言的一些特性.但是在具体的编码 ...
- 用自己的ID在appstore中更新app-黑苹果之路
由于之前套用了别人的镜像,在appstore中更新XCode时总要输别人id的密码,id还不能改.网上有的说要把XCode删掉,然后再用自己的ID更新,找到另外一个方法,更简单: 1.打开引用程序目录 ...
- 十大关系数据库SQL注入工具一览
摘要:众所周知,SQL注入攻击是最为常见的Web应用程序攻击技术.同时SQL注入攻击所带来的安全破坏也是不可弥补的.以下罗列的10款SQL工具可帮助管理员及时检测存在的漏洞. BSQL Hacker ...