openssl rsa 使用简介

openssl命令的用法

 密钥的生成

 a. 生成非对称密钥对
openssl genrsa -out rsa.key
b. 指定生成的密钥的位数,默认512
openssl genrsa -out rsa_2048.key 2048
c. 为私钥添加密码 (一般都不用)
openssl genrsa -out rsa_des3.key -des3


密钥的查看

d. 查看私钥
openssl rsa -in rsa.key
e. 查看公钥
openssl rsa -in rsa.key -pubout
f. 查看公钥和modulus
openssl rsa -in rsa.key -modulus
g. 查看密钥的详细信息,包含component prime等细节信息,这些信息的值都是冒号分割的,称为abstract
openssl rsa -in rsa.key -text
h. 查看只有public key的文件
openssl rsa -in pub.txt -pubin
注:
pub.txt 可由
openssl rsa -in rsa.key -pubout >pub.txt

openssl rsa -in rsa.key -pubout -out pub.txt
产生
如果pub.txt 中不是公钥将报错, -pubin 仅仅说明 -in 所指定的文件里面是什么
i. 只查看key的其他信息,不显示key
openssl  rsa -in rsa.key -noout

openssl rsautl 系列

1. 使用生成的公钥加密文件

-in指定要加密的文件,-inkey指定密钥,-pubin表明是用纯公钥文件加密,-out为加密后的文件

2. 使用生成的私钥解密文件

-in指定被加密的文件,-inkey指定私钥文件,-out为解密后的文件

参考: http://www.cnblogs.com/aLittleBitCool/archive/2011/09/22/2185418.html

 

其它:

检查是否含有某个子命令
openssl no-rsa
输出rsa,命令的返回值为1,说明存在该子命令
openssl no-des5
输出no-des5 命令的返回值为0,说明不存在des5这个子命令

分析key的详细信息
openssl asn1parse -in rsa.key
openssl asn1parse -in pub.txt

 

签名和签名的验证

  这里给出一个例子:

基本步骤:

1. 生成rsa密钥对

2. 用私钥做签名

3. 用公钥做验证

[root@bsso ~]# openssl
OpenSSL> genrsa -out rsa.1024 1024
Generating RSA private key, 1024 bit long modulus
………..++++++
……….++++++
e is 65537 (0x10001)
OpenSSL> rsa -in rsa.1024 -pubout
writing RSA key
—–BEGIN PUBLIC KEY—–
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD0BlFXN1wkgBb5hadMeLz4Pqj2
nZQGyyidW1GTun9rnKkG7o/v/YM8MCcrqW+2hizkbJygfRGvb1iHvc22SD7Q1Unk
7yKU5qDiDnXdIl1x05PMGRwfNhG75uv9tr/IsxA+bmIrEAZ+fxlGhXbg8R2gUm2O
c51AyBOgb92DtEfLgQIDAQAB
—–END PUBLIC KEY—–
OpenSSL> rsa -in rsa.1024 -pubout -out rsa.1024.pub
writing RSA key
OpenSSL> dgst -sign rsa.1024 -sha1 -out a.sign a.php
OpenSSL> dgst -verify rsa.1024.pub -sha1 -signature a.sign a.php

Verified OK
OpenSSL>

 

如何查看公钥的位数:

解释一个错误的说法: 上面的公钥base64 decode 之后为94字节, 不能说该公钥的位数为 94*8 = 752位; 而是用上面的方法查看,为: 512位

 

浏览器与http referer的问题

关于referer,它有很多用途,但是,只有我们真正理解了其含义,才不至于错用,下面说几个referer需要注意的问题

1. 对于firefox,关于referer这部分是可以在about:config里面设置的

2. 一般来讲

         点击一个a标签,访问一个目标页面,这时referer是源页面

         在标签里面访问一个地址,如: img   script 标签的src属性,这时src地址的请求里可以得到referer

