Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MSNCrypto.h"
- struct MSNPMSNG {
- unsigned long headerSize;
- unsigned long cryptMode;
- unsigned long cipherType;
- unsigned long hashType;
- unsigned long ivLength;
- unsigned long hashLength;
- unsigned long cipherLength;
- unsigned char ivBytes[8];
- unsigned char hashBytes[20];
- unsigned char cipherBytes[72];
- };
- MSNCrypto::MSNCrypto ( QObject *parent )
- : QObject ( parent )
- {
- }
- QString
- MSNCrypto::mbiEncrypt ( QString& ssoKey, QString& nonce )
- {
- QString magic1 = "WS-SecureConversationSESSION KEY HASH";
- QString magic2 = "WS-SecureConversationSESSION KEY ENCRYPTION";
- QString key1, key2, key3;
- QByteArray hash, des3hash;
- QCA::Base64 encoder(QCA::Encode);
- QCA::Base64 decoder(QCA::Decode);
- key1 = decoder.decodeString(ssoKey.toLatin1());
- qDebug() << "key1: " << key1;
- key2 = deriveKey(key1, magic1);
- qDebug() << "key2: " << key2;
- key3 = deriveKey(key1, magic2);
- qDebug() << "key3: " << key3;
- hash = createHMACSha1(key2, nonce);
- qDebug() << "hash: " << hash;
- qDebug() << "nonce data: " << nonce;
- qDebug() << "nonce len: " << nonce.size();
- nonce += QByteArray(8, '\x08');
- qDebug() << "nonce padded data: " << nonce;
- qDebug() << "nonce padded data len: " << nonce.size();
- QCA::InitializationVector iv;
- des3hash = createTripleDes(key3, nonce, iv);
- qDebug() << "des3hash: " << des3hash;
- des3hash += QByteArray(72 - des3hash.size(), '\x00');
- qDebug() << "des3hash len: " << des3hash.size();
- qDebug() << "sizeof p: " << sizeof(MSNPMSNG);
- MSNPMSNG blob;
- blob.headerSize = 28;
- blob.cryptMode = 1;
- blob.cipherType = 0x6603;
- blob.hashType = 0x8004;
- blob.ivLength = 8;
- blob.hashLength = 20;
- blob.cipherLength = 72;
- for(qint32 i = 0; i < iv.size(); ++i) {
- blob.ivBytes[i] = iv[i];
- }
- for(qint32 i = 0; i < hash.size(); ++i) {
- blob.hashBytes[i] = hash[i];
- }
- for(qint32 i = 0; i < des3hash.size(); ++i) {
- blob.cipherBytes[i] = des3hash[i];
- }
- QByteArray blobs(&blob, 128);
- }
- QString
- MSNCrypto::deriveKey ( QString& ssoKey, QString& magic )
- {
- QByteArray hash1, hash2, hash3, hash4, temp;
- QCA::SecureArray mmagic(magic.toLatin1());
- temp.append(mmagic.toByteArray());
- hash1 = createHMACSha1(ssoKey, temp);
- temp.clear();
- qDebug() << QCA::arrayToHex(hash1);
- temp.append(hash1);
- temp.append(mmagic.toByteArray());
- hash2 = createHMACSha1(ssoKey, temp);
- temp.clear();
- qDebug() << QCA::arrayToHex(hash2);
- temp.append(hash1);
- hash3 = createHMACSha1(ssoKey, temp);
- temp.clear();
- qDebug() << QCA::arrayToHex(hash3);
- temp.append(hash3);
- temp.append(mmagic.toByteArray());
- hash4 = createHMACSha1(ssoKey, temp);
- temp.clear();
- qDebug() << QCA::arrayToHex(hash4);
- temp.append(hash2);
- temp.append(hash4[0]);
- temp.append(hash4[1]);
- temp.append(hash3[2]);
- temp.append(hash4[3]);
- return temp;
- }
- QByteArray
- MSNCrypto::createHMACSha1(QString key, QString secret)
- {
- if(!QCA::isSupported("hmac(sha1)")) {
- qFatal("[Error] HMAC Sha1 not supported!");
- } else {
- QCA::MessageAuthenticationCode hmac("hmac(sha1)", QCA::SecureArray());
- QCA::SymmetricKey hmacKey(key.toLatin1());
- hmac.setup(hmacKey);
- hmac.update(secret.toLatin1());
- QByteArray final = hmac.final().toByteArray();
- return final;
- }
- return "";
- }
- QByteArray
- MSNCrypto::createTripleDes(QString key, QString secret, QCA::InitializationVector& iv)
- {
- if(!QCA::isSupported("tripledes-cbc")) {
- qFatal("triple des not support");
- } else {
- QCA::SymmetricKey desKey(key.toLatin1());
- QCA::InitializationVector tempIV(8);
- iv = tempIV;
- QCA::SecureArray data(secret.toLatin1());
- QCA::Cipher des3(QString("tripledes"), QCA::Cipher::CBC,
- QCA::Cipher::NoPadding, QCA::Encode, desKey, tempIV);
- QCA::SecureArray temp = des3.update(data);
- QCA::SecureArray hmmm = des3.final();
- if(!des3.ok()) {
- qFatal("Failed");
- }
- return temp.toByteArray();
- }
- return "";
- }
Add Comment
Please, Sign In to add comment