簡體   English   中英

如何在Android JAVA代碼中使用Camellia 128bit加密和解密?

[英]How to encrypt & decrypt using Camellia 128bit in Android JAVA code?

Android JAVA 代碼中 Camellia 128bit 的提供程序名稱語法是什么? 我嘗試從Cipher.getInstance("AES/CBC/PKCS7Padding") Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC")Cipher.getInstance("Camellia/CBC/PKCS7Padding","BC")但它說 Provider BC 不提供“Camellia/CBC/PKCS7Padding”。 下面是我的代碼

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());
  }

我通過這段代碼成功運行了代碼new org.bouncycastle.jce.provider.BouncyCastleProvider()); 作為提供者。 非常感謝這個評論。 下面是完整的語法

Cipher desCipher = Cipher.getInstance("Camellia/CBC/PKCS7Padding",new org.bouncycastle.jce.provider.BouncyCastleProvider());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM