【问题标题】:AES 128 Encryption in Angular 4 and decryption in JavaAngular 4 中的 AES 128 加密和 Java 中的解密
【发布时间】:2018-06-04 11:11:02
【问题描述】:

我使用 CryptoJS(AES) 在 Angular 4 中使用以下代码进行加密:

const key = CryptoJS.enc.Utf8.parse('7061737323313233');
const iv = CryptoJS.enc.Utf8.parse('7061737323313233');
const encrypted = CryptoJS.AES.encrypt('String to encrypt', key, {
  keySize: 16,
  iv: iv,
  mode: CryptoJS.mode.ECB,
  padding: CryptoJS.pad.Pkcs7
});
console.log('Encrypted :' + encrypted);

及以下使用AES解密的java代码:

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

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

public class Encryption {

private static SecretKeySpec secretKey;
private static byte[] key;

public static void setKey(String myKey)
{
    MessageDigest sha = null;
    try {
        key = myKey.getBytes("UTF-8");
        sha = MessageDigest.getInstance("SHA-1");
        key = sha.digest(key);
        key = Arrays.copyOf(key, 16);
        secretKey = new SecretKeySpec(key, "AES");
    }
    catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

public static String encrypt(String strToEncrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")));
    }
    catch (Exception e)
    {
        System.out.println("Error while encrypting: " + e.toString());
    }
    return null;
}

public static String decrypt(String strToDecrypt, String secret)
{
    try
    {
        setKey(secret);
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
    }
    catch (Exception e)
    {
        System.out.println("Error while decrypting: " + e.toString());
    }
    return null;
}
 }

public class Test {
public static void main(String[] args) {
    final String secretKey = "7061737323313233";
    String originalString = "Response from angular";
    String decryptedString = Encryption.decrypt(originalString, secretKey);
    System.out.println(decryptedString);
}
 }

如果我为加密和解密独立运行 Angular 和 java,这两个代码都可以正常工作。但是当我使用 angular 加密并使用 java 解密时,它会给出错误:

解密时出错:javax.crypto.BadPaddingException:给定的最终块未正确填充

现在我的问题是 angular 和 java 中的填充存在差异。在 Angular 中是 Pkcs7,在 Java 中是 Pkcs5,但是这个链接 Padding 说两者都是一样的,为什么会出现这个错误。请帮帮我