3. 跳转时一般没有referer

         使用location.href = "b.htm";则b.htm得不到referer

         使用location.replace("b.htm");则b.htm得不到referer

         使用header("Location: b.htm");则b.htm得不到referer

         怎么跳转可以得到referer呢?

         使用form表单的形式跳转,如:

       <html>
        <
head>
                <
title>redirect</title>
        </
head>
        <
body onload="document.getElementById(‘f’).submit()">
                <
form id="f" action="a.php" method="post">
                        <
input type="hidden" name="n" value="m" />
                </
form>
        </
body>
       </
html>

关于apache的跟踪

曾经修改过/etc/resolve.conf后,发现apache进程里还是使用旧的dns server来解析域名,重启apache后才生效;这里给出几个确认的办法:

办法一: 如果服务器上没有其它需要域名解析的进程的话,使用tcpdump 来观察53端口的udp包就基本可以知道了

                  命令: tcpdump -i eth1 -nn -X -s 0  "port 53 and udp"

办法二: 随便找一个httpd的子进程strace一下就基本知道了,但是要求两点:

                  1. 访问量比较大,随时都有做域名解析的可能;而且程序里确实有做域名解析的需要

                   2. httpd子进程不会死的太快,太快了你会抓不到的

                  命令: strace -p pid_of_httpd_child -e trace=network 2>&1 | grep "(53)"

办法三: 因为httpd子进程可能死的太快,我们就从httpd的守护进程着手
                 命令: strace -p pid_of_httpd_parent -fF -e trace=network 2>&1 | grep "(53)"

windows 应用程序操作IE Cookie

windows的cookie文件我们可以找到,文件格式也可以分析,但是一般我们操作IE cookie时,并不直接操作文件,windows给出了两个操作cookie的api:

InternetGetCookie
InternetSetCookie

但是这两个api的功能很简单:
1. 不能操作httponly属性,及 InternetSetCookie函数不能设置Httponly属性,InternetGetCookie函数也不能获取设置了Httponly属性的cookie

windows还提供了另外两个api:

InternetGetCookieEx
InternetSetCookieEx

这两个api可以操作Httponly属性,但是还有个限制,就是适用于IE8 or later的IE

 

相关文章:http://msdn.microsoft.com/en-us/library/aa385473(VS.85).aspx

要了解windows 就得上msdn

firefox config 简介

firefox中可配置的选项很多,作为一个web开发者来将,我们虽然没有权限修改用户的浏览器配置,但是我们起码可以从中知道浏览器的默认配置是怎样的,这也有利于我们的程序的开发。

在firefox地址栏里输入: about:config

你将发现很多firefox的配置选项,简单说一下我看过的几类:

cache 相关类,可以配置cache的使用方式,如果我们是做测试的,不希望有cache,你们彻底禁止掉就可以了,相关选项为:
browser.cache.disk.enable
browser.cache.memory.enable
network.http.use-cache false 不适用任何cache

另外: browser.cache.disk_cache_ssl 默认为false,说明firefox默认是不缓存走ssl的请求的

network.http 相关类
network.http.max-connections-per-server 默认为15,就是说每个server最多同时发起15个请求
network.http.max-connections 默认为30, 就是同时的最大连接数不超过30个

(测试时发现每个域同时最大的连接数也不超过2个,不知道为何,所以这些配置看完后最好测试一下再去相信)

network.dns 相关类
network.dns.disableIPv6 false 是否先按照ipv6来解析域名

cookie 相关

 

session相关

ssl相关

dom相关

dom.allow_scripts_to_close_windows    默认false , 是否允许script关闭窗口

 

PHP 操作ms word

Useful PHP MSWord class I created to do some simple file conversions.  This class could have a lot more to it but I am not familiar with all the COM MS Word function calls.
<?php
   
// msword.inc.php
   
   // NOTE: Using COM with windows NT/2000/XP with apache as a service
   // – Run dcomcnfg.exe
   // – Find word application and click properties
   // – Click the Security tab
   // – Use Custom Access Permissions
   //      – Add the user who runs the web server service
   // – Use Custom Launch permissions
   //      – Add the user who runs the web server service
   
   
