Skip to content

Commit

Permalink
crypt support
Browse files Browse the repository at this point in the history
  • Loading branch information
kurong committed Sep 18, 2017
1 parent 6a15e58 commit 4ef53b6
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 24 deletions.
14 changes: 0 additions & 14 deletions golang/xrsa/bytes.go

This file was deleted.

71 changes: 71 additions & 0 deletions golang/xrsa/crypt.go
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)
}
29 changes: 29 additions & 0 deletions golang/xrsa/crypt_test.go
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")
}
}
13 changes: 13 additions & 0 deletions golang/xrsa/xrsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,17 @@ func MarshalPKCS8PrivateKey(key *rsa.PrivateKey) []byte {

k, _ := asn1.Marshal(info)
return k
}

func split(buf []byte, lim int) [][]byte {
var chunk []byte
chunks := make([][]byte, 0, len(buf)/lim+1)
for len(buf) >= lim {
chunk, buf = buf[:lim], buf[lim:]
chunks = append(chunks, chunk)
}
if len(buf) > 0 {
chunks = append(chunks, buf[:len(buf)])
}
return chunks
}
59 changes: 59 additions & 0 deletions php/src/Crypt.php
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;
}
}
17 changes: 17 additions & 0 deletions php/tests/CryptTest.php
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);
}
}
2 changes: 0 additions & 2 deletions php/tests/Pem.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

namespace XRsa;

/**
* @author williamylian
*
Expand Down
12 changes: 4 additions & 8 deletions php/tests/XRsaTest.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
<?php

use XRsa\XRsa;
use XRsa\Pem;
use PHPUnit\Framework\TestCase;

class XRsaTest extends TestCase
{
public function test_create_keys()
{
//$keys = XRsa::createKeys(2048);
//$this->assertNotNull($keys['publicKey']);
//$this->assertNotNull($keys['privateKey']);
$keys = XRsa::createKeys(2048);
$this->assertNotNull($keys['publicKey']);
$this->assertNotNull($keys['privateKey']);

//return $keys;
$publicKey = file_get_contents(__DIR__. "/../../test/pub.pem");
$privateKey = file_get_contents(__DIR__. "/../../test/pri.pem");
return ['publicKey'=>$publicKey, 'privateKey'=>$privateKey];
return $keys;
}

/**
Expand Down

0 comments on commit 4ef53b6

Please sign in to comment.