WebCrypto GOST: CMS Enveloped/Encrypted data

The tool is intended for experimentation with data processing

Encrypt sample data

Recipient certificate

                        
Sender certificate

                        
Data for encryption
Мой дядя самых честных правил,
Когда не в шутку занемог,
Он уважать себя заставил
И лучше выдумать не мог.
Его пример другим наука;
Но, боже мой, какая скука
С больным сидеть и день и ночь,
Не отходя ни шагу прочь!
Какое низкое коварство
Полуживого забавлять,
Ему подушки поправлять,
Печально подносить лекарство,
Вздыхать и думать про себя:
Когда же черт возьмет тебя!
Enveloped data CMS PKCS#7 format

                    
                    
                    
                    
                    Encrypt data method
                    
var content = gostCrypto.coding.Chars.decode(message.textContent, 'utf-8');
var mode = encryptionType.value;
var promise, msg, senderKey, senderCert;
switch(mode) {
    case 'KEYAGREE':
        // Sender key and certificate required only for key agreement
        senderKey = new gostCrypto.asn1.PrivateKeyInfo(sender.textContent);
        senderCert = new gostCrypto.cert.X509(sender.textContent);
    case 'KEYTRANS':
        // Use recipient certificate for encryption
        msg = new gostCrypto.cms.EnvelopedDataContentInfo(); 
        var receiverCert = new gostCrypto.cert.X509(recipient.textContent);
        promise = msg.encloseContent(content, provider.value).then(function() {
            return msg.addRecipient(receiverCert, senderKey, senderCert);
        });
        break;
    case 'ES':
    case 'PBES':
        // Use secret key or password for encryption
        var keyPassword = mode === 'ES' ? gostCrypto.coding.Hex.decode(kek.textContent) :
                password.value;
        msg = new gostCrypto.cms.EncryptedDataContentInfo(); 
        promise =  msg.encloseContent(content, keyPassword, provider.value);
        break;
}
promise.then(function() {

    // Output encrypted data
    encryptedData.textContent = msg.encode('PEM');

}).catch(function(reason) {
    alert(reason.message);
});

Decrypt data

Decrypted data
                    
Decrypt data method
var encrypted = encryptedData.textContent;
var mode = encryptionType.value;
var promise, msg;
switch(mode) {
    case 'KEYAGREE':
    case 'KEYTRANS':
        // Use recipient key and certificate for decryption
        msg = new gostCrypto.cms.EnvelopedDataContentInfo(encrypted); 
        var receiverKey = new gostCrypto.asn1.PrivateKeyInfo(recipient.textContent);
        var receiverCert = new gostCrypto.cert.X509(recipient.textContent);
        promise = msg.getEnclosed(receiverKey, receiverCert);
        break;
    case 'ES':
    case 'PBES':
        // Use secret key or password for decryption
        var keyPassword = mode === 'ES' ? gostCrypto.coding.Hex.decode(kek.textContent) :
                password.value;
        msg = new gostCrypto.cms.EncryptedDataContentInfo(encrypted); 
        promise =  msg.getEnclosed(keyPassword);
        break;
}
promise.then(function(data) {

    // Decrypted result
    decryptedData.textContent = gostCrypto.coding.Chars.encode(data.content, 'utf-8');

}).catch(function(reason) {
    alert(reason.message);
});