Java 如何在angularJs中添加加密密码生成AES/CBC/PKCS5P

Java 如何在angularJs中添加加密密码生成AES/CBC/PKCS5P,java,cryptography,Java,Cryptography,我正在开发一个功能,该功能要求将Aes加密(Aes/CBC/PKCS5P)密码文本从客户端发送到后端有ASP.Net的服务器 我在服务器端有一个解密函数,如下所示: public static string Decrypt(string inputBase64, string passphrase = null) { byte[] key, iv = new byte[0]; byt

我正在开发一个功能,该功能要求将Aes加密(Aes/CBC/PKCS5P)密码文本从客户端发送到后端有ASP.Net的服务器

我在服务器端有一个解密函数,如下所示:

 public static string Decrypt(string inputBase64, string passphrase = null)
                {
                    byte[] key, iv = new byte[0];
                    byte[] base64data = Convert.FromBase64String(inputBase64);
                    byte[] passphrasedata = RawBytesFromString(passphrase);
                    byte[] currentHash = new byte[0];
                    SHA256Managed hash = new SHA256Managed();
                    currentHash = hash.ComputeHash(passphrasedata);
                    return DecryptStringFromBytes(base64data, currentHash, null);
                }



static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments. 
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            //if (IV == null || IV.Length <= 0)
            //  throw new ArgumentNullException("Key");

            // Declare the string used to hold 
            // the decrypted text. 
            string plaintext = null;

            // Create an RijndaelManaged object 
            // with the specified key and IV. 
            using (var cipher = new RijndaelManaged())
            {
                cipher.Key = Key;
                cipher.IV = new byte[16];
                //cipher.Mode = CipherMode.CBC;
                //cipher.Padding = PaddingMode.PKCS7;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = cipher.CreateDecryptor(Key, cipher.IV);

                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        var bytes = default(byte[]);
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
                            bytes = srDecrypt.CurrentEncoding.GetBytes(srDecrypt.ReadToEnd());

                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            //aintext = srDecrypt.ReadToEnd();
                        }
                        plaintext = ASCIIEncoding.UTF8.GetString(bytes, 0, bytes.Count());
                    }
                }

            }

            return plaintext;

        }
问题是,编码的字符串无法解密回其以前的形式。我认为客户端的加密逻辑和服务器端的解密逻辑不匹配

当我将CryptoJS加密密码传递给java decryption函数时,它显示错误:

javax.crypto.IllegalBlockSizeException:使用填充密码解密时,输入长度必须是16的倍数

或者有时:

javax.crypto.BadPaddingException:给定的最后一个块未正确填充


谢谢大家!!!,我让它与以下代码一起工作

    function hash (){
       return CryptoJS.SHA256(password);
    }
    var cipher = (function(plaintext, password) {
                        passwordHash = hash(password);
                        var iv = CryptoJS.enc.Hex.parse('0000000000000000');
                        var cipher = CryptoJS.AES.encrypt(plaintext, passwordHash, {
                            iv: iv,
                            mode: CryptoJS.mode.CBC,
                            keySize: 256 / 32,
                            padding: CryptoJS.pad.Pkcs7
                        });
                        return cipher;
    })(plaintext, password);

   cipherBase64 =  cipher.ciphertext.toString().hex2a().base64Encode();

看起来您的IV大小不同,JS为32字节,c为16字节。您在c和Java中使用UTF-8,但在JS中出于某种原因使用拉丁语。@BhavO:我已将IV更改为16字节,仍然无法工作。我给了Latin1,使之成为32字节的字符串。如果我将其转换为UTF-8,字符串的长度将超过32字节。Aes需要32字节的密钥(256bit)您是否可以更新答案,并有任何新的错误thanks@BhavO:我已将IV更新为16字节。错误是一样的。我怀疑这是否是填充错误。当我将密码粘贴到在线AES解密算法中时,我遇到了一个错误“给定的最终块未正确填充”。您好,我遇到了错误
cipher.ciphertext.toString(…)。hex2a不是最后一行的函数
。你能帮忙吗?嗯。。。我正在使用JQM。它是否仅与AngularJS相关?与react nativeHi相同,我们如何为用户生成公钥和私钥?
var password = '12345678';
var passwordHash = CryptoJS.SHA256(password).toString(CryptoJS.enc.Latin1);
var iv = CryptoJS.enc.Hex.parse('0000000000000000');                                                                       
var cipher = CryptoJS.AES.encrypt(plaintext,passwordHash,{
                            iv: iv,
                            mode: CryptoJS.mode.CBC,
                            keySize: 256/32,
                            padding: CryptoJS.pad.Pkcs7
                            });
cipherText = cipher.ciphertext.toString(CryptoJS.enc.Base64);
    function hash (){
       return CryptoJS.SHA256(password);
    }
    var cipher = (function(plaintext, password) {
                        passwordHash = hash(password);
                        var iv = CryptoJS.enc.Hex.parse('0000000000000000');
                        var cipher = CryptoJS.AES.encrypt(plaintext, passwordHash, {
                            iv: iv,
                            mode: CryptoJS.mode.CBC,
                            keySize: 256 / 32,
                            padding: CryptoJS.pad.Pkcs7
                        });
                        return cipher;
    })(plaintext, password);

   cipherBase64 =  cipher.ciphertext.toString().hex2a().base64Encode();