安装 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
    评论
安装 Bouncy Castle 是相对简单的过程,您只需要遵循以下步骤即可: 1. 访问 Bouncy Castle 官方网站:https://www.bouncycastle.org/ 2. 点击下载页面中的 “Downloads” 链接,然后选择您需要的版本,例如 “Bouncy Castle Provider 1.68” 。 3. 下载适合您操作系统的版本。 4. 解压下载的压缩包,您会发现其中包含了一个 JAR 文件。 5. 将该 JAR 文件添加到您的 Java 项目中,以便您可以使用 Bouncy Castle 提供的安全功能。具体方法取决于您使用的构建工具,但通常包括以下步骤: a. 在您的项目中创建一个名为 “lib” 的目录(如果您没有这样做)。 b. 将下载的 JAR 文件复制到这个目录中。 c. 在您的项目设置中添加对该 JAR 文件的引用。 6. 完成以上步骤后,您就可以使用 Bouncy Castle 提供的安全功能了。例如,在 Java 中使用 Bouncy Castle 加密数据的示例代码如下: ``` import org.bouncycastle.jce.provider.BouncyCastleProvider; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.Security; public class BouncyCastleExample { public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); byte[] key = "0123456789abcdef".getBytes(); byte[] iv = "fedcba9876543210".getBytes(); byte[] plaintext = "Hello, world!".getBytes(); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] ciphertext = cipher.doFinal(plaintext); System.out.println(new String(ciphertext)); } } ``` 希望这可以帮助您安装和使用 Bouncy Castle 库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值