Guest User

Untitled

a guest
Dec 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.86 KB | None | 0 0
  1. #include "sicrypto.h"
  2.  
  3. SICrypto::SICrypto(QObject *parent) :
  4. QObject(parent)
  5. {
  6.  
  7. }
  8.  
  9. /*
  10. CheckPassphraseStrength
  11.  
  12. Parameters:
  13. QString passphrase - The passphrase to be examined
  14.  
  15. Return Value:
  16. qint32 - A value between 0 and 500 that indicates the strengt of the passphrase.
  17.  
  18. */
  19. qint32 SICrypto::CheckPassphreaseStrength(QString passphrase) {
  20.  
  21. QString existing (""); // Placeholder for existing characters
  22. QString loweralpha ("abcdefghijklmnopqrstuvwxyz"); // All lower alpha characters
  23. QString upperalpha ("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); // All upper alpha characters
  24. QString num ("0123456789"); // All numeric characters
  25. QString commonspecials ("!?._-,"); // All "common" special characters
  26.  
  27. qint32 pp_len=0; // Passphrase total length counter
  28. qint32 unique=0; // Unique character counter
  29. qint32 la=0; // Lower a-z counter
  30. qint32 ua=0; // Upper A-Z counter
  31. qint32 nm=0; // Numeric counter
  32. qint32 cs=0; // Common specials counter
  33. qint32 ucs=0; // Uncommon specials counter
  34.  
  35. // Count all measured characteristics
  36. for(qint32 i=0;i<passphrase.length();i++) {
  37. if(!existing.contains(passphrase.at(i))){
  38. unique++;
  39. existing.append(passphrase.at(i));
  40. }
  41. if(loweralpha.contains(passphrase.at(i))){la++;}
  42. else if(upperalpha.contains(passphrase.at(i))){ua++;}
  43. else if(num.contains(passphrase.at(i))){nm++;}
  44. else if(commonspecials.contains(passphrase.at(i))){cs++;}
  45. else {ucs++;}
  46. pp_len++;
  47. }
  48.  
  49. // Weight the measured characteristics
  50. qint32 securityIndicator=0;
  51. securityIndicator+=unique*4; // Unique = 3
  52. securityIndicator+=la*2; // Lower alpha = 2
  53. securityIndicator+=ua*3; // Upper alpha = 3
  54. securityIndicator+=nm*1; // Numbers = 1
  55. securityIndicator+=cs*4; // Common specials = 4
  56. securityIndicator+=ucs*5; // Uncommon specials = 5
  57. securityIndicator-=40; /* Make the first 42 "points" useless
  58. (equals a password of 6 numeric chars) */
  59. securityIndicator+=pp_len*7; /* Weight the length of the password 7 times
  60. more than adding a single digit (length is of great importance) :) */
  61.  
  62. // Apply limits (currently 0 - 500)
  63. if(securityIndicator>500) securityIndicator=500;
  64. if(securityIndicator<0) securityIndicator=0;
  65.  
  66. return securityIndicator;
  67.  
  68. }
  69.  
  70. QString SICrypto::HashSHA256(QString str){
  71.  
  72. std::string digest;
  73. SHA256 hash;
  74. StringSource foo(str.toStdString(), true,
  75. new HashFilter(hash,
  76. new HexEncoder(
  77. new StringSink(digest)
  78. )
  79. )
  80. );
  81. return QString(digest.c_str());
  82.  
  83. }
  84.  
  85. QString SICrypto::SecByteBlockToQString(SecByteBlock sbb){
  86.  
  87. std::string out;
  88. StringSource foo(sbb.BytePtr(), sbb.SizeInBytes(),true,
  89. new HexEncoder(
  90. new StringSink(out)
  91. )
  92. );
  93.  
  94. return QString(out.c_str());
  95.  
  96. }
  97.  
  98. SecByteBlock SICrypto::QStringToSecByteBlock(QString qs){
  99.  
  100. std::string out;
  101. StringSource foo(qs.toStdString().c_str(),qs.toStdString().size(),
  102. new HexDecoder(
  103. new StringSink(out)
  104. )
  105. );
  106. SecByteBlock sbb;
  107. sbb.Assign((byte*)out.c_str(),out.size());
  108.  
  109. return sbb;
  110.  
  111. }
  112.  
  113. QString SICrypto::EncryptAES(QString pt,SIAesKey key) {
  114.  
  115. std::string plain = pt.toStdString();
  116. std::string cipher;
  117.  
  118. CBC_Mode< AES >::Encryption e;
  119. e.SetKeyWithIV(key.getKey(), key.getKey().SizeInBytes(), key.getIv());
  120.  
  121. StringSource(plain, true,
  122. new StreamTransformationFilter(e,
  123. new StringSink(cipher)
  124. )
  125. );
  126.  
  127. std::string cipher64;
  128.  
  129. StringSource(cipher, true, new Base64Encoder(new StringSink(cipher64)));
  130.  
  131. return QString(cipher64.c_str());
  132.  
  133. }
  134.  
  135. bool SICrypto::DecryptAES(QString ct,SIAesKey key,QString &out) {
  136.  
  137. std::string cipher64 = ct.toStdString();
  138. std::string cipher;
  139. std::string plain;
  140.  
  141. try
  142. {
  143. StringSource(cipher64, true, new Base64Decoder(new StringSink(cipher)));
  144.  
  145. CBC_Mode< AES >::Decryption e;
  146. e.SetKeyWithIV(key.getKey(), key.getKey().SizeInBytes(), key.getIv());
  147.  
  148. StringSource(cipher, true,
  149. new StreamTransformationFilter(e,
  150. new StringSink(plain)
  151. )
  152. );
  153.  
  154. out = QString(plain.c_str());
  155.  
  156. return true;
  157.  
  158. }
  159. catch (CryptoPP::Exception const& e)
  160. {
  161. qDebug() << "CryptoPP::Exception caught:" << endl
  162. << e.what() << endl;
  163. return false;
  164. }
  165. }
  166.  
  167. QString SICrypto::EncryptRSA(QString pt,SIRsaKey pbkey) {
  168.  
  169. AutoSeededRandomPool rng;
  170.  
  171. std::string cipher = "";
  172. std::string plain = pt.toStdString();
  173.  
  174. RSAES_OAEP_SHA_Encryptor e( pbkey.GetPublic() );
  175.  
  176. StringSource( plain, true,
  177. new PK_EncryptorFilter( rng, e,
  178. new StringSink( cipher )
  179. ) // PK_EncryptorFilter
  180. ); // StringSource
  181.  
  182. std::string cipher64;
  183.  
  184. StringSource(cipher, true, new Base64Encoder(new StringSink(cipher64)));
  185.  
  186. return QString(cipher64.c_str());
  187.  
  188. }
  189.  
  190. bool SICrypto::DecryptRSA(QString ct,SIRsaKey pvkey,QString &pt){
  191.  
  192. try
  193. {
  194. AutoSeededRandomPool rng;
  195.  
  196. std::string cipher64 = ct.toStdString();
  197. std::string cipher, recovered;
  198.  
  199. RSAES_OAEP_SHA_Decryptor d( pvkey.GetPrivate() );
  200.  
  201. StringSource(cipher64, true, new Base64Decoder(new StringSink(cipher)));
  202.  
  203. StringSource( cipher, true,
  204. new PK_DecryptorFilter( rng, d,
  205. new StringSink( recovered )
  206. ) // PK_DecryptorFilter
  207. ); // StringSource
  208.  
  209. pt = recovered.c_str();
  210.  
  211. return true;
  212.  
  213. }
  214. catch (CryptoPP::Exception const& e)
  215. {
  216. qDebug() << "CryptoPP::Exception caught:" << endl
  217. << e.what() << endl;
  218. return false;
  219. }
  220. }
  221.  
  222. QString SICrypto::SignRSA(QString message,SIRsaKey pbkey){
  223.  
  224. AutoSeededRandomPool rng;
  225.  
  226. std::string msg = message.toStdString();
  227. std::string signature;
  228.  
  229. RSASS<PSSR, SHA1>::Signer signer(pbkey.GetPrivate());
  230.  
  231. StringSource(msg, true,
  232. new SignerFilter(rng, signer,
  233. new StringSink(signature),
  234. true
  235. ) // SignerFilter
  236. ); // StringSource
  237.  
  238. std::string signature64;
  239.  
  240. StringSource(signature, true, new Base64Encoder(new StringSink(signature64)));
  241.  
  242. return QString(signature64.c_str());
  243.  
  244. }
  245.  
  246. bool SICrypto::VerifyRSA(QString signature,SIRsaKey pbkey,QString &pt){
  247.  
  248. try
  249. {
  250. AutoSeededRandomPool rng;
  251.  
  252. std::string sig64 = signature.toStdString();
  253. std::string sig, recovered;
  254.  
  255. StringSource(sig64, true, new Base64Decoder(new StringSink(sig)));
  256.  
  257. RSASS<PSSR, SHA1>::Verifier verifier(pbkey.GetPublic());
  258.  
  259. StringSource(sig, true,
  260. new SignatureVerificationFilter(
  261. verifier,
  262. new StringSink(recovered),
  263. SignatureVerificationFilter::THROW_EXCEPTION | SignatureVerificationFilter::PUT_MESSAGE
  264. ) // SignatureVerificationFilter
  265. ); // StringSource
  266.  
  267. pt = recovered.c_str();
  268.  
  269. return true;
  270.  
  271. }
  272. catch (CryptoPP::Exception const& e)
  273. {
  274. qDebug() << "CryptoPP::Exception caught:" << endl
  275. << e.what() << endl;
  276. return false;
  277. }
  278. }
Add Comment
Please, Sign In to add comment