RSA encryption using modulus and exponent in PHP - c#

I want to encrypt password using modulus and exponent. I'm using .NET RSACryptoServiceProvider and in PHP I'm using seclib.
I have this piece of C# code and I want to do the exact same thing in PHP.
string exponent = "010001";
string modulus = "A1DFE8C5D4140E46610A4B2B0E0B77F7048A9386F50D5CF0B02C983C0138392F60C5B20C67285A37FABD2E270CB32FDBCB0DB1902EC74A0ADF41C623B3CF250EC4287BF9C3A0477BAEFD5803C07401C3E013BABEE8EE528EF765EB8EA4A82034DF7B0AC2BA9CDA6B0D6B87999D2AF0CA851C4FD6D62EF538EF73F1183658E0D810132DE2F7814C73A14E31B6472027A682EFD938EA6CC7C6E48DFD7F5145A870595E4B9A63909AF3AABD75C5148E276D1D329531CB2F27E45E0F09B076B08D56A310DE5BD2BF1EFD04D9A88BF1A1C0B74C60E04CE2EAB96E9B5BE7F6C42CA72E9D2970031550EB515C4F7EB5C245B3EF141E67864C536D54D3DA10CED3DF63F1";
string password = "testing";
byte[] encryptedPasswordBytes;
using (var rsaEncryptor = new RSACryptoServiceProvider())
{
var passwordBytes = Encoding.ASCII.GetBytes(password);
var rsaParameters = rsaEncryptor.ExportParameters(false);
rsaParameters.Exponent = HexStringToByteArray(exponent);
rsaParameters.Modulus = HexStringToByteArray(modulus);
rsaEncryptor.ImportParameters(rsaParameters);
encryptedPasswordBytes = rsaEncryptor.Encrypt(passwordBytes, false);
}
string encryptedPassword = Convert.ToBase64String(encryptedPasswordBytes);
Console.WriteLine(WebUtility.UrlEncode(encryptedPassword));
Console.ReadLine();
HexStringToByteArray method source code:
public static byte[] HexStringToByteArray(string hex)
{
int hexLen = hex.Length;
byte[] ret = new byte[hexLen / 2];
for (int i = 0; i < hexLen; i += 2)
ret[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return ret;
}
I'm trying to get same results in PHP using seclib and I think I'm not successfull.
$exponent = '010001';
$modulus = 'A1DFE8C5D4140E46610A4B2B0E0B77F7048A9386F50D5CF0B02C983C0138392F60C5B20C67285A37FABD2E270CB32FDBCB0DB1902EC74A0ADF41C623B3CF250EC4287BF9C3A0477BAEFD5803C07401C3E013BABEE8EE528EF765EB8EA4A82034DF7B0AC2BA9CDA6B0D6B87999D2AF0CA851C4FD6D62EF538EF73F1183658E0D810132DE2F7814C73A14E31B6472027A682EFD938EA6CC7C6E48DFD7F5145A870595E4B9A63909AF3AABD75C5148E276D1D329531CB2F27E45E0F09B076B08D56A310DE5BD2BF1EFD04D9A88BF1A1C0B74C60E04CE2EAB96E9B5BE7F6C42CA72E9D2970031550EB515C4F7EB5C245B3EF141E67864C536D54D3DA10CED3DF63F1';
$password = 'testing';
$rsa = new Crypt_RSA();
$rsa->loadKey(
array(
'e' => new Math_BigInteger($exponent,16),
'n' => new Math_BigInteger($modulus,16)
)
);
$encryptedPassword = urlencode(base64_encode($rsa->encrypt($password)));

$exponent = '010001';
$modulus = 'A1DFE8C5D4140E46610A4B2B0E0B77F7048A9386F50D5CF0B02C983C0138392F60C5B20C67285A37FABD2E270CB32FDBCB0DB1902EC74A0ADF41C623B3CF250EC4287BF9C3A0477BAEFD5803C07401C3E013BABEE8EE528EF765EB8EA4A82034DF7B0AC2BA9CDA6B0D6B87999D2AF0CA851C4FD6D62EF538EF73F1183658E0D810132DE2F7814C73A14E31B6472027A682EFD938EA6CC7C6E48DFD7F5145A870595E4B9A63909AF3AABD75C5148E276D1D329531CB2F27E45E0F09B076B08D56A310DE5BD2BF1EFD04D9A88BF1A1C0B74C60E04CE2EAB96E9B5BE7F6C42CA72E9D2970031550EB515C4F7EB5C245B3EF141E67864C536D54D3DA10CED3DF63F1';
$password = 'testing';
$rsa = new Crypt_RSA();
$modulus = new Math_BigInteger(($modulus), 16);
$exponent = new Math_BigInteger(($exponent), 16);
$rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
$rsa->setPublicKey(array('n' => $modulus, 'e' => $exponent));
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$encryptedPassword = $rsa->encrypt($password);

Related

Decrypt RSA encrypted value generated from .net in Android

I have gone through many posts here but did not find the right solution. I want to decrypt a value encrypted in c# .net from Android.
I have successfully decrypted in .net platform using the following code snippet
public static void Main()
{
string _privateKey = Base64Decode("myprivatekey");
var rsa = new RSACryptoServiceProvider();
string data = "198,47,144,175,154,47,194,175,242,41,212,150,220,177,198,161,236,36,197,62,18,111,21,244,232,245,90,234,195,169,141,195,139,199,131,163,26,124,246,50,102,229,73,148,18,110,170,145,112,237,23,123,226,135,158,206,71,116,9,219,56,96,140,19,180,192,80,29,63,160,43,127,204,135,155,67,46,160,225,12,85,161,107,214,104,218,6,220,252,73,252,92,152,235,214,126,245,126,129,150,49,68,162,120,237,246,27,25,45,225,106,201,251,128,243,213,250,172,26,28,176,219,198,194,7,202,34,210";
var dataArray = data.Split(new char[] { ',' });
byte[] dataByte = new byte[dataArray.Length];
for (int i = 0; i < dataArray.Length; i++)
{
dataByte[i] = Convert.ToByte(dataArray[i]);
}
rsa.FromXmlString(_privateKey);
var decryptedByte = rsa.Decrypt(dataByte, false);
Console.WriteLine(_encoder.GetString(decryptedByte));
}
Now I want to do the same process in Android app. Please can somebody guide me through this?
I have tried the following code but its throwing javax.crypto.IllegalBlockSizeException: input must be under 128 bytes exception
String modulusString = "hm2oRCtP6usJKYpq7o1K20uUuL11j5xRrbV4FCQhn/JeXLT21laKK9901P69YUS3bLo64x8G1PkCfRtjbbZCIaa1Ci/BCQX8nF2kZVfrPyzcmeAkq4wsDthuZ+jPInknzUI3TQPAzdj6gim97E731i6WP0MHFqW6ODeQ6Dsp8pc=";
String publicExponentString = "AQAB";
byte[] modulusBytes = Base64.decode(modulusString, DEFAULT);
byte[] exponentBytes = Base64.decode(publicExponentString, DEFAULT);
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger publicExponent = new BigInteger(1, exponentBytes);
RSAPrivateKeySpec rsaPubKey = new RSAPrivateKeySpec(modulus, publicExponent);
KeyFactory fact = KeyFactory.getInstance("RSA");
PrivateKey pubKey = fact.generatePrivate(rsaPubKey);
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] plainBytes = result.getBytes("UTF-16LE");
byte[] cipherData = cipher.doFinal(plainBytes);
String encryptedStringBase64 = Base64.decode(cipherData, DEFAULT).toString();

