crypt.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // Todo:
  23. // - Crypt/decrypt button in the userinterface
  24. // - Setting if crypto should be on by default
  25. // - Add a setting "Don´t encrypt files larger than xx because of performance reasons"
  26. // - Transparent decrypt/encrpt in filesystem.php. Autodetect if a file is encrypted (.encrypted extensio)
  27. // - Don't use a password directly as encryption key. but a key which is stored on the server and encrypted with the user password. -> password change faster
  28. // - IMPORTANT! Check if the block lenght of the encrypted data stays the same
  29. require_once('Crypt_Blowfish/Blowfish.php');
  30. /**
  31. * This class is for crypting and decrypting
  32. */
  33. class OC_Crypt {
  34. static $encription_extension='.encrypted';
  35. public static function init($login,$password) {
  36. $_SESSION['user_password'] = $password; // save the password as passcode for the encryption
  37. if(OC_User::isLoggedIn()){
  38. // does key exist?
  39. if(!file_exists(OC_Config::getValue( "datadirectory").'/'.$login.'/encryption.key')){
  40. OC_Crypt::createkey($_SESSION['user_password']);
  41. }
  42. }
  43. }
  44. public static function createkey($passcode) {
  45. if(OC_User::isLoggedIn()){
  46. // generate a random key
  47. $key=mt_rand(10000,99999).mt_rand(10000,99999).mt_rand(10000,99999).mt_rand(10000,99999);
  48. // encrypt the key with the passcode of the user
  49. $enckey=OC_Crypt::encrypt($key,$passcode);
  50. // Write the file
  51. $username=OC_USER::getUser();
  52. @file_put_contents(OC_Config::getValue( "datadirectory").'/'.$username.'/encryption.key', $enckey );
  53. }
  54. }
  55. public static function changekeypasscode( $newpasscode) {
  56. if(OC_User::isLoggedIn()){
  57. $username=OC_USER::getUser();
  58. // read old key
  59. $key=file_get_contents(OC_Config::getValue( "datadirectory").'/'.$username.'/encryption.key');
  60. // decrypt key with old passcode
  61. $key=OC_Crypt::decrypt($key, $_SESSION['user_password']);
  62. // encrypt again with new passcode
  63. $key=OC_Crypt::encrypt($key,$newpassword);
  64. // store the new key
  65. file_put_contents(OC_Config::getValue( "datadirectory").'/'.$username.'/encryption.key', $key );
  66. $_SESSION['user_password']=$newpasscode;
  67. }
  68. }
  69. /**
  70. * @brief encrypts an content
  71. * @param $content the cleartext message you want to encrypt
  72. * @param $key the encryption key
  73. * @returns encrypted content
  74. *
  75. * This function encrypts an content
  76. */
  77. public static function encrypt( $content, $key) {
  78. $bf = new Crypt_Blowfish($key);
  79. return($bf->encrypt($content));
  80. }
  81. /**
  82. * @brief decryption of an content
  83. * @param $content the cleartext message you want to decrypt
  84. * @param $key the encryption key
  85. * @returns cleartext content
  86. *
  87. * This function decrypts an content
  88. */
  89. public static function decrypt( $content, $key) {
  90. $bf = new Crypt_Blowfish($key);
  91. return($bf->encrypt($contents));
  92. }
  93. /**
  94. * @brief encryption of a file
  95. * @param $filename
  96. * @param $key the encryption key
  97. *
  98. * This function encrypts a file
  99. */
  100. public static function encryptfile( $filename, $key) {
  101. $handleread = fopen($filename, "rb");
  102. if($handleread<>FALSE) {
  103. $handlewrite = fopen($filename.OC_Crypt::$encription_extension, "wb");
  104. while (!feof($handleread)) {
  105. $content = fread($handleread, 8192);
  106. $enccontent=OC_CRYPT::encrypt( $content, $key);
  107. fwrite($handlewrite, $enccontent);
  108. }
  109. fclose($handlewrite);
  110. unlink($filename);
  111. }
  112. fclose($handleread);
  113. }
  114. /**
  115. * @brief decryption of a file
  116. * @param $filename
  117. * @param $key the decryption key
  118. *
  119. * This function decrypts a file
  120. */
  121. public static function decryptfile( $filename, $key) {
  122. $handleread = fopen($filename.OC_Crypt::$encription_extension, "rb");
  123. if($handleread<>FALSE) {
  124. $handlewrite = fopen($filename, "wb");
  125. while (!feof($handleread)) {
  126. $content = fread($handleread, 8192);
  127. $enccontent=OC_CRYPT::decrypt( $content, $key);
  128. fwrite($handlewrite, $enccontent);
  129. }
  130. fclose($handlewrite);
  131. unlink($filename.OC_Crypt::$encription_extension);
  132. }
  133. fclose($handleread);
  134. }
  135. }