Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "sicrypto.h"
- SICrypto::SICrypto(QObject *parent) :
- QObject(parent)
- {
- }
- /*
- CheckPassphraseStrength
- Parameters:
- QString passphrase - The passphrase to be examined
- Return Value:
- qint32 - A value between 0 and 500 that indicates the strengt of the passphrase.
- */
- qint32 SICrypto::CheckPassphreaseStrength(QString passphrase) {
- QString existing (""); // Placeholder for existing characters
- QString loweralpha ("abcdefghijklmnopqrstuvwxyz"); // All lower alpha characters
- QString upperalpha ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // All upper alpha characters
- QString num ("0123456789"); // All numeric characters
- QString commonspecials ("!?._-,"); // All "common" special characters
- qint32 pp_len=0; // Passphrase total length counter
- qint32 unique=0; // Unique character counter
- qint32 la=0; // Lower a-z counter
- qint32 ua=0; // Upper A-Z counter
- qint32 nm=0; // Numeric counter
- qint32 cs=0; // Common specials counter
- qint32 ucs=0; // Uncommon specials counter
- // Count all measured characteristics
- for(qint32 i=0;i<passphrase.length();i++) {
- if(!existing.contains(passphrase.at(i))){
- unique++;
- existing.append(passphrase.at(i));
- }
- if(loweralpha.contains(passphrase.at(i))){la++;}
- else if(upperalpha.contains(passphrase.at(i))){ua++;}
- else if(num.contains(passphrase.at(i))){nm++;}
- else if(commonspecials.contains(passphrase.at(i))){cs++;}
- else {ucs++;}
- pp_len++;
- }
- // Weight the measured characteristics
- qint32 securityIndicator=0;
- securityIndicator+=unique*4; // Unique = 3
- securityIndicator+=la*2; // Lower alpha = 2
- securityIndicator+=ua*3; // Upper alpha = 3
- securityIndicator+=nm*1; // Numbers = 1
- securityIndicator+=cs*4; // Common specials = 4
- securityIndicator+=ucs*5; // Uncommon specials = 5
- securityIndicator-=40; /* Make the first 42 "points" useless
- (equals a password of 6 numeric chars) */
- securityIndicator+=pp_len*7; /* Weight the length of the password 7 times
- more than adding a single digit (length is of great importance) :) */
- // Apply limits (currently 0 - 500)
- if(securityIndicator>500) securityIndicator=500;
- if(securityIndicator<0) securityIndicator=0;
- return securityIndicator;
- }
- QString SICrypto::HashSHA256(QString str){
- std::string digest;
- SHA256 hash;
- StringSource foo(str.toStdString(), true,
- new HashFilter(hash,
- new HexEncoder(
- new StringSink(digest)
- )
- )
- );
- return QString(digest.c_str());
- }
- QString SICrypto::SecByteBlockToQString(SecByteBlock sbb){
- std::string out;
- StringSource foo(sbb.BytePtr(), sbb.SizeInBytes(),true,
- new HexEncoder(
- new StringSink(out)
- )
- );
- return QString(out.c_str());
- }
- SecByteBlock SICrypto::QStringToSecByteBlock(QString qs){
- std::string out;
- StringSource foo(qs.toStdString().c_str(),qs.toStdString().size(),
- new HexDecoder(
- new StringSink(out)
- )
- );
- SecByteBlock sbb;
- sbb.Assign((byte*)out.c_str(),out.size());
- return sbb;
- }
- QString SICrypto::EncryptAES(QString pt,SIAesKey key) {
- std::string plain = pt.toStdString();
- std::string cipher;
- CBC_Mode< AES >::Encryption e;
- e.SetKeyWithIV(key.getKey(), key.getKey().SizeInBytes(), key.getIv());
- StringSource(plain, true,
- new StreamTransformationFilter(e,
- new StringSink(cipher)
- )
- );
- std::string cipher64;
- StringSource(cipher, true, new Base64Encoder(new StringSink(cipher64)));
- return QString(cipher64.c_str());
- }
- bool SICrypto::DecryptAES(QString ct,SIAesKey key,QString &out) {
- std::string cipher64 = ct.toStdString();
- std::string cipher;
- std::string plain;
- try
- {
- StringSource(cipher64, true, new Base64Decoder(new StringSink(cipher)));
- CBC_Mode< AES >::Decryption e;
- e.SetKeyWithIV(key.getKey(), key.getKey().SizeInBytes(), key.getIv());
- StringSource(cipher, true,
- new StreamTransformationFilter(e,
- new StringSink(plain)
- )
- );
- out = QString(plain.c_str());
- return true;
- }
- catch (CryptoPP::Exception const& e)
- {
- qDebug() << "CryptoPP::Exception caught:" << endl
- << e.what() << endl;
- return false;
- }
- }
- QString SICrypto::EncryptRSA(QString pt,SIRsaKey pbkey) {
- AutoSeededRandomPool rng;
- std::string cipher = "";
- std::string plain = pt.toStdString();
- RSAES_OAEP_SHA_Encryptor e( pbkey.GetPublic() );
- StringSource( plain, true,
- new PK_EncryptorFilter( rng, e,
- new StringSink( cipher )
- ) // PK_EncryptorFilter
- ); // StringSource
- std::string cipher64;
- StringSource(cipher, true, new Base64Encoder(new StringSink(cipher64)));
- return QString(cipher64.c_str());
- }
- bool SICrypto::DecryptRSA(QString ct,SIRsaKey pvkey,QString &pt){
- try
- {
- AutoSeededRandomPool rng;
- std::string cipher64 = ct.toStdString();
- std::string cipher, recovered;
- RSAES_OAEP_SHA_Decryptor d( pvkey.GetPrivate() );
- StringSource(cipher64, true, new Base64Decoder(new StringSink(cipher)));
- StringSource( cipher, true,
- new PK_DecryptorFilter( rng, d,
- new StringSink( recovered )
- ) // PK_DecryptorFilter
- ); // StringSource
- pt = recovered.c_str();
- return true;
- }
- catch (CryptoPP::Exception const& e)
- {
- qDebug() << "CryptoPP::Exception caught:" << endl
- << e.what() << endl;
- return false;
- }
- }
- QString SICrypto::SignRSA(QString message,SIRsaKey pbkey){
- AutoSeededRandomPool rng;
- std::string msg = message.toStdString();
- std::string signature;
- RSASS<PSSR, SHA1>::Signer signer(pbkey.GetPrivate());
- StringSource(msg, true,
- new SignerFilter(rng, signer,
- new StringSink(signature),
- true
- ) // SignerFilter
- ); // StringSource
- std::string signature64;
- StringSource(signature, true, new Base64Encoder(new StringSink(signature64)));
- return QString(signature64.c_str());
- }
- bool SICrypto::VerifyRSA(QString signature,SIRsaKey pbkey,QString &pt){
- try
- {
- AutoSeededRandomPool rng;
- std::string sig64 = signature.toStdString();
- std::string sig, recovered;
- StringSource(sig64, true, new Base64Decoder(new StringSink(sig)));
- RSASS<PSSR, SHA1>::Verifier verifier(pbkey.GetPublic());
- StringSource(sig, true,
- new SignatureVerificationFilter(
- verifier,
- new StringSink(recovered),
- SignatureVerificationFilter::THROW_EXCEPTION | SignatureVerificationFilter::PUT_MESSAGE
- ) // SignatureVerificationFilter
- ); // StringSource
- pt = recovered.c_str();
- return true;
- }
- catch (CryptoPP::Exception const& e)
- {
- qDebug() << "CryptoPP::Exception caught:" << endl
- << e.what() << endl;
- return false;
- }
- }
Add Comment
Please, Sign In to add comment