forked from liamylian/x-rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kurong
committed
Sep 18, 2017
1 parent
6a15e58
commit 4ef53b6
Showing
8 changed files
with
193 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package xrsa | ||
|
||
import ( | ||
"crypto/md5" | ||
"encoding/hex" | ||
) | ||
|
||
type Crypt struct { | ||
key string | ||
} | ||
|
||
func NewCrypt(key string) *Crypt { | ||
return &Crypt{ key:key } | ||
} | ||
|
||
func (c *Crypt) Encrypt(data []byte) []byte { | ||
sum := getMd5([]byte(c.key)) | ||
dataLen := len(data) | ||
sumLen := len(sum) | ||
|
||
j := 0 | ||
encrypted := make([]byte, dataLen * 2) | ||
for i := 0; i < dataLen; i++ { | ||
if j == sumLen - 1 { | ||
j = 0 | ||
} | ||
encrypted[i*2] = sum[j] | ||
encrypted[i*2+1] = data[i] ^ sum[j] | ||
j++ | ||
} | ||
|
||
return ed(encrypted, c.key) | ||
} | ||
|
||
func (c *Crypt) Decrypt(data []byte) []byte { | ||
ed := ed(data, c.key) | ||
dataLen := len(data) | ||
|
||
decrypted := make([]byte, dataLen / 2) | ||
for i := 0; i < dataLen / 2; i++ { | ||
decrypted[i] = ed[i*2] ^ ed[i*2+1] | ||
} | ||
|
||
return decrypted | ||
} | ||
|
||
func ed(data []byte, key string) []byte { | ||
sum := getMd5([]byte(key)) | ||
dataLen := len(data) | ||
sumLen := len(sum) | ||
|
||
j := 0 | ||
encrypted := make([]byte, dataLen) | ||
for i := 0; i < dataLen; i++ { | ||
if j == sumLen - 1 { | ||
j = 0 | ||
} | ||
|
||
encrypted[i] = data[i] ^ sum[j] | ||
j++ | ||
} | ||
|
||
return encrypted | ||
} | ||
|
||
func getMd5(data []byte) string { | ||
md5Ctx := md5.New() | ||
md5Ctx.Write(data) | ||
sum := md5Ctx.Sum(nil) | ||
return hex.EncodeToString(sum) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package xrsa | ||
|
||
import( | ||
"testing" | ||
) | ||
|
||
func TestEd(t *testing.T) { | ||
a := "Hello" | ||
key := "123456" | ||
|
||
b := ed([]byte(a), key) | ||
c := ed(b, key) | ||
if string(a) != string(c) { | ||
t.Fatal("failed") | ||
} | ||
} | ||
|
||
func TestEncryptDecryptt(t *testing.T) { | ||
a := "Hello" | ||
key := "123456" | ||
|
||
crypt := NewCrypt(key) | ||
encrypted := crypt.Encrypt([]byte(a)) | ||
|
||
decrypted := crypt.Decrypt(encrypted) | ||
if string(decrypted) != a { | ||
t.Fatal("encrypt decrypt failed") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace XRsa; | ||
|
||
class Crypt | ||
{ | ||
private $key; | ||
|
||
public function __construct($key) | ||
{ | ||
$this->key = $key; | ||
} | ||
|
||
public function encrypt($str) | ||
{ | ||
$r = md5($this->key); | ||
$c = 0; | ||
$v = ""; | ||
$len = strlen($str); | ||
$l = strlen($r); | ||
for ($i = 0; $i < $len; $i++) { | ||
if ($c == $l) $c = 0; | ||
$v .= substr($r, $c, 1) . | ||
(substr($str, $i, 1) ^ substr($r, $c, 1)); | ||
$c++; | ||
} | ||
|
||
return self::ed($v, $this->key); | ||
} | ||
|
||
public function decrypt($str) | ||
{ | ||
$str = self::ed($str, $this->key); | ||
|
||
$v = ""; | ||
$len = strlen($str); | ||
for ($i = 0; $i < $len; $i++) { | ||
$md5 = substr($str, $i, 1); | ||
$i++; | ||
$v .= (substr($str, $i, 1) ^ $md5); | ||
} | ||
return $v; | ||
} | ||
|
||
private static function ed($str, $key) | ||
{ | ||
$r = md5($key); | ||
$c = 0; | ||
$v = ""; | ||
$len = strlen($str); | ||
$l = strlen($r); | ||
for ($i = 0; $i < $len; $i++) { | ||
if ($c == $l) $c = 0; | ||
$v .= substr($str, $i, 1) ^ substr($r, $c, 1); | ||
$c++; | ||
} | ||
return $v; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
use XRsa\Crypt; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CryptTest extends TestCase | ||
{ | ||
public function testEncryptDecrypt() | ||
{ | ||
$crypt = new Crypt(123456); | ||
$data = "Hello, World"; | ||
$encrypted = $crypt->encrypt($data); | ||
$decrypted = $crypt->decrypt($encrypted); | ||
|
||
$this->assertEquals($data, $decrypted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
<?php | ||
|
||
namespace XRsa; | ||
|
||
/** | ||
* @author williamylian | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters