AES加密协助
创始人
2024-07-29 14:00:25
0

要给出带有代码示例的AES加密协助的解决方法,需要使用一个支持AES加密的编程语言或库。以下是使用Java和Python的示例:

Java示例:

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

public class AESHelper {

    public static String encrypt(String plaintext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plaintext.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String ciphertext, String key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] encryptedBytes = Base64.getDecoder().decode(ciphertext);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }

    public static void main(String[] args) throws Exception {
        String plaintext = "Hello, AES!";
        String key = "abcdefghijklmnop";

        String encryptedText = encrypt(plaintext, key);
        System.out.println("Encrypted Text: " + encryptedText);

        String decryptedText = decrypt(encryptedText, key);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

Python示例:

from Crypto.Cipher import AES
import base64

def encrypt(plaintext, key):
    cipher = AES.new(key.encode(), AES.MODE_ECB)
    encryptedBytes = cipher.encrypt(plaintext.encode())
    return base64.b64encode(encryptedBytes).decode()

def decrypt(ciphertext, key):
    cipher = AES.new(key.encode(), AES.MODE_ECB)
    decryptedBytes = cipher.decrypt(base64.b64decode(ciphertext))
    return decryptedBytes.decode()

plaintext = "Hello, AES!"
key = "abcdefghijklmnop"

encryptedText = encrypt(plaintext, key)
print("Encrypted Text: " + encryptedText)

decryptedText = decrypt(encryptedText, key)
print("Decrypted Text: " + decryptedText)

这些示例使用了AES算法和ECB模式进行加密和解密,使用了预设的密钥长度和填充方式(PKCS5Padding)。请注意,ECB模式不是最安全的模式,因此在实际使用中可能需要考虑使用其他模式(如CBC)和一个更安全的密钥生成和管理方案。

相关内容

热门资讯

安卓系统怎么连不上carlif... 安卓系统无法连接CarLife的原因及解决方法随着智能手机的普及,CarLife这一车载互联功能为驾...
iwatch怎么连接安卓系统,... 你有没有想过,那款时尚又实用的iWatch,竟然只能和iPhone好上好?别急,今天就来给你揭秘,怎...
oppo手机安卓系统换成苹果系... OPPO手机安卓系统换成苹果系统:现实吗?如何操作?随着智能手机市场的不断发展,用户对于手机系统的需...
安卓平板改windows 系统... 你有没有想过,你的安卓平板电脑是不是也能变身成Windows系统的超级英雄呢?想象在同一个设备上,你...
iphone系统与安卓系统更新... 最近是不是你也遇到了这样的烦恼?手机更新系统总是失败,急得你团团转。别急,今天就来给你揭秘为什么iP...
安卓系统上滑按键,便捷生活与高... 你有没有发现,现在手机屏幕越来越大,操作起来却越来越方便了呢?这都得归功于安卓系统上的那些神奇的上滑...
安卓系统连接耳机模式,蓝牙、有... 亲爱的手机控们,你们有没有遇到过这种情况:手机突然变成了“耳机模式”,明明耳机没插,声音却只从耳机孔...
希沃系统怎么装安卓系统,解锁更... 亲爱的读者们,你是否也像我一样,对希沃一体机上的安卓系统充满了好奇呢?想象在教室里,你的希沃一体机不...
安装了Anaconda之后找不... 在安装Anaconda后,如果找不到Jupyter Notebook,可以尝试以下解决方法:检查环境...
安卓换鸿蒙系统会卡吗,体验流畅... 最近手机圈可是热闹非凡呢!不少安卓用户都在议论纷纷,说鸿蒙系统要来啦!那么,安卓手机换上鸿蒙系统后,...