首页 > 其他分享 >Base64(AES128(字段,秘钥))对称加密

Base64(AES128(字段,秘钥))对称加密

时间:2022-09-30 13:45:39浏览次数:36  
标签:AES String Base64 秘钥 AES128 cipher static KEY byte

 

代码实现如下:

public class SecurityAESUtils {

    private static String AES_KEY = "123456789qazwsx#";
    private static String AES_MODE = "AES/ECB/PKCS5Padding";

    private Key key;

    private static SecurityAESUtils securityAESUtils;
    public static SecurityAESUtils getInstance() {
        if(securityAESUtils == null) {
            securityAESUtils = new SecurityAESUtils();
        }
        return securityAESUtils;
    }

    public static byte[] encryptECB(String data, String AES_KEY) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = Cipher.getInstance(AES_MODE);
        cipher.init(Cipher.ENCRYPT_MODE,new SecretKeySpec(AES_KEY.getBytes(),"AES"));
        byte[] result = cipher.doFinal(data.getBytes());
        return result;
    }

    public static byte[] decryptEBC(byte[] data,String AES_KEY) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        Cipher cipher = null;
        cipher = Cipher.getInstance(AES_MODE);
        cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(AES_KEY.getBytes(), "AES"));
        byte[] result = cipher.doFinal(data);
        return result;
    }

    public static void main(String[] args) throws NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
        String data = "367601198908244058";
        byte[] ciphertext = encryptECB(data,AES_KEY);
        String encodeString = Base64.getEncoder().encodeToString(ciphertext);
        System.out.println("ECB 模式加密结果(Base64):" + encodeString);

        //解密
        byte[] decodeByte = Base64.getDecoder().decode(encodeString);
        String decodeString = new String(SecurityAESUtils.decryptEBC(decodeByte,AES_KEY));
        System.out.printf("ECB 模式解密结果(Base64):"+decodeString);
    }

}

 

标签:AES,String,Base64,秘钥,AES128,cipher,static,KEY,byte
From: https://www.cnblogs.com/sfnz/p/16744647.html

相关文章

  • base64相关知识(包含标准及非标准base64知识,附带转换表)
    base64是用于传输8Bit字节码的编码方式之一。Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看......
  • 网络图片转base64
    /**        * 网络图片转base64        * @param src        * @return        * @throws Exception     ......
  • base64格式在Html中的用法
     Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。 图片data:image/gif;base64,base64编码的gi......
  • JDK8的base64编解码、图片转base64
    importjava.io.ByteArrayOutputStream;importjava.io.FileInputStream;importjava.io.InputStream;importjava.nio.charset.StandardCharsets;importjava.util.Base64......
  • 请求 blob 图像并使用 fetch API 转换为 base64
    通用功能:functionurlContentToDataUri(url){returnfetch(url).then(response=>response.blob()).then(blob=>newPromise(cal......
  • 14、Android Studio通过http向C++服务端传递base64图片,然后对图片处理(写入本地)返回数
    基本思想:最近做了一个项目需要使用将androidstudio中抓取的视频帧和一些数据上传服务器处理,然后将处理结果返回给androidstudio手机端一、因为不太会写通信,着实补充了一......
  • base64图片保存
    讲base64编码的图片,保存为pngfromioimportBytesIOfromPILimportImageimportbase64defbase64_to_image(base64_str):#输入为base64格式字符串,输出为PIL......
  • Convert gif to Base64 String Using JavaScript
    letxhRequest=newXMLHttpRequest();xhRequest.onload=function(){letreader=newFileReader();reader.onloadend=function(){......
  • ASCII、MIME、BASE64
    ASCII美国信息交换标准代码(AmericanStandardCodeforInformationInterchange,ASCII)在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用......
  • 前端安全之Base64基础
    Base64基础Base64是什么?Base64是一种基于64个可打印字符(也就是ASCII字符)来表示二进制数据的编码方式,是将二进制数据转化为字符串的过程,原则上来说一些存储在计算机上的......