//psw:加密的文本,pswkey:加密的密码,即密钥
String psw="12345",pswkey="999";
//对该DES情况下加密,密钥需要为56位难以人工由string为根据设定,因此这里:
//借助密钥生成器,由new SecureRandom(pswkey.getBytes()),以pswkey为种子值生成密钥
KeyGenerator keyGenerator=KeyGenerator.getInstance("DES");
keyGenerator.init(56,new SecureRandom(pswkey.getBytes()));
Key key=keyGenerator.generateKey();
//加密,DES/ECB/pkcs5padding为:算法/模式/填充 的参数
//参数new BouncyCastleProvider()也可修改为"BC",要求前面要有Security.addProvider(new BouncyCastleProvider());
Cipher cipher=Cipher.getInstance("DES/ECB/pkcs5padding",new BouncyCastleProvider());
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] out=cipher.doFinal(psw.getBytes());
System.out.println(out);//字节数组,没有编码
//解密
Cipher cipher2=Cipher.getInstance("DES/ECB/pkcs5padding",new BouncyCastleProvider());
cipher2.init(Cipher.DECRYPT_MODE, key);
byte[] in=cipher2.doFinal(out);
System.out.println(new String(in));
java BouncyCastle 使用
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 错误 Class JavaLaunchHelper is implemented in both /Library...