AES加密CTR模式

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AES {

    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPHER_ALGORITHM = "AES/CTR/PKCS5Padding";
    public static final String ivParameter = "1234123412341234";

    public static String Encrypt(String key, String text) {
        try {
            byte[] keyBytes = key.getBytes();
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            SecretKeySpec sKeySpec = new SecretKeySpec(keyBytes, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv);
            byte[] encrypted = cipher.doFinal(text.getBytes("utf-8"));
            return new BASE64Encoder().encode(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String Decrypt(String Key, String text) {
        try {
            byte[] raw = Key.getBytes("ASCII");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(text);
            byte[] original = cipher.doFinal(encrypted1);
            String originalString = new String(original, "utf-8");
            return originalString;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

}

如果文章写到这里,那我也太次了!!!

事情的起因是这样的,我需要调用堡垒机的接口,接口的传参是要经过aes加密的,于是乎,我咨询了厂商:

我:请问咱们使用的是哪种aes?

厂商:aes256

我:这不行啊,需要知道加密的模式,例如有没有对齐和初始化向量。

厂商:不太懂

我:。。。

我:那给我一个示例代码吧,然后给了我上面的代码。

好吧,CTR方式,我发现竟然不是常用的cbc,或者cfb之类的,果然是不同凡响。

果断写出python代码:

import binascii
from Crypto.Cipher import AES
from Crypto.Util import Counter
from base64 import b64decode, b64encode

BLOCK_SIZE = AES.block_size

class AESCipher:

    def __init__(self, key, iv):
        self.key = key.encode()
        self.iv = iv.encode()

    def encrypt(self, text):
        text = text.encode()
        iv_int = int(binascii.hexlify(self.iv), 16)
        ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
        cipher = AES.new(key=self.key, mode=AES.MODE_CTR, counter=ctr)
        encrypted_text = cipher.encrypt(text)
        return b64encode(encrypted_text).decode('utf-8')

    def decrypt(self, encrypted_text):
        encrypted_text = b64decode(encrypted_text)
        iv_int = int(binascii.hexlify(self.iv), 16)
        ctr = Counter.new(AES.block_size * 8, initial_value=iv_int)
        cipher = AES.new(key=self.key, mode=AES.MODE_CTR, counter=ctr)
        decrypted_text = cipher.decrypt(encrypted_text)
        return decrypted_text.decode()

def main():
    cipher = AESCipher("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "1234123412341234")
    enc = cipher.encrypt("{\"key1\":\"value1\"}")
    print(enc)
    print(cipher.decrypt(enc))

if __name__ == '__main__':
    main()

结束!

你可能感兴趣的:(算法,安全,https,java)