2 han3262302 han3262302 于 2013.12.23 11:38 提问

java登录thinksns时的加密解密操作

api说在登录的时候账号需要des加密, 密码需要des和md5加密, 可是我用了好几种des加密都是认证失败.
我用的是java, 下面是我从thinksns 里找到的php代码, 求好心人转成java, php真心不太懂

<?php
# 可与java object 兼容

class DES_MOBILE {
var $key = '12345678';

function  setKey($key){
    $this->key = $key;
    return $this;
}

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);

    $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 );  
}  

}

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!