ECDH nodejs and C# key exchange

I've lost my self and I need help to go in the right direction :)
I have nodejs server that have to exchange some critical data with the server that is written in C#, so in that case, I want my data to be encrypted somehow. I was thinking about AES, and to safely exchange keys i want to use ECDH but I don't know how to make it work correctly... If I'm thinking right way I can make my C# "allice" side like this :
ECDiffieHellman alice = ECDiffieHellman.Create(ECCurve.NamedCurves.brainpoolP192t1);
var aliceKey = alice.PublicKey;
var ketyToExport = Convert.ToBase64String(aliceKey.ToByteArray());
//send ketyToExport to nodejs
//KEY FROM nodejs
var key1 = Convert.FromBase64String("BIzSJ1dmTXpSS8rqkVFISZ+vumhqXYMWfWoU5vB9GHhOtxxDInu/GZ92VJpqhrE3vw==").ToList();
var keyType = new byte[] { 0x45, 0x43, 0x4B, 0x31 };
var keyLength = new byte[] { 0x20, 0x00, 0x00, 0x00 };
key1.RemoveAt(0);
key1 = keyType.Concat(keyLength).Concat(key1).ToList();
byte[] bobKeyBytes = key1.ToArray();
//and I have a problem with that line bellow, I do not know how to make it work
var aliceSecret = alice.DeriveKeyMaterial(/*ECDiffieHellmanPublicKey bobKeyBytes*/);
And nodejs "Bob" side like this:
const crypto = require("crypto");
const bob = crypto.createECDH('brainpoolP192t1')
const bobKey = bob.generateKeys('base64');
var bobLength = Buffer.from(bobKey, 'base64').length;
//send bobkey to c#
//recive alicekey
var tmp = "RUNLUBgAAAAR9C7kO2o+vxNT/UBvvEuJHNdI8NfU4fUxUT431ER1q3kJbeUVHepoG5SWUM2NHj8="
var aliceKeyBuffer = Buffer.from(tmp, 'base64');
var aliceKey = Buffer.alloc(bobLength)
aliceKeyBuffer.copy(aliceKey, 1, 8);
aliceKey[0] = 4;
bob.computeSecret(aliceKey);
//create aes
//get mesage and iv ...
Okay so I've made some adjustments to all of that but right now I don't know what to do about this line how to make it work...
var aliceSecret = alice.DeriveKeyMaterial(/*ECDiffieHellmanPublicKey bobKeyBytes*/);
#BIG EDIT
I got help in ECDiffieHellmanPublicKey from ByteArray (using ECDiffieHellman NamedCurves)
and now I have another problem -_-
my node js code didn't change from above but c# looks like:
using (ECDiffieHellman alice = ECDiffieHellman.Create(ECCurve.NamedCurves.brainpoolP256r1))
{
var alicePublicKey = Convert.ToBase64String(alice.PublicKey.ToByteArray());
//NODEJS brainpoolP256r1 publickey
var key1 = Convert.FromBase64String("BASsbkule53ARqlMBA8hYysyyoRi3xGxGnSzIJ2fS5FlLniQD/zYiiGUVydmO/BBkQwVTUo5f4OMCxVNtQ/LuMQ=");
byte[] keyX = new byte[key1.Length / 2];
byte[] keyY = new byte[keyX.Length];
Buffer.BlockCopy(key1, 1, keyX, 0, keyX.Length);
Buffer.BlockCopy(key1, 1 + keyX.Length, keyY, 0, keyY.Length);
ECParameters parameters = new ECParameters
{
Curve = ECCurve.NamedCurves.brainpoolP256r1,
Q =
{
X = keyX,
Y = keyY,
},
};
byte[] derivedKey;
using (ECDiffieHellman bob = ECDiffieHellman.Create(parameters))
using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
{
derivedKey = alice.DeriveKeyFromHash(bobPublic, HashAlgorithmName.SHA256);
}
var aliceKey = Convert.ToBase64String(derivedKey);
byte[] encryptedMessage = null;
byte[] iv = null;
// Send(aliceKey, "Secret message", out encryptedMessage, out iv);
}
and it is working but it gives me different secret keys ...
out of bob.computeSecret(aliceKey) i got iIoH9aJoWf3666QQ6X+kj4iUKrk9j+hbRuXbhgs7YzM=
and out of alice.DeriveKeyFromHash(bobPublic, HashAlgorithmName.SHA256);
I got wJ7O4Hm2Jxs1FcLx6KaMmENvqdTQJPZ/YNSs1+MQDOQ=
if I'm thinking correctly they should be equal. am I thinking wrong?
#EDIT DONE !!
So this adding this code on end of js file gave me what I needed.
const hash = crypto.createHash('sha256');
var tt = bob.computeSecret(aliceKey);
hash.update(tt);
console.log(hash.digest('base64'));
##SOLUTION##
C#
class Program
{
static void Main(string[] args)
{
using (ECDiffieHellman alice = ECDiffieHellman.Create(ECCurve.NamedCurves.brainpoolP256r1))
{
var alicePublicKey = Convert.ToBase64String(alice.PublicKey.ToByteArray());
//send alicePublicKey
var nodejsKey = ""; //NODEJS brainpoolP256r1 publickey base64
byte[] nodejsKeyBytes= Convert.FromBase64String(nodejsKey);
var aliceKey = Convert.ToBase64String(getDeriveKey(nodejsKeyBytes,alice));
byte[] encryptedMessage = null;
byte[] iv = null;
// Send(aliceKey, "Secret message", out encryptedMessage, out iv);
}
}
static byte[] getDeriveKey(byte[] key1, ECDiffieHellman alice)
{
byte[] keyX = new byte[key1.Length / 2];
byte[] keyY = new byte[keyX.Length];
Buffer.BlockCopy(key1, 1, keyX, 0, keyX.Length);
Buffer.BlockCopy(key1, 1 + keyX.Length, keyY, 0, keyY.Length);
ECParameters parameters = new ECParameters
{
Curve = ECCurve.NamedCurves.brainpoolP256r1,
Q =
{
X = keyX,
Y = keyY,
},
};
byte[] derivedKey;
using (ECDiffieHellman bob = ECDiffieHellman.Create(parameters))
using (ECDiffieHellmanPublicKey bobPublic = bob.PublicKey)
{
return derivedKey = alice.DeriveKeyFromHash(bobPublic, HashAlgorithmName.SHA256);
}
}
}
NODEJS
const crypto = require("crypto");
const bob = crypto.createECDH('brainpoolP256r1')
bob.generateKeys();
const bobKey = bob.getPublicKey('base64');
var bobLength = Buffer.from(bobKey, 'base64').length;
//send bobkey to c#
//recive alicekey
var alicePublicKey = "RUNLUCAAAAB/xP7JhSIhYIYAijyC2zHu7obB5CwfK/ynQPxcRAIhBI6OLRRcHyPo61AhfSZN3qA2vGDfWO2mrdWWvqqhVaDf";
var aliceKeyBuffer = Buffer.from(alicePublicKey, 'base64');
var aliceKey = Buffer.alloc(bobLength)
aliceKeyBuffer.copy(aliceKey, 1, 8);
aliceKey[0] = 4;
const hash = crypto.createHash('sha256');
var tt = bob.computeSecret(aliceKey);
var bobSecretKey = hash.update(tt).digest('base64');
big thanks for #bartonjs and #Maarten Bodewes
The above answer helped me a lot. I am using secp521r1/nistP521 to do the key generation in both NodeJS and C#. In my case, a call to alice.PublicKey.ToByteArray() would result in an exception:
Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
at System.Security.Cryptography.ECDiffieHellmanImplementation.ECDiffieHellmanSecurityTransforms.ECDiffieHellmanSecurityTransformsPublicKey.ToByteArray()
According to an issue logged with the dotnet runtime team here:
ECDiffieHellmanPublicKey.ToByteArray() doesn't have a (standards-)defined export format. The version used by ECDiffieHellmanPublicKeyCng is Windows-specific, and only works for NIST P-256 (secp256r1), NIST P-384 (secp384r1), and NIST P-521 (secp521r1).
To create a copy of a key you should use the ExportParameters() method to obtain the rich ECParameters object; which can then be sent through ECDiffieHellman.Create(ECParameters) and the PublicKey property of that object be read to get back a second instance of ECDiffieHellmanPublicKey.
Assuming I send my X and Y from Alice to Bob (I used hex instead of base64), the corresponding process to arrive at the same shared secret on the NodeJS side looks like this:
// Alice's public key X, Y coordinates from DotNet Core
const aliceHexX = '00248F624728B17196B22005742C13D80D3DFF75BCA74AF865195E5A29F41C013653B0931BC544245402EDD7922F38692F38DCCF780CF1A9E27D3CFB5E09E53883C0';
const aliceHexY = '0043517F3B2EF33ED70EFA2BC4163E9B99558A7C2ECB7C659A12EA4024633CFA8B9FC997F0A42D30759B4280FDADC13A67A3E7BB0227047C907FAAE92E7716E8A10D';
const alicePublicKeyXBytes = Buffer.from(aliceHexX, 'hex');
const alicePublicKeyYBytes = Buffer.from(aliceHexY, 'hex');
const alicePublicKey = Buffer.concat([Buffer.from([0x04]), alicePublicKeyXBytes, alicePublicKeyYBytes])
const bobSecret = bob.computeSecret(alicePublicKey);
const hash = crypto.createHash('sha256');
hash.update(bobSecret);
const sharedSecret = hash.digest('hex');
console.log(`Shared Secret: ${sharedSecret.toString('hex')}`);

