Bouncy Castle加密库简介

Bouncy Castle Crypto是一个Java实现的加密包,同时也有C#版本。它包含一套轻量级的API可以在包括J2ME在内的绝大多数环境中运行。部分手机不支持JSR177也可使用该加密包实现加密功能。Bouncy Castle加密包包含了大多数流行的加密实现(如:AES、DES、Blowfish等)、散列算法(如:MD5、SHA等)、字节字符编码(如:Base64)等。

下面一个段测试代码(包括MD5,Base64以及AES加密):
[code]
public void test() {
String clearText = "Hello World!";
String password = "hi";

//md5
MD5Digest digest = new MD5Digest();
digest.update(password.getBytes(), 0, password.length());
int md5Num = digest.getByteLength();
byte[] md5Buf = new byte[md5Num];
digest.doFinal(md5Buf, 0);

//base64
byte[] base64Buf = Base64.encode(md5Buf);

byte[] key = new byte[16];//设置key长度为128位
System.arraycopy(base64Buf, 0, key, 0, 16);

//AES加密
byte[] cipherBuf = null;
cipherBuf = AESEncode(clearText.getBytes(), key);

//AES解密
String decryptText = AESDecode(cipherBuf, key);

System.out.println("md5:" + new String(md5Buf));
System.out.println("base64:" + new String(base64Buf));
System.out.println("key:" + new String(key));
System.out.println("cipher:" + new String(cipherBuf));
System.out.println("clear:" + decryptText);
}
[/code]Base64编码后的字符串是由a~z,A~Z,+,/这64个字符组合而成,末尾补齐用“=”号表示。
AES加密算法对key的长度有限制,它只支持128,192和256位的key。所以代码中接key设置成16个字节。

下面是AES加密的代码:
[code]
public byte[] AESEncode(byte[] clearText, byte[] key) {
byte[] rv = null;

BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new AESEngine());
cipher.init(true, new KeyParameter(key));
rv = new byte[cipher.getOutputSize(clearText.length)];
int oLen = cipher.processBytes(clearText, 0, clearText.length, rv, 0);
try {
cipher.doFinal(rv, oLen);
} catch (DataLengthException e) {
} catch (IllegalStateException e) {
} catch (InvalidCipherTextException e) {
}

return rv;
}
[/code]

下面是AES解密的代码:
[code]
public String AESDecode(byte[] cipherText, byte[] key) {
byte[] rv = null;
BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new AESEngine());
cipher.init(false, new KeyParameter(key));
rv = new byte[cipher.getOutputSize(cipherText.length)]; //该大小会大于实际的大小
int oLen = cipher.processBytes(cipherText, 0, cipherText.length, rv, 0);
try
{
cipher.doFinal(rv, oLen);
} catch (DataLengthException e) {
} catch (IllegalStateException e) {
} catch (InvalidCipherTextException e) {
}

return new String(rv).trim();
}
[/code]
代码中rv的初始化长度会大于实际解密后数据的长度,所以生成的String要去掉空格。

Bouncy Castle官网:http://www.bouncycastle.org/
JavaTM Cryptography Extension (JCE) 参考指南:
http://download.oracle.com/javase/1.4.2/docs/guide/security/jce/JCERefGuide.html
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
org.bouncycastle.openssl.PEMException: problem parsing ENCRYPTED PRIVATE KEY: java.lang.SecurityException: JCE cannot authenticate the provider BC at org.bouncycastle.openssl.PEMReader$EncryptedPrivateKeyParser.parseObject(Unknown Source) at org.bouncycastle.openssl.PEMReader.readObject(Unknown Source) at org.whispersystems.textsecuregcm.push.RetryingApnsClient.initializePrivateKey(RetryingApnsClient.java:135) at org.whispersystems.textsecuregcm.push.RetryingApnsClient.(RetryingApnsClient.java:65) at org.whispersystems.textsecuregcm.push.APNSender.(APNSender.java:61) at org.whispersystems.textsecuregcm.WhisperServerService.run(WhisperServerService.java:182) at org.whispersystems.textsecuregcm.WhisperServerService.run(WhisperServerService.java:111) at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:43) at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87) at io.dropwizard.cli.Cli.run(Cli.java:78) at io.dropwizard.Application.run(Application.java:93) at org.whispersystems.textsecuregcm.WhisperServerService.main(WhisperServerService.java:283) Caused by: java.lang.SecurityException: JCE cannot authenticate the provider BC at javax.crypto.Cipher.getInstance(Cipher.java:656) at javax.crypto.Cipher.getInstance(Cipher.java:595) ... 12 more Caused by: java.util.jar.JarException: file:/opt/code/signal-Server-master/target/TextSecureServer-1.87.jar has unsigned entries - org/whispersystems/dispatch/DispatchManager$4.class at javax.crypto.JarVerifier.verifySingleJar(JarVerifier.java:502) at javax.crypto.JarVerifier.verifyJars(JarVerifier.java:363) at javax.crypto.JarVerifier.verify(JarVerifier.java:289) at javax.crypto.JceSecurity.verifyProviderJar(JceSecurity.java:164) at javax.crypto.JceSecurity.getVerificationResult(JceSecurity.java:190) at javax.crypto.Cipher.getInstance(Cipher.java:652) ... 13 more --------------------- 作者:idwtwt 来源:CSDN 原文:https://blog.csdn.net/idwtwt/article/details/83793940 版权声明:本文为

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值