Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中使用CryptoJS和AES加密的AES加密的不同输出_Java_Javascript_Aes_Appcelerator_Cryptojs - Fatal编程技术网

在Java中使用CryptoJS和AES加密的AES加密的不同输出

在Java中使用CryptoJS和AES加密的AES加密的不同输出,java,javascript,aes,appcelerator,cryptojs,Java,Javascript,Aes,Appcelerator,Cryptojs,我正在尝试使用AES加密和cryptoJS加密数据。 我想将加密数据传输到设备/服务器或从设备/服务器传输加密数据。但我从设备和服务器Java获得了不同的AES加密输出 以下是我的代码: 移动式侧面使用钛器械: var AES_File = require("aes"); var Pad_File = require("pad-nopadding-min"); function encrypt_data(message) { Titanium.API.info("encrypt_dat

我正在尝试使用AES加密和cryptoJS加密数据。 我想将加密数据传输到设备/服务器或从设备/服务器传输加密数据。但我从设备和服务器Java获得了不同的AES加密输出

以下是我的代码:

移动式侧面使用钛器械:

var AES_File = require("aes");
var Pad_File = require("pad-nopadding-min");

function encrypt_data(message) {
    Titanium.API.info("encrypt_data : "+message);

    var key = AES_File.CryptoJS.enc.Latin1.parse('0123456789abcdef');
    var iv = AES_File.CryptoJS.enc.Latin1.parse('fedcba9876543210');

    //var key = AES_File.CryptoJS.enc.Hex.parse('0123456789abcdef');
    //var iv = AES_File.CryptoJS.enc.Hex.parse('fedcba9876543210');

    var message = "soldier";
    var padMsg = padString(message);

    var encrypted = AES_File.CryptoJS.AES.encrypt(padMsg, key, { iv: iv, pad:    AES_File.CryptoJS.pad.NoPadding, mode: AES_File.CryptoJS.mode.CBC});

    Titanium.API.log("Encrypted: " + encrypted);
    Titanium.API.log("Encrypted text: " + encrypted.ciphertext);

    return encrypted;
};

function padString(source) {
    var paddingChar = ' ';
    var size = 16;
    var x = source.length % size;
    var padLength = size - x;

    for (var i = 0; i < padLength; i++) source += paddingChar;

    return source;
}

exports.encrypt_data = encrypt_data;
服务器上的我的Java代码:

import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.*;

public class AesCipher {

    private static final String algorithm = "AES/CBC/NoPadding";

    private static final byte[] keyValue = new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private static final byte[] ivValue = new byte[] { 'f', 'e', 'd', 'c', 'b', 'a', '9', '8', '7', '6', '5', '4', '3', '2', '1', '0' };

    private static final IvParameterSpec ivspec = new IvParameterSpec(ivValue);
    private static final SecretKeySpec keyspec = new SecretKeySpec(keyValue, "AES");

    final protected static char[] hexArray = "0123456789abcdef".toCharArray();

    public static String encrypt(String Data) throws Exception {
        Cipher c = Cipher.getInstance(algorithm);
        c.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
        byte[] encVal = c.doFinal(Data.getBytes());
        String encryptedValue = new BASE64Encoder().encode(encVal);
        return encryptedValue;
    }

    public static String decrypt(String encryptedData) throws Exception {
        Cipher c = Cipher.getInstance(algorithm);
        c.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for ( int j = 0; j < bytes.length; j++ ) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

    private static String padString(String source) {
        char paddingChar = ' ';
        int size = 16;
        int x = source.length() % size;
        int padLength = size - x;

        for (int i = 0; i < padLength; i++)
        {
            source += paddingChar;
        }
        return source;
    }

    public static void main(String[] args) throws Exception {

        if (args.length < 1) {
            System.err.println("Usage: Encrypt [-d] <cleartext>");
            System.exit(1);
        }
        if (args[0].equals("-d")) {
            if (args.length != 2) {
                System.err.println("Usage: Encrypt [-d] <encrypted_text>");
                System.exit(1);
            }
            String encryptedText = args[1];
            System.out.println("encryptedText  is :"+encryptedText);
            String clearText = AesCipher.decrypt(encryptedText);
            System.out.println(clearText);

        } else {
            String clearText = args[0];
            String encryptedText = AesCipher.encrypt(padString(clearText));
            System.out.println(encryptedText);
        }
    }
 }
请帮帮我,我哪里做错了

钛合金SDK-3.5.0

目标设备-Androidabove 4.0和iOS 6.0以上


提前感谢。

请显示加密后两种方法得到的值。为什么要自己填充?由于您控制通信通道的两侧,因此请使用标准填充方案,例如PKCS 7.Server-j6dsmg2lfjy2rpn91gnw==移动端-lLTa74eiyE2fweqw7ouBjX8TMhR6xFogkyEpT9efHns=@Duncan:很抱歉最后的评论。使PKCS5填充起作用。非常感谢你帮助我。。。。