Converting RSA encryption code from C# to php

I have c# code for encryption. And I need to convert it with same logic in PHP.
// Public key to be used for encryption
String publicKey="yxnxuhj2322i23k232sasas123121";
// Sample data to be encrypted
String data="test";
//Encryption logic
CspParameters cspParams = new CspParameters { ProviderType = 1 };
RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);
rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));
byte[] plainBytes = Encoding.UTF8.GetBytes(data);
byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);
string encryptedString = Convert.ToBase64String(encryptedBytes);
PHP Code:
include('Crypt/RSA.php');
$a = 'yxnxuhj2322i23k232sasas123121';
$a = base64_decode($a);
$rsa= parseCSBBlob($a);
$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
define('CRYPT_RSA_PKCS15_COMPAT', true);
$user=utf8_encode("test");
$enc_user= $rsa->encrypt($user);
$enc_user=base64_encode($enc_user);
function parseCSBBlob($str) {
// from https://msdn.microsoft.com/en-us/library/windows/desktop/aa387453(v=vs.85).aspx
extract(unpack('atype/aversion/vreserved/Valgo', $str));
if (ord($type) != 6) { // 6 == PUBLICKEYBLOB
return false;
}
//https://msdn.microsoft.com/en-us/library/windows/desktop/aa375549(v=vs.85).aspx
if ($algo != 0x0000a400) { // 0x0000a400 == CALG_RSA_KEYX
return false;
}
$str = substr($str, 8); // aavV
extract(unpack('Vmagic/Vbitlen/Vpubexp', $str));
if ($magic != 0x31415352) { // RSA1
return false;
}
$str = substr($str, 12); // VVV
if (strlen($str) != $bitlen / 8) {
return false;
}
$str = strrev($str);
$rsa = new Crypt_RSA();
$rsa->loadKey(array(
'e' => new Math_BigInteger($pubexp, 256),
'n' => new Math_BigInteger($str, 256)
));
return $rsa;
}
I have used the parseCSBBlob method from other stackoverflow post. But it wont work. I am getting encryption from this code but it is not get decrypted in C#. I could not get the ImportCspBlob method in PHP. Encryption should be done in PHP and decryption is done in C#.

