首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 企业软件 > 行业软件 >

bouncycastle(一) Use bouncy castle provider runtime

2012-10-20 
bouncycastle(1) Use bouncy castle provider runtimebouncycastle(1) Use bouncy castle provider runtim

bouncycastle(1) Use bouncy castle provider runtime
bouncycastle(1) Use bouncy castle provider runtime

JCE is the short for The Java Cryptography Extension.

From this class, we can see the provider list of my JDK.
package com.sillycat.easycastle.tools;


import java.security.Provider;
import java.security.Security;
import java.util.Iterator;
import java.util.Set;


publicclass ProviderInformation {
publicstaticvoid main(String[] args) {
Provider[] providers = Security.getProviders();
for (int i = 0; i < providers.length; i++) {
Provider provider = providers[i];
System.out.println("Provider name: " + provider.getName());
System.out.println("Provider information: " + provider.getInfo());
System.out.println("Provider version: " + provider.getVersion());
Set<?> entries = provider.entrySet();
Iterator<?> iterator = entries.iterator();
while (iterator.hasNext()) {
System.out.println("Property entry: " + iterator.next());
}
}
}
}

And I use a test class to verify DES with bouncy castle:
package com.sillycat.easycastle.runner;

import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

public class DESEncryptAndDecrypt {
   
     private static final String JCE_PROVIDER_BC = "BC";
   
     public static void main(String[] args) {
          //Security.addProvider(new com.sun.crypto.provider.SunJCE());
          Security.addProvider(new BouncyCastleProvider());
          try {
               // "BC" is the name of the BouncyCastle provider
               KeyGenerator kg = KeyGenerator.getInstance("DES",JCE_PROVIDER_BC);
             
               Key key = kg.generateKey();
               Cipher cipher = Cipher.getInstance("DES",JCE_PROVIDER_BC);

               byte[] data = "Hello World!".getBytes();
               System.out.println("Original data : " + new String(data));

               cipher.init(Cipher.ENCRYPT_MODE, key);
               byte[] result = cipher.doFinal(data);
               System.out.println("Encrypted data: " + new String(result));

               cipher.init(Cipher.DECRYPT_MODE, key);
               byte[] original = cipher.doFinal(result);
               System.out.println("Decrypted data: " + new String(original));
          } catch (NoSuchAlgorithmException e) {
               e.printStackTrace();
          } catch (NoSuchPaddingException e) {
               e.printStackTrace();
          } catch (InvalidKeyException e) {
               e.printStackTrace();
          } catch (IllegalStateException e) {
               e.printStackTrace();
          } catch (IllegalBlockSizeException e) {
               e.printStackTrace();
          } catch (BadPaddingException e) {
               e.printStackTrace();
          } catch (NoSuchProviderException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }
}

I import the package in pom.xml like this:
<dependency>
  <groupId>bouncycastle</groupId>
  <artifactId> bcprov-jdk16</artifactId>
  <version> 140</version>
</dependency>

references:
http://www.bouncycastle.org
http://www.bouncycastle.org/specifications.html#install
http://hi.baidu.com/bluewhale84/blog/item/f3bb20a1f538a9884710648c.html
http://security.group.iteye.com/group/wiki/2280-Non-symmetric-encryption-Digital-Signature
http://snowolf.iteye.com/blog/379860
http://sillycat.iteye.com/blog/563515

热点排行