Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.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
从div中获取一个图像,并将其设置为JavaScript(chrome扩展)中的变量_Javascript_Image_Google Chrome Extension_Steganography - Fatal编程技术网

从div中获取一个图像,并将其设置为JavaScript(chrome扩展)中的变量

从div中获取一个图像,并将其设置为JavaScript(chrome扩展)中的变量,javascript,image,google-chrome-extension,steganography,Javascript,Image,Google Chrome Extension,Steganography,我正试图去隐藏一个图像来揭示秘密信息 我已经让它工作,但只有通过浏览包含信息的图像 如果您查看屏幕截图: 为了实现这一目标,我需要保存阿森纳徽章上的图像。然后使用“浏览”再次上载图像以使其正常工作。我想跳过浏览阶段,通过直接从div中抓取图像来自动创建 有什么想法吗 这是我的密码很乱很抱歉 window.onload = function() { // add action to the file input var input = document.getElementById('file')

我正试图去隐藏一个图像来揭示秘密信息

我已经让它工作,但只有通过浏览包含信息的图像

如果您查看屏幕截图:

为了实现这一目标,我需要保存阿森纳徽章上的图像。然后使用“浏览”再次上载图像以使其正常工作。我想跳过浏览阶段,通过直接从div中抓取图像来自动创建

有什么想法吗

这是我的密码很乱很抱歉

window.onload = function() {
// add action to the file input
var input = document.getElementById('file');
input.addEventListener('change', importImage);
};
var maxMessageSize = 1000;

$("#desteg").click(function()
{
decode();
});     


var importImage = function(e) {
var reader = new FileReader();

reader.onload = function(event) {
    // set the preview
    document.getElementById('preview').style.display = 'block';
    document.getElementById('preview').src = event.target.result;

    // wipe all the fields clean
    document.getElementById('pass').value = '';
    document.getElementById('ContentArea').innerHTML = '';

    // read the data into the canvas element
    var img = new Image();
    img.onload = function() {
        var ctx = document.getElementById('canvas').getContext('2d');
        ctx.canvas.width = img.width;
        ctx.canvas.height = img.height;
        ctx.drawImage(img, 0, 0);

        decode();
    };
    img.src = event.target.result;
};

reader.readAsDataURL(e.target.files[0]);
};

// encode the image and save it

// decode the image and display the contents if there is anything
var decode = function() {
var password = document.getElementById('pass').value;
var passwordFail = 'Password is incorrect or there is nothing here.';

// decode the message with the supplied password
var ctx = document.getElementById('canvas').getContext('2d');
var imgData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
var message = decodeMessage(imgData.data, sjcl.hash.sha256.hash(password));

// try to parse the JSON
var obj = null;
try {
    obj = JSON.parse(message);
} catch (e) {
    // display the "choose" view

    if (password.length > 0) {
        alert(passwordFail);
    }
}

// display the "reveal" view
if (obj) {

    // decrypt if necessary
    if (obj.ct) {
        try {
            obj.text = sjcl.decrypt(password, message);
        } catch (e) {
            alert(passwordFail);
        }
    }

    // escape special characters
    var escChars = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
        '"': '&quot;',
        '\'': '&#39;',
        '/': '&#x2F;',
        '\n': '<br/>'
    };
    var escHtml = function(string) {
        return String(string).replace(/[&<>"'\/\n]/g, function (c) {
            return escChars[c];
        });
    };
    document.getElementById('ContentArea').innerHTML = escHtml(obj.text);
}
};

// returns a 1 or 0 for the bit in 'location'
var getBit = function(number, location) {
return ((number >> location) & 1);
};

// sets the bit in 'location' to 'bit' (either a 1 or 0)
var setBit = function(number, location, bit) {
return (number & ~(1 << location)) | (bit << location);
};

// returns an array of 1s and 0s for a 2-byte number
var getBitsFromNumber = function(number) {
var bits = [];
for (var i = 0; i < 16; i++) {
   bits.push(getBit(number, i));
}
return bits;
};

// returns the next 2-byte number
var getNumberFromBits = function(bytes, history, hash) {
var number = 0, pos = 0;
while (pos < 16) {
    var loc = getNextLocation(history, hash, bytes.length);
    var bit = getBit(bytes[loc], 0);
    number = setBit(number, pos, bit);
    pos++;
}
return number;
};

// returns an array of 1s and 0s for the string 'message'
var getMessageBits = function(message) {
var messageBits = [];
for (var i = 0; i < message.length; i++) {
    var code = message.charCodeAt(i);
    messageBits = messageBits.concat(getBitsFromNumber(code));
}
return messageBits;
};

// gets the next location to store a bit
var getNextLocation = function(history, hash, total) {
var pos = history.length;
var loc = Math.abs(hash[pos % hash.length] * (pos + 1)) % total;
while (true) {
    if (loc >= total) {
        loc = 0;
    } else if (history.indexOf(loc) >= 0) {
        loc++;
    } else if ((loc + 1) % 4 === 0) {
        loc++;
    } else {
        history.push(loc);
        return loc;
    }
}
};

// encodes the supplied 'message' into the CanvasPixelArray 'colors'

// returns the message encoded in the CanvasPixelArray 'colors'
var decodeMessage = function(colors, hash) {
// this will store the color values we've already read from
var history = [];

// get the message size
var messageSize = getNumberFromBits(colors, history, hash);

// exit early if the message is too big for the image
if ((messageSize + 1) * 16 > colors.length * 0.75) {
    return '';
}

// exit early if the message is above an artificial limit
if (messageSize === 0 || messageSize > maxMessageSize) {
    return '';
}

// put each character into an array
var message = [];
for (var i = 0; i < messageSize; i++) {
    var code = getNumberFromBits(colors, history, hash);
    message.push(String.fromCharCode(code));
}

// the characters should parse into valid JSON
return message.join('');
};