求 AES-ECB加解密dex

用于数据加密传输,返回的结果是BASE64编码

img


结果和这个一样,
也可以是java类,我自己转换一下就行。

java 的这个加密 你可能还真不好转,基本都是直接借助底层包依赖实现的

下面是一个示例,说明如何使用CipherJava 密码术扩展 (JCE) 中的类执行数据的 AES-ECB 加密和解密,返回的结果以 BASE64 编码:

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AESExample {

    public static void main(String[] args) throws Exception {
        // Set the key and transformation for the cipher
        Key key = new SecretKeySpec("1234567890123456".getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");

        // Define the plaintext message
        byte[] plaintext = "This is the message to be encrypted".getBytes();

        // Encrypt the message
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] ciphertext = cipher.doFinal(plaintext);

        // Encode the ciphertext in BASE64
        String encodedCiphertext = Base64.getEncoder().encodeToString(ciphertext);

        // Decrypt the message
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decryptedPlaintext = cipher.doFinal(ciphertext);

        // Encode the decrypted message in BASE64
        String encodedDecryptedPlaintext = Base64.getEncoder().encodeToString(decryptedPlaintext);

        // Print the results
        System.out.println("Encrypted message (BASE64): " + encodedCiphertext);
        System.out.println("Decrypted message (BASE64): " + encodedDecryptedPlaintext);
    }
}

此代码将首先Cipher使用指定的密钥和转换(“AES/ECB/NoPadding”)创建一个新对象。然后它将使用init()和doFinal()方法加密明文消息并解密密文。生成的加密和解密消息使用类中的getEncoder().encodeToString()方法以 BASE64 编码Base64。

请记住,这只是在 Java 中使用 BASE64 编码执行 AES-ECB 加密和解密的一种方法。您可以使用其他方法或库来获得相同的结果。

下面是一个使用 Java 实现 AES-ECB 加解密的示例代码,其中使用了 BASE64 编码进行数据加密传输:


import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class AESUtil {

  private static final String ALGORITHM = "AES";
  private static final String KEY = "your secret key";
  private static final String CHARSET = "UTF-8";

  /**
   * 加密
   * @param data 要加密的数据
   * @return 加密后的数据(BASE64编码)
   */
  public static String encrypt(String data) {
    try {
      Key key = new SecretKeySpec(KEY.getBytes(CHARSET), ALGORITHM);
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(Cipher.ENCRYPT_MODE, key);
      byte[] encryptedData = cipher.doFinal(data.getBytes(CHARSET));
      return new BASE64Encoder().encode(encryptedData);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  /**
   * 解密
   * @param encryptedData 加密后的数据(BASE64编码)
   * @return 解密后的数据
   */
  public static String decrypt(String encryptedData) {
    try {
      Key key = new SecretKeySpec(KEY.getBytes(CHARSET), ALGORITHM);
      Cipher cipher = Cipher.getInstance(ALGORITHM);
      cipher.init(Cipher.DECRYPT_MODE, key);
      byte[] decryptedData = cipher.doFinal(new BASE64Decoder().decodeBuffer(encryptedData));
      return new String(decryptedData, CHARSET);
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}

在这个示例代码中,你需要将 KEY 常量替换成你自己的密钥。另外,你还可以通过更改 ALGORITHM 常量来使用其他的加密算法。

由浅入深AES-ECB模式加密、解密
可以借鉴下
https://blog.csdn.net/m0_53061933/article/details/120383770

提供参考实例:AES加密、解密工具类,链接:https://blog.csdn.net/Wang_WY/article/details/88691646?spm=1001.2101.3001.6650.15&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-15-88691646-blog-112213597.pc_relevant_recovery_v2&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-15-88691646-blog-112213597.pc_relevant_recovery_v2&utm_relevant_index=23