$wdFormatDocument 0
;
   
$wdFormatTemplate 1
;
   
$wdFormatText 2
;
   
$wdFormatTextLineBreaks 3
;
   
$wdFormatDOSText 4
;
   
$wdFormatDOSTextLineBreaks 5
;
   
$wdFormatRTF 6
;
   
$wdFormatUnicodeText 7
;
   
$wdFormatHTML=8
;

   class MSWord
   
{
       
// Vars:
       
var $handle
;
       
       
// Create COM instance to word
       
function MSWord($Visible false
)
       {
           
$this->handle = new COM("word.application") or die("Unable to instanciate Word"
);
           
$this->handle->Visible $Visible
;
       }
       
       
// Open existing document
       
function Open($File
)
       {
           
$this->handle->Documents->Open($File
);
       }
       
       
// Create new document
       
function NewDocument
()
       {
           
$this->handle->Documents->Add
();
       }
       
       
// Write text to active document
       
function WriteText$Text 
)
       {
           
$this->handle->Selection->Typetext$Text 
);
       }
       
       
// Number of documents open
       
function DocumentCount
()
       {
           return 
$this->handle->Documents->Count
;
       }
       
       
// Save document as another file and/or format
       
function SaveAs($File$Format 
)
       {
           
$this->handle->ActiveDocument->SaveAs($File$Format
);
       }
       
       
// Save active document
       
function Save
()
       {
           
$this->handle->ActiveDocument->Save
();
       }
       
       
// close active document.
       
function Close
()
       {
           
$this->handle->ActiveDocument->Close
();
       }
       
       
// Get word version
       
function GetVersion
()
       {
           return 
$this->handle->Version
;
       }
       
       
// get handle to word
       
function GetHandle
()
       {
           return 
$this->handle
;
       }
   
       
// Clean up instance with word
       
function Quit
()
       {
           if( 
$this->handle 
)
           {
               
// close word
               
$this->handle->Quit
();
   
               
// free the object
               
$this->handle->Release
();
               
$this->handle null
;
           }
       }
   };

?>

Example 1, opens an html file, writes text to it, then saves it as a document:
<?php
   $input 
"C:\test.htm"
;
   
$output "C:\test.doc"
;

   $Word = new MSWord;
   
$Word->Open($input
);
   
$Word->WriteText("This is a test "
);
   
$Word->SaveAs($output
);
   
$Word->Quit
();
?>

Example 2, opens an html file, then saves it as a rtf file:
<?php
   $input 
"C:\test.htm"
;
   
$output "C:\test.rtf"
;

   $Word = new MSWord;
   
$Word->Open($input
);
   
$Word->SaveAs($output$wdFormatRTF
);
   
$Word->Quit
();
?> 

长格式命令行参数解析类

下面是一个摘自php手册的一个长格式的命令行参数解析类,感觉实现的思想还不错:

<?php
/**********************************************
 * Simple argv[] parser for CLI scripts
 * Diego Mendes Rodrigues – So Paulo – Brazil
 * diego.m.rodrigues [at] gmail [dot] com
 * May/2005
 **********************************************/

class ArgParser{
    var 
$argc
;
    var 
$argv
;
    var 
$parsed
;
    var 
$force_this
;

    function ArgParser($force_this="") {
        global 
$argc$argv
;
        
$this->argc $argc
;
        
$this->argv $argv
;
        
$this->parsed 
= array();

        array_push($this->parsed,
            array(
$this->argv[0
]) );

        if ( !empty($force_this) )
            if ( 
is_array($force_this
) )
                
$this->force_this $force_this
;

        //Sending parameters to $parsed
        
if ( $this->argc 
) {
            for(
$i=$i$this->argc $i
++) {
                
//We only have passed -xxxx
                
if ( substr($this->argv[$i],0,1) == "-" 
) {
                    
//Se temos -xxxx xxxx
                    
if ( $this->argc > ($i+1
) ) {
                        if ( 
substr($this->argv[$i+1],0,1) != "-" 
) {
                            
array_push($this->parsed
,
                                array(
$this->argv[$i
],
                                
$this->argv[$i+1
]) );
                            
$i
++;
                            continue;
                        }
                    }
                }
                
//We have passed -xxxxx1 xxxxx2
                
array_push($this->parsed
,
                    array(
$this->argv[$i
]) );
            }
        }

        //Testing if all necessary parameters have been passed
        
$this->force
();
    }

