JAVA上加密算法的实现用例
http://www-900.ibm.com/developerWorks/java/l-security/index.shtml
http://www-900.ibm.com/developerWorks/java/l-security/index.shtml
解决方案 »
- 银行存取款,查询小程序 存在一个TXT中 多次操作txt会乱啊
- 新手求解 新手问题
- ant自动打包脚本执行到编译环节停止的问题
- 遇到一种奇怪的方式,同时extends Thread与implements Runnable,这时怎么处理呢?请教各位老师
- 求一道数据结构算法题的答案?
- 与学Java的大学生们分享一点经验(二)
- java中如何使用外部jar文件
- 一个奇怪的问题:为什么应用程序关闭后还有javaw.exe?
- 一个 java.io.NotSerializableException的问题。
- jar文件能不能编译为.exe文件!!
- 关于用IE5.0浏览applet的问题,请各位高手发表意见。
- 提问:为什么我打包的jar文件无法运行?
* Title:
* Description:
* Copyright: Copyright (c) 2001
* Company:
* @author
* @version 1.0
*/
import java.io.*;
import java.math.BigInteger;
import java.security.*;
import java.security.spec.*;
import java.security.interfaces.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;public class En1 { public En1() { KeyGenerator keygen;
SecretKey desKey;
Cipher desCipher; try{
//DES,DESede,PBEWithMD5AndDES,Blowfish
//Generating a Key
// keygen = KeyGenerator.getInstance("DES");
// keygen = KeyGenerator.getInstance("DESede");
keygen = KeyGenerator.getInstance("Blowfish"); desKey = keygen.generateKey();
System.out.println(
desKey.getEncoded().toString()
); // Create the cipher
// desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// desCipher = Cipher.getInstance("DESede");
desCipher = Cipher.getInstance("Blowfish");
// Our cleartext
byte[] cleartext = "This is just an example:邢战军".getBytes();
StringBuffer StrBuf = new StringBuffer();
for(int i=0,j=cleartext.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)cleartext[i]));
}
System.out.println(" ");
System.out.println(StrBuf.toString()); //Initialize the cipher for encryption
desCipher.init(Cipher.ENCRYPT_MODE,desKey); // Encrypt the cleartext
StrBuf = new StringBuffer();
byte[] ciphertext = desCipher.doFinal(cleartext);
// System.out.println(ciphertext.toString());
for(int i=0,j=ciphertext.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)ciphertext[i]));
}
System.out.println(" ");
System.out.println("Encrypt the cleartext");
System.out.println(StrBuf.toString()); SecretKeySpec seckey = new javax.crypto.spec.SecretKeySpec(
desKey.getEncoded(),"Blowfish");
System.out.println(
seckey.getEncoded()
); seckey = new javax.crypto.spec.SecretKeySpec(
seckey.getEncoded(),"Blowfish");
System.out.println(
seckey.getEncoded()
); // Initialize the same cipher for decryption
// desCipher.init(Cipher.DECRYPT_MODE, desKey);
desCipher.init(Cipher.DECRYPT_MODE, seckey); // Decrypt the ciphertext
StrBuf = new StringBuffer();
byte[] cleartext1 = desCipher.doFinal(ciphertext);
// System.out.println(cleartext1.toString());
for(int i=0,j=cleartext1.length;i<j;i++)
{
StrBuf.append(String.valueOf((char)cleartext1[i]));
}
System.out.println(" ");
System.out.println("Decrypt the ciphertext");
System.out.println(StrBuf.toString());
}
catch(java.security.InvalidKeyException e2)
{
e2.printStackTrace();
}
catch(java.security.NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch(javax.crypto.NoSuchPaddingException e1)
{
e1.printStackTrace();
}
catch(javax.crypto.BadPaddingException e3)
{
e3.printStackTrace();
}
catch(javax.crypto.IllegalBlockSizeException e4)
{
e4.printStackTrace();
} } public static void main(String[] args) {
En1 en11 = new En1();
}
}
在哪个JAR里?