加密解密,脑袋都搞大了,请大家帮忙看看!(200分)

  • 主题发起人 主题发起人 yuanzi
  • 开始时间 开始时间
多人接受答案了。
 
对不起,我说过要帮你看一下的,一直忙,刚才给你试了一下。
你用的是JCE.在JCE文档之中,其实已经包含了使用JCE的步骤。
JCE使用的是可插入式的构造,就是说,JCE的具体实现是依靠provider来实现的。JCE已经被
整合进入JDK 1.4的下载包了。而jdk 1.2和jdk 1.3还是需要你手工配置。
我们来看看文档中是怎么写的:

There are two types of JCE installations supported:
The JCE 1.2.1 framework is an "installed" extension. JCE providers are installed or bundled.
The JCE 1.2.1 framework and JCE providers are located on the class path (not installed extensions).
Thus, there are a couple possible ways of installing provider classes, depending on how the JCE 1.2.1 framework is installed:
Install the JAR file containing the provider classes as an "installed" or "bundled" (also known as "download") extension if the JCE 1.2.1 framework is an installed extension.
Place the JAR file containing the provider classes anywhere on the CLASSPATH if the JCE 1.2.1 framework is on the CLASSPATH.
Note: A provider JAR file will be considered an installed extension if it is placed in the standard place for the JAR files of an installed extension:
<java-home>/lib/ext [Win32]
<java-home>/lib/ext [Solaris]

Here <java-home> refers to the directory where the runtime software is installed, which is the top-level directory of the JRE or the jre directory in the JavaTM 2 SDK software. For example, if you have J2SDK v 1.2.2 installed on Solaris in a directory named jdk1.2.2, you need to install the JAR file in the following directory:
jdk1.2.2/jre/lib/ext
Similarly, if you have JavaTM 2 Runtime Environment v 1.2.2 installed on Solaris in a directory named jre1.2.2, you need to install the JAR file in the following directory:
jre1.2.2/lib/ext
If a provider JAR file is placed anywhere other than the standard place for installed extensions (e.g., in a directory on your class path instead), it will be considered "bundled."
For more information on "installed" extensions, see Installed Extensions.

For more information on "bundled" extensions, see Bundled Extensions.
Configuring the Provider
The next step is to add the provider to your list of approved providers. This isdo
ne statically by editing the security properties file
<java-home>/lib/security/java.security [Win32]
<java-home>/lib/security/java.security [Solaris]
Here <java-home> refers to the directory where the JRE was installed. For example, if you have J2SDK v 1.2.2 installed on Solaris in a directory named jdk1.2.2, you need to edit the following file:
jdk1.2.2/jre/lib/security/java.security
Similarly, if you have JavaTM 2 Runtime Environment, Standard Edition, v 1.2.2 installed on Solaris in a directory named jre1.2.2, you need to edit this file:
jre1.2.2/lib/security/java.security
For each provider, this file should have a statement of the following form:
security.provider.n=masterClassName
This declares a provider, and specifies its preference order n. The preference order is the order in which providers are searched for requested algorithms when no specific provider is requested. The order is 1-based;
1 is the most preferred, followed by 2, and so on.
masterClassName must specify the fully qualified name of the provider's "master class". The provider vendor should supply you this name.
J2SE comes standard with a provider named "SUN", which is automatically configured as a static provider in the java.security properties file, as follows:
security.provider.1=sun.security.provider.Sun
(The "SUN" provider's master class is the Sun class in the sun.security.provider package.)
Note that when you install JCE 1.2.1 (the jce1_2_1.jar extension), the "SunJCE" provider that is also included with the JCE 1.2.1 release is not automatically installed. That is, if you want to use the "SunJCE" provider, you need to install the sunjce_provider.jar file as an optional package (extension) and explicitly register the "SunJCE" provider - either statically or dynamically, as described in this section.
In order to statically add "SunJCE" to your list of providers, you need to edit the security properties file to contain a line of the format shown above. For example, to make "SunJCE" the second preferred provider, add the following line to the java.security file below the line for the "SUN" provider:
security.provider.2=com.sun.crypto.provider.SunJCE
行了,看这么多就可以了。
按照他的说法,只需要把这两个包放到classpath中,并且修改java.security文件就可以了。
Visual agae for Java的内置JRE的配置文件位于IBM Visual age for Java/ide/lib
里面,你可以找到java.security文件。
按照文档所写,我们加上如下一行:
security.provider.2=com.sun.crypto.provider.SunJCE
然后保存。
为了能让你的程序编译正确,我们把jce1_2_1.jar import到一个新的project JCE中去,
而把你的第一段程序放到一个JCETest的工程里去。在window->Options里面找到Resource ->
workbench class path,把jce1_2_1.jar和sunjce_provider.jar放倒classpath里面。然后执行
En1.令人失望的是,错误依然。
无可怀疑,JCE一定是正确的,那什么错误了呢?注意到文档中特别提到:
Note: A provider JAR file will be considered an installed extension if it is placed in the standard place for the JAR files of an installed extension:
<java-home>/lib/ext [Win32]
<java-home>/lib/ext [Solaris]
你看,这两个JAR文件需要放到lib/ext目录下。而IBM的VAJ的ide/lib目录下并没有ext目录。
下面的事情就好办了。按照文档所说,我们在VAJ的ide/lib下新建ext目录,然后把
JCE的下载包中的lib目录下的4个jar文件copy到这个ide/lib/ext目录,最后把这4个文件
加入workbench classpath里面去。
由于所有的包都已经包含在workbench classpath中,在En1本身的classptah就不要再包含JCE工程或者其他jar文件了。
好,这次再运行En1,哈!成功了。
这就是运行结果:
[B@13c3

This is just an example:abd

Encrypt the cleartext
z?Bb0`j???7_?A?f?~.?T?a&amp;??B??
[B@2624
[B@2a5e

Decrypt the ciphertext
This is just an example:abd
注意,我用的是英文的windows系统和英文VAJ,所以你看到的应该有所不同。
你所说在JDK1.4中运行正常,是因为JDK1.4已经内置JCE,所以你如果去看他的java.security文件,应该已经包含了
security.provider.2=com.sun.crypto.provider.SunJCE
这一句。
总结一下,其实很多问题看起来很困难,但是在文档中已经全部提到了。如果yuanzi同志能够
在遇到问题的时候仔仔细细看看文档,这个问题早就迎刃而解了。
 
后退
顶部