    //Testing if one parameter have benn passed
    
function passed($argumento
) {
        for(
$i=$i$this->argc $i
++) 
            if ( 
$this->parsed[$i][0] == $argumento 
)
                return 
$i
;
        return 
0
;
    }

    //Testing if you have passed a estra argument, -xxxx1 xxxxx2
    
function full_passed($argumento
) {
        
$findArg $this->passed($argumento
);
        if ( 
$findArg 
)
            if ( 
count($this->parsed[$findArg] ) > 

                return 
$findArg
;
        return 
0
;
    }

    //Returns  xxxxx2 at a " -xxxx1 xxxxx2" call
    
function get_full_passed($argumento
) {
        
$findArg $this->full_passed($argumento
);

        if ( $findArg )
            return 
$this->parsed[$findArg][1
];

        return;
    }

    //Necessary parameters to script
    
function force
() {
        if ( 
is_array$this->force_this 
) ) {
            for(
$i=$icount($this->force_this) ; $i
++) {
                if ( 
$this->force_this[$i][1] == 
"SIMPLE" 
                    
&& !$this->passed($this->force_this[$i][0
])
                )
                die(
"nnMissing " $this->force_this[$i][0] . "nn"
);

                if ( $this->force_this[$i][1] == "FULL"
                    
&& !$this->full_passed($this->force_this[$i][0
]) 
                )
                die(
"nnMissing " $this->force_this[$i][0] ." <arg>nn"
);
            }
        }
    }
}

//Example
$forcar 
= array(
    array(
"-name""FULL"
),
    array(
"-email","SIMPLE"
) );

$parser = new ArgParser($forcar);

if ( $parser->passed("-show") ) 
    echo 
"nGoing…:"
;

echo "nName: " $parser->get_full_passed("-name");

if ( $parser->full_passed("-email") )  
    echo 
"nEmail: " $parser->get_full_passed("-email"
);
else
    echo 
"nEmail: default"
;

if ( $parser->full_passed("-copy") )
    echo 
"nCopy To: " $parser->get_full_passed("-copy"
);

echo "nn";

?>

httpwatch 与 gzip

    我非常喜欢使用httpwatch来查看客户端与服务器之间的交互,我一贯是看httpwatch里面的stream选项,因为这样看到的都是最原始的请求和相应数据,但是有一天我的这种做法受到的挑战,因为服务器的响应时作了gzip压缩的,这样不能很直观地看到响应的内容,怎么办呢?

目前这里有三个办法,当然肯定还有其他办法。

办法1.  自己做一个小工具来解压压缩的数据

        该办法比较麻烦,而且不够通用,我根本没有尝试过这种办法。

办法2.  httpwatch看到的请求的数据复制到文本文件里,然后将accept请求头去掉,打开cmd,使用telnet来访问

        该办法比上一种办法稍微好一点,比较简单易行,但是缺点有二:

        一、对于不允许重放的请求无效

        二、对于https的请求无效

           其实,对于https的请求,可以借助openssl来完成,命令:

           openssl s_client -connect host:port 

          然后就像用telnet那样输入明文就可以了

办法3.  使用curl自己构造请求串

        该办法可以避免办法二里面的第二个缺点,即:对于走https的请求也可以搞定;但是要求你机器上有curl命令,而且操作也比较麻烦

办法4.  使用firefox,通过如下配置可以使得返回的数据不压缩,这个利用了http协议的知识

         一、在firefox地址栏里输入:   about:config

        二、查找network.http.accept-encoding  清空即可