-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrypto.php
51 lines (46 loc) · 1.35 KB
/
Crypto.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Rss;
/**
* Class Crypto
*
* This is a class I previously created for a university assignment. It allows for encrypting and decrypting of a string
*
* @package Rss
*/
class Crypto
{
private $iv;
private $secret;
/**
* Constructs the crypto class.
* @param $secret Pre-defined secret string. Only use one per app.
*/
public function __construct($secret)
{
$this->secret = $secret;
$iv_size = mcrypt_get_iv_size(MCRYPT_TWOFISH, MCRYPT_MODE_ECB);
$this->iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
}
/**
* Encrypts a string and returns in base64.
* @param $string String to encrypt.
* @return string Encrypted base64 string.
*/
public function encrypt($string)
{
$encrypted_string = mcrypt_encrypt(MCRYPT_TWOFISH, $this->secret, $string, MCRYPT_MODE_ECB, $this->iv);
$base64_string = base64_encode($encrypted_string);
return trim($base64_string);
}
/**
* Decrypts a base64 string.
* @param $string Base64 string to decrypt.
* @return string Decrypted string.
*/
public function decrypt($string)
{
$string = base64_decode($string);
$decrypted_string = mcrypt_decrypt(MCRYPT_TWOFISH, $this->secret, $string, MCRYPT_MODE_ECB, $this->iv);
return trim($decrypted_string);
}
}