【问题讨论】:

    标签: java angular aes cryptojs


    【解决方案1】:

    我生成密钥的代码在 java 中生成了错误的密钥。所以我把它改成了:

     Key key = new SecretKeySpec(key.getBytes("UTF-8"),"AES" );
    

    现在可以正常使用了

    【讨论】:

    • 顺便说一句,我希望这只是为了学习,你明白拥有静态密钥和 iv 是一个非常糟糕的主意
    • @gusto2 是的,这只是为了了解我将使用动态密钥和 iv。
    • @Rider,你能告诉我你是如何在 Angular 和 Java 端实现加密和解密的吗?
    【解决方案2】:

    由于 Javascript 和 Java 库的不同,这有点问题。

    请按照以下对我有用的代码,我成功地能够在 Java 中加密数据并在 Java 中解密。

    import java.security.Key;
    import java.util.Base64;
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Crypt {
    
        private static final String ALGO = "AES"; // Default uses ECB PKCS5Padding
    
        public static String encrypt(String Data, String secret) throws Exception {
            Key key = generateKey(secret);
            Cipher c = Cipher.getInstance(ALGO);
            c.init(Cipher.ENCRYPT_MODE, key);
            byte[] encVal = c.doFinal(Data.getBytes());
            String encryptedValue = Base64.getEncoder().encodeToString(encVal);
            return encryptedValue;
        }
    
        public static String decrypt(String strToDecrypt, String secret) {
    
            try {
                Key key = generateKey(secret);
                Cipher cipher = Cipher.getInstance(ALGO);
                cipher.init(Cipher.DECRYPT_MODE, key);
                return new String(cipher.doFinal(Base64.getDecoder().decode(strToDecrypt)));
            } catch (Exception e) {
                System.out.println("Error while decrypting: " + e.toString());
            }
            return null;
        }
    
        private static Key generateKey(String secret) throws Exception {
            byte[] decoded = Base64.getDecoder().decode(secret.getBytes());
            Key key = new SecretKeySpec(decoded, ALGO);
            return key;
        }
    
        public static String decodeKey(String str) {
            byte[] decoded = Base64.getDecoder().decode(str.getBytes());
            return new String(decoded);
        }
    
        public static String encodeKey(String str) {
            byte[] encoded = Base64.getEncoder().encode(str.getBytes());
            return new String(encoded);
        }
    
        public static void main(String a[]) throws Exception {
            /*
             * Secret Key must be in the form of 16 byte like,
             *
             * private static final byte[] secretKey = new byte[] { ‘m’, ‘u’, ‘s’, ‘t’, ‘b’,
             * ‘e’, ‘1’, ‘6’, ‘b’, ‘y’, ‘t’,’e’, ‘s’, ‘k’, ‘e’, ‘y’};
             *
             * below is the direct 16byte string we can use
             */
            String secretKey = "mustbe16byteskey";
            String encodedBase64Key = encodeKey(secretKey);
            System.out.println("EncodedBase64Key = " + encodedBase64Key); // This need to be share between client and server
    
            // To check actual key from encoded base 64 secretKey
            // String toDecodeBase64Key = decodeKey(encodedBase64Key);
            // System.out.println("toDecodeBase64Key = "+toDecodeBase64Key);
            String toEncrypt = "Please encrypt this message!";
            System.out.println("Plain text = " + toEncrypt);
    
            // AES Encryption based on above secretKey
            String encrStr = Crypt.encrypt(toEncrypt, encodedBase64Key);
            System.out.println("Cipher Text: Encryption of str = " + encrStr);
    
            // AES Decryption based on above secretKey
            String decrStr = Crypt.decrypt(encrStr, encodedBase64Key);
            System.out.println("Decryption of str = " + decrStr);
        }
    }
    
    <!DOCTYPE html>
    <html>
    
    <body>
        <h2>Java JavaScript Encryption & Decryption</h2>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
        <script type="text/javascript">
            console.log('crypto - js’, CryptoJS);
            var encryptedBase64Key = 'bXVzdGJlMTZieXRlc2tleQ==’;
            var parsedBase64Key = CryptoJS.enc.Base64.parse(encryptedBase64Key);
            var encryptedData = null;
            {
                // Encryption process
                var plaintText = "Please encrypt this message!”;
                // console.log( "plaintText = " + plaintText );
                // this is Base64-encoded encrypted data
                encryptedData = CryptoJS.AES.encrypt(plaintText, parsedBase64Key, {
                    mode: CryptoJS.mode.ECB,
                    padding: CryptoJS.pad.Pkcs7
                });
                console.log("encryptedData = " + encryptedData);
            }
            {
                // Decryption process
                var encryptedCipherText = 'U2WvSc8oTur1KkrB6VGNDmA3XxJb9cC+T9RnqT4kD90=’; // or encryptedData;
                var decryptedData = CryptoJS.AES.decrypt(encryptedCipherText, parsedBase64Key, {
                    mode: CryptoJS.mode.ECB,
                    padding: CryptoJS.pad.Pkcs7
                });
                // console.log( "DecryptedData = " + decryptedData );
                // this is the decrypted data as a string
                var decryptedText = decryptedData.toString(CryptoJS.enc.Utf8);
                console.log("DecryptedText = " + decryptedText);
            }
        </script>
    </body>
    
    </html>
    

    【讨论】:

      猜你喜欢
      • 2020-12-03
      • 2013-10-12
      • 1970-01-01
      • 2013-08-24
      • 2019-04-18
      • 2022-10-16
      • 2011-07-14
      • 2013-12-17
      相关资源
      最近更新 更多