Java equivalent of .NET RSACryptoServiceProvider with SHA-1

I have the following data signing code in C#
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
string PrivateKeyText = "<RSAKeyValue><Modulus>....</D></RSAKeyValue>";
rsa.FromXmlString(PrivateKeyText);
string data = "my data";
byte[] SignedByteData = rsa.SignData(Encoding.UTF8.GetBytes(data), new SHA1CryptoServiceProvider());
and I want reproduce the same code in Java (Android):
String modulusElem = "...";
String expElem = "...";
byte[] expBytes = Base64.decode(expElem, Base64.DEFAULT);
byte[] modulusBytes = Base64.decode(modulusElem, Base64.DEFAULT);
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger exponent = new BigInteger(1, expBytes);
try {
KeyFactory factory = KeyFactory.getInstance("RSA");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
String data = "my data";
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] hashedData = md.digest(data.getBytes("UTF-8"));
RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modulus, exponent);
PublicKey publicKey = factory.generatePublic(pubSpec);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] SignedByteData = cipher.doFinal(hashedData);
} catch (Exception e){
}
But I get mismatched output byte arrays. Where am I wrong and What should be the transformation used in Cipher.getInstance(...) ?
Use Signature.getInstance("SHA1withRSA"). Encryption is not the same as signature generation. Different padding mechanisms for one.
Update by Afshin
Complete solution. Note the use of the private exponent, ie <D>, rather the public <Exponent>
String modulusElem = "...";
String dElem = "...";
byte[] modulusBytes = Base64.decode(modulusElem, Base64.DEFAULT);
byte[] dBytes = Base64.decode(dElem, Base64.DEFAULT);
BigInteger modulus = new BigInteger(1, modulusBytes);
BigInteger d = new BigInteger(1, dBytes);
String data = "my data";
try {
Signature signature = Signature.getInstance("SHA1withRSA");
KeyFactory factory = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(modulus, d);
PrivateKey privateKey = factory.generatePrivate(privateKeySpec);
signature.initSign(privateKey);
signature.update(data.getBytes("UTF-8"));
byte[] SignedByteData = signature.sign();
} catch(Exception e) {
e.printStackTrace();
}

