安装 bouncy castle

document:http://www.bouncycastle.org/documentation.html
download:http://www.bouncycastle.org/latest_releases.html   ---bcprov-jdk15-138.jar


installation step:

The provider can be configured as part of your environment
via static registration by adding an entry to the java.security
properties file (found in $JAVA_HOME/jre/lib/security/java.security,
where $JAVA_HOME is the location of your JDK/JRE distribution).
You'll find detailed instructions in the file but basically it
comes down to adding a line:

security.provider.<n>=org.bouncycastle.jce.provider.BouncyCastleProvider

Where <n> is the preference you want the provider at (1 being the most prefered).
Note: issues arise if the Sun provided providers are not first.

Where you put the jar is up to mostly up to you, although with jdk1.3 and jdk1.4
the best (and in some cases only) place to have it is in $JAVA_HOME/jre/lib/ext.
Note: under Windows there will normally be a JRE and a JDK install of Java if you
think you have installed it correctly and it still doesn't work chances are you
have added the provider to the installation not being used.

Note: with JDK 1.4 and later you will need to have installed the unrestricted policy
files to take full advantage of the provider. If you do not install the policy files
you are likely to get something like the following:

java.lang.SecurityException: Unsupported keysize or algorithm parameters
            at javax.crypto.Cipher.init(DashoA6275)
The policy files can be found at the same place you downloaded the JDK.

 

test programme:

import java.security.*;
import javax.crypto.*;
import java.util.*;
import java.io.*;
import java.net.*;
import sun.misc.*;

public class testBC{

 public static void main(String[] args) throws Exception{

  KeyGenerator keyGenerator=KeyGenerator.getInstance("Blowfish");

  keyGenerator.init(128);

  SecretKey key=keyGenerator.generateKey();

  Cipher cipher=Cipher.getInstance("Blowfish/ECB/PKCS5Padding");

  cipher.init(Cipher.ENCRYPT_MODE,key);

  for(int i=1;i<5;i++){
   
   System.out.print("enter the string needed to encrypt:");
   
   byte[] buff = new byte[100];
   
   Scanner scanner = new Scanner(System.in);
   String stringToEncrypt = scanner.nextLine();
    
   byte[] cipherText=cipher.doFinal(stringToEncrypt.getBytes("UTF8"));
   
   System.out.print("the string after encrypting is: "); 
   
   System.out.println(new BASE64Encoder().encode(cipherText));
  }
 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值