DES加密和解密PHP等语言的方法
PHP的加解密函数
class DesComponent {
var $key = 12345678;function encrypt($string) {
$ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);
$iv=null;
foreach ($ivArray as $element)
$iv.=CHR($element);
$size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
$string = $this->pkcs5Pad ( $string, $size );$data = mcrypt_encrypt(MCRYPT_DES, $this->key, $string, MCRYPT_MODE_CBC, $iv);
$data = base64_encode($data);
return $data;
}function decrypt($string) {
$ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);
$iv=null;
foreach ($ivArray as $element)
$iv.=CHR($element);$string = base64_decode($string);
//echo("****");
//echo($string);
//echo("****");
$result = mcrypt_decrypt(MCRYPT_DES, $this->key, $string, MCRYPT_MODE_CBC, $iv);
$result = $this->pkcs5Unpad( $result );return $result;
}
function pkcs5Pad($text, $blocksize)
{
$pad = $blocksize - (strlen ( $text ) % $blocksize);
return $text . str_repeat ( chr ( $pad ), $pad );
}
function pkcs5Unpad($text)
{
$pad = ord ( $text {strlen ( $text ) - 1} );
if ($pad > strlen ( $text ))
return false;
if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad)
return false;
return substr ( $text, 0, - 1 * $pad );
}
}
$des = new DesComponent();
echo ($des->encrypt("19760519"));
echo "";//die($des->decrypt("zLVdpYUM0qw="));
//die($des->decrypt("zLVdpYUM0qzEsNshEEI6Cg=="));$t2 =$des->decrypt("zLVdpYUM0qw=");
echo $t2;
echo "--";
echo strlen($t2);
echo is_utf8($t2);
echo "";
$t3 = mb_convert_encoding($t2,"GB2312", "utf-8");
echo $t3;
echo "--";
echo strlen($t3);
echo is_utf8($t3);
echo "";
$t1 =$des->decrypt("zLVdpYUM0qzEsNshEEI6Cg==");
echo $t1;
echo "--";
echo strlen($t1);
echo is_utf8($t1);echo "";
$t3 = mb_convert_encoding($t1, "utf-8","GB2312");
echo $t3;
echo "--";
echo strlen($t3);
echo is_utf8($t3);function is_utf8($string) {
return preg_match(%^(?:
[x09x0Ax0Dx20-x7E] # ASCII
| [xC2-xDF][x80-xBF] # non-overlong 2-byte
| xE0[xA0-xBF][x80-xBF] # excluding overlongs
| [xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte
| xED[x80-x9F][x80-xBF] # excluding surrogates
| xF0[x90-xBF][x80-xBF]{2} # planes 1-3
| [xF1-xF3][x80-xBF]{3} # planes 4-15
| xF4[x80-x8F][x80-xBF]{2} # plane 16
)*$%xs, $string);
}
?>Java的加解密函数
package ghj1976.Demo;
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.UnsupportedEncodingException;
/**
* Utilities for encoding and decoding the Base64 representation of
* binary data. See RFCs 2045 and 3548.
*/
public class Base64 {
/**
* Default values for encoder/decoder flags.
*/
public static final int DEFAULT = 0;
/**
* Encoder flag bit to omit the padding = characters at the end
* of the output (if any).
*/
public static final int NO_PADDING = 1;
/**
* Encoder flag bit to omit all line terminators (i.e., the output
* will be on one long line).
*/
public static final int NO_WRAP = 2;
/**
* Encoder flag bit to indicate lines should be terminated with a
* CRLF pair instead of just an LF. Has no effect if {@code
* NO_WRAP} is specified as well.
*/
public static final int CRLF = 4;
/**
* Encoder/decoder flag bit to indicate using the "URL and
* filename safe" variant of Base64 (see RFC 3548 section 4) where
* {@code -} and {@code _} are used in place of {@code +} and
* {@code /}.
*/
public static final int URL_SAFE = 8;
/**
* Flag to pass to {@link Base64OutputStream} to indicate that it
* should not close the output stream it is wrapping when it
* itself is closed.
*/
public static final int NO_CLOSE = 16;
// --------------------------------------------------------
// shared code
// --------------------------------------------------------
/* package */ static abstract class Coder {
public byte[] output;
public int op;
/**
* Encode/decode another block of input data. this.output is
* provided by the caller, and must be big enough to hold all
* the coded data. On exit, this.opwill be set to the length
* of the coded data.
*
* @param finish true if this is the final call to proce
补充:综合编程 , 安全编程 ,