首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java实现加密函数

用Java实现加密函数
EN

Stack Overflow用户
提问于 2017-12-28 12:20:36
回答 1查看 1.8K关注 0票数 0

javascript开发人员使用密码js对文本进行加密。使用起来很简单。

代码语言:javascript
复制
var CryptoJS = require("crypto-js");

// Encrypt
var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123');

// Decrypt
var bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
var plaintext = bytes.toString(CryptoJS.enc.Utf8);

console.log(plaintext);

在本例中,encrypt函数只使用两个参数messageToEncrypt和salt。配置的其余部分将在其定义中。我不是一个javascript的家伙,所以很难找到和理解加密定义。

我希望使用java实现相同的AES加密。因此,对于相同的输入参数,例如messageToEncrypt和salt,我应该使用密码-js库和Java实现获得相同的加密文本。

我在谷歌上浏览了一些链接,尝试了javax.crypto

代码语言:javascript
复制
String plainText = "messageToEncrypt";
String key = "mySalt";
SecretKey secKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
byte[] x = aesCipher.doFinal(plainText.getBytes());
System.out.println(x);

但这并不适用于我,因为我不知道确切的参数,如keySize和iterationCount。

我也尝试过使用https://github.com/mpetersen/aes-example/blob/master/src/main/java/org/cloudme/sample/aes/AesUtil.java,但是对于keySize和iterationCount,我还是不太确定。

如何在java中创建密码js的AES加密的简单精确实现?

EN

回答 1

Stack Overflow用户

发布于 2017-12-28 17:44:22

经过一些尝试和尝试,我得到了Java的工作实现。

代码语言:javascript
复制
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class TestAES {

    static String encrypt(String textToEncrypt, String myOwnSalt) throws Exception {
        final byte[] pass = textToEncrypt.getBytes(StandardCharsets.UTF_8);
        final byte[] salt = (new SecureRandom()).generateSeed(8);
        final byte[] inBytes = myOwnSalt.getBytes(StandardCharsets.UTF_8);

        final byte[] passAndSalt = array_concat(pass, salt);
        byte[] hash = new byte[0];
        byte[] keyAndIv = new byte[0];
        for (int i = 0; i < 3 && keyAndIv.length < 48; i++) {
            final byte[] hashData = array_concat(hash, passAndSalt);
            final MessageDigest md = MessageDigest.getInstance("MD5");
            hash = md.digest(hashData);
            keyAndIv = array_concat(keyAndIv, hash);
        }

        final byte[] keyValue = Arrays.copyOfRange(keyAndIv, 0, 32);
        final byte[] iv = Arrays.copyOfRange(keyAndIv, 32, 48);
        final SecretKeySpec key = new SecretKeySpec(keyValue, "AES");

        final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
        byte[] data = cipher.doFinal(inBytes);
        data =  array_concat(array_concat("Salted__".getBytes(StandardCharsets.UTF_8), salt), data);
        return Base64.getEncoder().encodeToString( data );
    }

    private static byte[] array_concat(final byte[] a, final byte[] b) {
        final byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        return c;
    }

    public static void main(String[] args) throws Exception {
        String s = encrypt("myPassword", "2ErFG");
        System.out.println(s);
    }

}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48007423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档