怎样用java给文件加密和解密 JAVA上加密算法的实现用例http://www-900.ibm.com/developerWorks/java/l-security/index.shtml 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package encryption;/** * 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(); }} import javax.crypto.*;在哪个JAR里? 分页查询如何做缓存的问题 JAVA前一周每天时间的算法 JAVA 语言做APP程序的界面,用哪个集成开发环境比较多? java编码问题 求助:用JDIC做嵌入式浏览器为何不显示? java中object类型如何与数组类型转换? 关于数据库操作的奇怪问题 frame 的close问题 应该怎么改呢? 各位谁能帮忙看一下这个程序?谢谢!! 关于用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里?