首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Android JAVA代码中使用Camellia 128bit进行加解密?

如何在Android JAVA代码中使用Camellia 128bit进行加解密?
EN

Stack Overflow用户
提问于 2020-11-03 10:13:09
回答 1查看 76关注 0票数 0

Android JAVA代码中Camellia 128bit的提供程序名称语法是什么??我尝试从Cipher.getInstance("AES/CBC/PKCS7Padding")更改为Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC"),但它显示提供者BC不提供“Camellia/CBC/PKCS7Padding.”。下面是我的代码

代码语言:javascript
复制
try {
      String keyString = "Potatoman55@";//length of key is 16
      Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC");
      byte[] key = new byte[0];
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
              key = keyString.getBytes(StandardCharsets.UTF_8);
          }
      }
      MessageDigest sha = MessageDigest.getInstance("SHA-1");
      key = sha.digest(key);
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
          key = Arrays.copyOf(key, 16); // use only first 128 bit
      }

      SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
      desCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

      String plainText = "hello wolrd";
      System.out.println("plaintext: "+plainText);

      byte[] text = plainText.getBytes("UTF-8");
      byte[] textencrypted = desCipher.doFinal(text);
      String textEnc = null;
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
          System.out.println("encrypted: " + Base64.getEncoder().encodeToString(textencrypted));
          textEnc = Base64.getEncoder().encodeToString(textencrypted);
      }
      result.success(textEnc);

  } catch (Exception ex) {
      System.out.println(ex.toString());
  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-03 10:35:35

我以这个代码new org.bouncycastle.jce.provider.BouncyCastleProvider());作为提供者成功地运行了代码。非常感谢this的评论。下面是完整的语法

代码语言:javascript
复制
Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64655886

复制
相关文章

相似问题

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