You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the dRowAudio JUCE module
  4. Copyright 2004-13 by dRowAudio.
  5. ------------------------------------------------------------------------------
  6. dRowAudio is provided under the terms of The MIT License (MIT):
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in all
  14. copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. ==============================================================================
  23. */
  24. #ifndef __DROWAUDIO_ENCRYPTEDSTRING_H__
  25. #define __DROWAUDIO_ENCRYPTEDSTRING_H__
  26. #if JUCE_MODULE_AVAILABLE_juce_cryptography
  27. //==============================================================================
  28. /**
  29. This class contains some simple methods to encrypt and decrypt Strings.
  30. This uses RSA encryption and can convert between a 64-bit encoded string
  31. and optionally Hex.
  32. This is a quick and useful way of encrypting passwords etc.
  33. @code
  34. RSAKey publicKey, privateKey;
  35. RSAKey::createKeyPair (publicKey, privateKey, 128);
  36. String encryptedString (EncryptedString::encrypt ("hello world!", publicKey.toString()));
  37. // encryptedString will now contain an encrypted base-64 string you can store
  38. String decryptedString (EncryptedString::decrypt (encryptedString, privateKey.toString()));
  39. // decryptedString equals "hello world!"
  40. @endcode
  41. */
  42. class EncryptedString
  43. {
  44. public:
  45. //==============================================================================
  46. /** Encrypts a String with a given public key.
  47. This will output a base-64 string you can store safely.
  48. @param stringToEncrypt The input string to encrypt.
  49. @param publicKey The public key created from RSAKey::createKeyPair().
  50. @param resultAsHex If this is true the result will be a base-16 hex
  51. string. If false, the defaul base-64 encoding is used.
  52. @returns The encrypted string.
  53. @see RSAKey
  54. */
  55. static String encrypt (const String& stringToEncrypt, const String& publicKey,
  56. bool resultAsHex = false);
  57. /** Decrypts a String that was encrypted using the encrypt method.
  58. @param encryptedString The input string to decrypt.
  59. @param privateKey The private key created from RSAKey::createKeyPair().
  60. This must be the private key partner of the public
  61. key that was used to encrypt the string.
  62. @param inputIsHex Make sure this is true if the resultAsHex option was
  63. used when encrypting the String.
  64. @returns The decrypted string.
  65. @see RSAKey
  66. */
  67. static String decrypt (const String& encryptedString, const String& privateKey,
  68. bool inputIsHex = false);
  69. private:
  70. //==============================================================================
  71. EncryptedString(); // don't instantiate this object, just use its static methods!
  72. ~EncryptedString();
  73. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EncryptedString);
  74. };
  75. #endif // JUCE_MODULE_AVAILABLE_juce_cryptography
  76. #endif // __DROWAUDIO_ENCRYPTEDSTRING_H__