首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RC6密钥生成器不可用

RC6密钥生成器不可用
EN

Stack Overflow用户
提问于 2017-10-21 19:42:59
回答 1查看 634关注 0票数 2

我会尝试使用RC6算法,但我有一个错误:

RC6 KeyGenerator不可用

如何获得rc6的密钥生成器?

线程"main“中的异常: javax.crypto.KeyGenerator.(KeyGenerator.java:169) at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:223) at RC6.encrypt(RC6.java:27)在RC6.main上不可用的RC6 KeyGenerator (RC6.java:16)

代码语言:javascript
复制
import javax.crypto.spec.*;
import java.security.*;
import javax.crypto.*;

public class Main
{
   private static String algorithm = "RC6";

   public static void main(String []args) throws Exception {
      String toEncrypt = "The shorter you live, the longer you're dead!";

      System.out.println("Encrypting...");
      byte[] encrypted = encrypt(toEncrypt, "password");

      System.out.println("Decrypting...");
      String decrypted = decrypt(encrypted, "password");

      System.out.println("Decrypted text: " + decrypted);
   }

   public static byte[] encrypt(String toEncrypt, String key) throws Exception {
      // create a binary key from the argument key (seed)
      SecureRandom sr = new SecureRandom(key.getBytes());
      KeyGenerator kg = KeyGenerator.getInstance(algorithm);
      kg.init(sr);
      SecretKey sk = kg.generateKey();

      // create an instance of cipher
      Cipher cipher = Cipher.getInstance(algorithm);

      // initialize the cipher with the key
      cipher.init(Cipher.ENCRYPT_MODE, sk);

      // enctypt!
      byte[] encrypted = cipher.doFinal(toEncrypt.getBytes());

      return encrypted;
   }

   public static String decrypt(byte[] toDecrypt, String key) throws Exception {
      // create a binary key from the argument key (seed)
      SecureRandom sr = new SecureRandom(key.getBytes());
      KeyGenerator kg = KeyGenerator.getInstance(algorithm);
      kg.init(sr);
      SecretKey sk = kg.generateKey();

      // do the decryption with that key
      Cipher cipher = Cipher.getInstance(algorithm);
      cipher.init(Cipher.DECRYPT_MODE, sk);
      byte[] decrypted = cipher.doFinal(toDecrypt);

      return new String(decrypted);
   }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-21 20:54:16

RC6不是由Oracle安全提供者之一提供的算法。提供程序提供了CipherKeyGenerator后面的算法实现。

在类路径中添加了.jar之后,应该可以这样做:

代码语言:javascript
复制
static {
    Security.addProvider(new BouncyCastleProvider());
}

您还可能需要在JRE文件夹中安装无限加密文件。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46867344

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档