TripleDES implementation in Javascript different comparing with C#

I need to replicate the following C# method to encrypt some text from Javascript. Currently I am using Crypto JS, but the output from JS is not equals to the C# output.
const string EncryptKey = "hello";
private static String getHexStringFromArray(byte[] arr) {
StringBuilder sBuilder = new StringBuilder();
for (int i = 0; i < arr.Length; i++) {
sBuilder.Append(arr[i].ToString("x2"));
}
return sBuilder.ToString();
}
public void Encrypt(string toEncrypt, bool useHashing) {
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
string key = EncryptKey;
if (useHashing) {
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
} else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
Console.WriteLine("hexadecimal key: " + getHexStringFromArray(keyArray));
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray =
cTransform.TransformFinalBlock(toEncryptArray, 0,
toEncryptArray.Length);
tdes.Clear();
Console.WriteLine("hexadecimal encrypted: " + getHexStringFromArray(resultArray));
//Return the encrypted data into unreadable string format
string test = Convert.ToBase64String(resultArray, 0, resultArray.Length);
Console.WriteLine("Output: " + test);
}
The output for Encrypt("password", true) is:
hexadecimal key: 5d41402abc4b2a76b9719d911017c592
hexadecimal encrypted: 069c44845e907b346b9d82d1d553f391
Output: BpxEhF6QezRrnYLR1VPzkQ==
Now, the Javascript implementation (please, ignore the global variables):
window.text = "password";
window.key = "hello";
var useHashing = true;
if (useHashing){
key = CryptoJS.MD5(key).toString();
}
window.options = {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
};
window.textWordArray = CryptoJS.enc.Utf8.parse(text);
window.keyHex = CryptoJS.enc.Hex.parse(key);
console.log('hexadecimal key: ' + keyHex);
window.encrypted = CryptoJS.TripleDES.encrypt(textWordArray, keyHex, options);
var base64String = encrypted.toString();
console.log('base64: ' + base64String);
window.decrypted = CryptoJS.TripleDES.decrypt( {
ciphertext: CryptoJS.enc.Base64.parse(base64String)
}, keyHex, options);
console.log('decrypted: ' + decrypted.toString(CryptoJS.enc.Utf8));
Produces this result:
hexadecimal key: 5d41402abc4b2a76b9719d911017c592
base64: BK5f0AhEuUl9pYEy2Mliyw==
Which is different from the C# implementation.
Here you can find the Javascript code.
Any help?
TripleDES requires 24-byte key (k1 + k2 + k3). Your key is only 16-byte. And .NET auto completes with k3 = k1. But Javascript does not, k3 = 0. Please modify the key:
if (useHashing){
key = CryptoJS.MD5(key).toString();
var k1 = key.substring(0, 16);
key = key + k1;
}
Here is Decrypter using 3DES-ECB of Forge js.
Since I couldn't find forge js solution adding one so others can use it.
var md = forge.md.md5.create();
md.update(window.key);
var key = md.digest().getBytes();
//3DES-ECB requires 24byte of data and key returned from md5 is 16byte
var k1 = key.substring(0, 8);
var key1 = key + key1;
var input = forge.util.createBuffer(forge.util.decode64(window.text));
var decTer = forge.cipher.createDecipher('3DES-ECB', key1);
decTer.start();
decTer.update(input);
return decTer.output.getBytes();
var message = "information";
var key = "t8g5av9Z0IsZ77tyox9H19Rb"; //length=22
var iv = "OjgLqBur"; //length=22
let cipher = CryptoJS.TripleDES.encrypt(message, CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv),
mode: CryptoJS.mode.CBC
});
let decrypt = CryptoJS.TripleDES.decrypt(cipher, CryptoJS.enc.Utf8.parse(key), {
iv: CryptoJS.enc.Utf8.parse(iv)
});
console.log(cipher.toString());
console.log(decrypt.toString(CryptoJS.enc.Utf8));

Categories

Resources