I'm trying to create in encryption in C# where the decryption will be in javascript. (CryptoJS)
I'm running .net 4.5 framework
Here is the sample code in javascript for decryption
var sample = CryptoJS.AES.decrypt(text, secret);
var decryptedText = sample.toString(CryptoJS.enc.Utf8);
Related
I have an asymmetric RSA key pair stored in two separate files. I want to generate a new symmetric key and encrypt it with public RSA key in my postbuild.js GULP script, so the user cannot access it. Then I want to send it to the C# server, where it would be decrypted and used.
I use the following JavaScript code in Node.js for encryption:
const generateAndEncryptKey = () => {
const symmetricKey = crypto.randomBytes(32);
const publicKey = fs.readFileSync("pubkey.pem", "utf8");
const encryptedSymmetricKey = crypto.publicEncrypt({
key: publicKey,
padding: crypto.constants.RSA_PKCS1_OAEP_PADDING,
oaepHash: "sha256",
}, Buffer.from(symmetricKey)).toString("base64");
return encryptedSymmetricKey;
}
The above code somehow works and generates a base64 string that I later send to the server. I'm not sure if this is the correct way to do this.
But I'm unable to find a way to decrypt this string in C#. I tried to use the BouncyCastle library and the following code:
public string DecryptKey(string encryptedKey) {
var privateKey = #"-----BEGIN RSA PRIVATE KEY-----
...shortened...
-----END RSA PRIVATE KEY-----";
var bytesToDecrypt = Convert.FromBase64String(encryptedKey);
var decryptEngine = new Pkcs1Encoding(new RsaEngine());
using (var txtreader = new StringReader(privateKey)) {
AsymmetricCipherKeyPair keyPair = (AsymmetricCipherKeyPair)new PemReader(txtreader).ReadObject();
decryptEngine.Init(false, keyPair.Private);
}
var decrypted = Encoding.UTF8.GetString(decryptEngine.ProcessBlock(bytesToDecrypt, 0, bytesToDecrypt.Length));
return decrypted;
}
But the ProcessBlock method always throws an InvalidCipherTextException "unknown block type".
Can someone help me to find out what am I doing wrong or point me to another better way of achieving this?
Decryption with the C# code fails because in the NodeJS code OAEP/SHA256 is used as padding and in the C# code PKCS#1 v1.5 padding. For decryption to work, both paddings must be identical. The padding in the C# code can be adapted to that of the NodeJS code as follows:
var decryptEngine = new OaepEncoding(new RsaEngine(), new Sha256Digest());
Also, the decrypted key must not be UTF-8 decoded as this corrupts the data. Either it is returned as byte[], or if conversion to a string is desired, a suitable binary-to-text encoding such as Base64 or hex must be used.
With these changes decryption works in the C# code.
I found core library and Now I can encrypt and decrypt on Jsp
var encryptedpassword=CryptoJS.AES.encrypt(password,'abcd');
var ciphertext = encryptedpassword.ciphertext.toString(CryptoJS.enc.Base64);
console.log(ciphertext);
and decrypted using following
var decryptedpassword=CryptoJS.AES.decrypt(encryptedpassword,'abcd');
var plaintext = decryptedpassword.toString(CryptoJS.enc.Utf8);
console.log(plaintext);
My problem now is I want to decrypt the encrypted password on java side can some one tell me how can I do it?
i need to decript my secret string generated from open_ssl php function with javascript.
I'm trying to decrypt string generated from php with cryptoJS
PHP FUNCTION
$encData = openssl_encrypt(utf8_encode($pure_string), 'DES-EDE3',$encryption_key , OPENSSL_RAW_DATA);
$session['chip'] = base64_encode($encData);
JAVASCRIPT FUNCTION
var keyHex = CryptoJS.enc.Utf8.parse(secretkey);
// direct decrypt ciphertext
var decrypted = CryptoJS.DES.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(secretText)
}, keyHex, {
mode: CryptoJS.mode.ECB
});
console.info('decrypted :', decrypted);
var plaintext = decrypted.toString(CryptoJS.enc.Utf8);
console.info('plaintext :', plaintext);
But nothing to do, i'm not able to get right result.
I think that problem happen because php use EDE3 mode, and i haven't found any way to use that mode with cryptoJS.
Any suggestion or any other sample to decrypt DES-EDE3 string?
Thank you!
I am trying to encrypt a message from client and decrypt it on the server. I put the AES key and iv in users cookies.
The problem is that the encrypted string from Crypto.js is G0eNQap/h6u+7566MTOH3w==, and the encrypted string from .NET is F7RemlJeNBhcaZ/FjCK4xw==. It has the same length, but not the same value.
I gues I am doing something wrong with encoding. Could you point out the mistake? Thanks in advance.
Crypto.js
var communicationKey = CryptoJS.enc.Base64.parse(getCookie("SessionKey"));
var communicationIV = CryptoJS.enc.Base64.parse(getCookie("IV"));
var encrypted = CryptoJS.AES.encrypt("Message", communicationKey, {
iv: communicationIV,
mode: CryptoJS.mode.CFB
});
console.log("Result: " + CryptoJS.enc.Base64.stringify(encrypted.ciphertext));
.NET:
string key = context.Cookies["SessionKey"].Value;
newUser.UserKey = Convert.FromBase64String(key);
string iv = context.Cookies["IV"].Value;
newUser.InitializationVector = Convert.FromBase64String(iv);
byte[] encryptedMessage = EncryptStringToBytes_Aes("Message", source.UserKey, source.InitializationVector);
In your js code you are using CryptoJS.mode.CFB.
If your EncryptStringToBytes_Aes is exact copy of MSDN sample - then it uses CBC AES encryption mode (it is default for AESManaged).
So you have to change either js or C# code for both of them use the same encryption mode.
I try to decrypt an cipher with AES in GCM mode with the SJCL library in Javascript (from within CasperJS).
When I execute the code below the error I receive is:
error: TypeError: 'undefined' is not a function (evaluating 'b.encrypt([0,
0,0,0])')
The code:
var masterkey = '39537496606860671661230109146651832357';
var cipher = 'Sa2Rk3bbdiaI7mO/';
var iv = '59804781381539321505720964105';
var authdata = '199590863504973848417387014842606357793';
var decff = sjcl.mode.gcm.decrypt(masterkey, cipher, iv, authdata);
console.log (decff);
As you can see, I am basically just calling the decrypt function as the SJCL docs told me to.
The encryption was done in python with this code: https://github.com/bozhu/AES-GCM-Python Wich I found is this thread: AES in GCM mode in Python
Is there anything special I have to consider when encrypting in one language and decrypting in another? Im afraid so...
Can I check somehow if the encryption information are valid AES/GCM?
Im not really sure how to proceed here since Im no JS or Python or encryption expert.
For background information:
I try to achieve a more or less secure encryption in pure python (so I can run it on Google App Engine) and the fitting decryption in pure JS.
Thanks for any help.
You cannot directly decrypt try converting your encrypted string, cypher, IV and auth data to bitArray.
const data = sjcl.mode.gcm.decrypt(cipherArray, encryptedBitArray, iv, authArray, 128);
Here 128 is size you can use 256 as well.
Also try to append your IV with the encypted string.
const bitArray = sjcl.codec.base64.toBits(content);
const bitArrayCopy = bitArray.slice(0);
const ivdec = bitArrayCopy.slice(0, 4);
const encryptedBitArray = bitArray.slice(4);
var key = sjcl.codec.base64.toBits("2d73c1dd2f6a3c981afc7c0d49d7b58f");
let cipher = new sjcl.cipher.aes(key);