简体   繁体   English

给定密钥的Java AES加密

[英]AES encryption in Java for given key

I am trying to do AES encryption in Java. 我正在尝试用Java进行AES加密。 I have the following function: 我有以下功能:

public static String encrypt(String plainText, String key) throws Exception {
    if (plainText == null || plainText.length() == 0) {
        return plainText;
    }

    // get aes key
    SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");

    // encrypt
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
    byte[] bytes = cipher.doFinal(plainText.getBytes("UTF-8"));

    //encode
    String encoded = Base64.encodeToString(bytes, Base64.NO_PADDING | Base64.NO_WRAP);

    return encoded;
}

My aim is to be able to encrypt some text with a given key the same way every time. 我的目标是每次都能以相同的方式用给定的密钥加密某些文本。 That is, if I call this function with the same two parameters many times, I want the same string to be returned on every call. 也就是说,如果我多次使用相同的两个参数调用此函数,则希望每次调用都返回相同的字符串。 I'll say in advance, I know that is not how cryptography is supposed to be done, but I need this functionality for my project. 我先说,我不知道应该怎么做加密,但是我的项目需要此功能。

Unfortunately, that is not the case. 不幸的是,事实并非如此。 The key generated in line 7 seems to encrypt my string differently every time around. 第7行中生成的密钥似乎每次都对我的字符串进行不同的加密。 I assume there is some kind of extra random automatic salting occurring on the lower levels of this library, preventing me from achieving my goal. 我假设在此库的较低级别上发生了某种额外的随机自动加盐,这使我无法实现自己的目标。

Does anyone know a way in Java how I could go about encrypting a given string with a given key to the same value every time? 有谁知道Java中的方法,我该如何每次使用给定的密钥将给定的字符串加密为相同的值? Thanks. 谢谢。

UPDATE/CLARIFICATION: This is not for security. 更新/说明:这不是为了安全。 This is for the encryption of data for it to be obfuscated for certain people that might come in contact with working on the app itself. 这是为了加密数据,以便某些可能与使用应用程序本身联系的人员混淆。 The information is not highly sensitive, but I do want it encrypted and then decrypted by the same key. 该信息不是高度敏感,但是我确实希望对其进行加密,然后再使用同一密钥解密。 I have others working with me on this with libraries in their respective languages, eg Ruby, and their libraries allow them to encrypt a value with a given key the same way every time. 我和其他人一起使用各自语言的库(例如Ruby)来与我合作,它们的库允许他们每次以相同的方式用给定的密钥加密值。 We all want to use the same parameters of the same encryption algorithm: Key length: 128 bits Mode of operation: CBC Initialization Vector: None (all zeros) 我们都希望使用相同加密算法的相同参数:密钥长度:128位操作模式:CBC初始化向量:无(全零)

Is it perhaps that if one does not set an initialization vector, it is randomly assigned? 也许如果没有设置初始化向量,它是否会被随机分配? I've got to check that out. 我必须检查一下。

Yes, Java - or rather the security provider supplying the AES in CBC mode implementation - may default to a random IV (which you then have to retrieve afterwards and include with your ciphertext) if you don't explicitly specify it you get secure, randomized ciphertext. 是的,如果您未明确指定Java,则Java(或更确切地说是以CBC模式实施的提供AES的安全提供程序)可能会默认为随机IV(然后您必须检索该随机IV并包含密文)。密文。

If you want to use a zero IV you will have to specify it explicitly: 如果要使用零IV,则必须明确指定:

cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]);

This is only slightly more secure than ECB mode, as any repetition in different messages in the initial ciphertext blocks will be instantly visible to an attacker. 这仅比ECB模式稍微安全一些,因为攻击者可以立即看到初始密文块中不同消息中的任何重复。

If you want to have a more secure mode without random IV - which is required for CBC mode to obtain CPA security - then you could check out synthetic IV (SIV) mode or GCM-SIV. 如果您想拥有一个没有随机IV的更安全的模式-CBC模式才能获得CPA安全性,那么您可以签出合成IV(SIV)模式或GCM-SIV。 For those modes the entire message needs to be identical with a previous one to leak info to an attacker. 对于那些模式,整个消息需要与先前的消息相同,以将信息泄露给攻击者。 It's however slowe, not included in the standard VM and the ciphertext could be larger than AES/CBC (inclusion of IV/tag vs padding requirements). 但是它很慢,没有包含在标准VM中,并且密文可能比AES / CBC大(包括IV / tag与padding要求)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM