如何在PHP中加密/解密整数

时间:2014-06-22 11:42:01

标签: php encryption

有没有办法对整数(或字符串)进行双向加密/解密 请注意,我不是在寻找编码

我需要这样的东西

  

加密(100) - > 24694

     

加密(101) - > 9564jh4或45216或gvhjdfT或其他......

     

解密(24694) - > 100

我不需要编码,因为它是双射的

  

base64_encode(100) - > MTAw

     

base64_encode(101) - > MTAx

我希望我能在这里找到一种方法来加密/解密PURE NUMBERS(计算机爱情数字,它更快)

7 个答案:

答案 0 :(得分:9)

您是否尝试过调查ROT-13

更严肃的回答:从this回答,您可以使用:

function numhash($n) {
    return (((0x0000FFFF & $n) << 16) + ((0xFFFF0000 & $n) >> 16));
}

numhash(42);           // 2752512
numhash(numhash(42));   // 42

答案 1 :(得分:8)

function decrypt($string, $key) {
$result = '';
$string = base64_decode($string);
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)-ord($keychar));
$result.=$char;
}
return $result;
}

function encrypt($string, $key) {
$result = '';
for($i=0; $i<strlen($string); $i++) {
$char = substr($string, $i, 1);
$keychar = substr($key, ($i % strlen($key))-1, 1);
$char = chr(ord($char)+ord($keychar));
$result.=$char;
}
return base64_encode($result);
}

答案 2 :(得分:2)

64位支持。负数支持。还有一点安全盐。

@Petr Cibulka

class NumHash {

  private static $SALT = 0xd0c0adbf;

  public static function encrypt($n) {
    return (PHP_INT_SIZE == 4 ? self::encrypt32($n) : self::encrypt64($n)) ^ self::$SALT;
  }

  public static function decrypt($n) {
    $n ^= self::$SALT;
    return PHP_INT_SIZE == 4 ? self::decrypt32($n) : self::decrypt64($n);
  }

  public static function encrypt32($n) {
    return ((0x000000FF & $n) << 24) + (((0xFFFFFF00 & $n) >> 8) & 0x00FFFFFF);
  }

  public static function decrypt32($n) {
    return ((0x00FFFFFF & $n) << 8) + (((0xFF000000 & $n) >> 24) & 0x000000FF);
  }

  public static function encrypt64($n) {
    /*
    echo PHP_EOL . $n . PHP_EOL;
    printf("n   :%20X\n", $n);
    printf("<<  :%20X\n", (0x000000000000FFFF & $n) << 48);
    printf(">>  :%20X\n", (0xFFFFFFFFFFFF0000 & $n) >> 16);
    printf(">>& :%20X\n", ((0xFFFFFFFFFFFF0000 & $n) >> 16) & 0x0000FFFFFFFFFFFF);
    printf("=   :%20X\n", ((0x000000000000FFFF & $n) << 48) + (((0xFFFFFFFFFFFF0000 & $n) >> 16) & 0x0000FFFFFFFFFFFF));
    /* */
    return ((0x000000000000FFFF & $n) << 48) + (((0xFFFFFFFFFFFF0000 & $n) >> 16) & 0x0000FFFFFFFFFFFF);
  }

  public static function decrypt64($n) {
    /*
    echo PHP_EOL;
    printf("n   :%20X\n", $n);
    printf("<<  :%20X\n", (0x0000FFFFFFFFFFFF & $n) << 16);
    printf(">>  :%20X\n", (0xFFFF000000000000 & $n) >> 48);
    printf(">>& :%20X\n", ((0xFFFF000000000000 & $n) >> 48) & 0x000000000000FFFF);
    printf("=   :%20X\n", ((0x0000FFFFFFFFFFFF & $n) << 16) + (((0xFFFF000000000000 & $n) >> 48) & 0x000000000000FFFF));
    /* */
    return ((0x0000FFFFFFFFFFFF & $n) << 16) + (((0xFFFF000000000000 & $n) >> 48) & 0x000000000000FFFF);
  }
}

var_dump(NumHash::encrypt(42));
var_dump(NumHash::encrypt(NumHash::encrypt(42)));
var_dump(NumHash::decrypt(NumHash::encrypt(42)));

echo PHP_EOL;


// stability test

var_dump(NumHash::decrypt(NumHash::encrypt(0)));
var_dump(NumHash::decrypt(NumHash::encrypt(-1)));
var_dump(NumHash::decrypt(NumHash::encrypt(210021200651)));
var_dump(NumHash::decrypt(NumHash::encrypt(210042420501)));

以下是逐步操作(删除评论):

210042420501
n   :          30E780FD15
<<  :    FD15000000000000
>>  :              30E780
>>& :              30E780
=   :    FD1500000030E780

n   :    FD1500000030E780
<<  :          30E7800000
>>  :    FFFFFFFFFFFFFD15
>>& :                FD15
=   :          30E780FD15
int(210042420501)

答案 3 :(得分:0)

您只需使用3DES CBC模式加密即可执行操作。如果您只想接受已生成的值,可以将HMAC添加到密文。如果HMAC不够,您可以依赖此特定方案的数字格式。如果您希望用户无法将值复制到彼此,则可以使用随机IV。

所以基本上你通过左边填充零值将数字存储为8字节或8个ASCII字符串。然后,您执行单个块的加密。这允许您有2 ^ 64或10 ^ 8个数字。您可以使用64加密结果,将+/字符替换为网址安全的-_字符。

请注意,此加密/解密当然是双射的(或置换,因为它通常在加密中调用)。但这没关系,因为输出足够大,攻击者无法猜测值。

答案 4 :(得分:0)

这可能比你想要的更多,但我认为构建答案会很有趣。这是一个简单的格式保留加密,它采用任何16位数(即从0到65535),并根据128位对称密钥将其加密为另一个16位数,然后再加回。你可以建立这样的东西。

它是确定性的,因为任何输入总是使用相同的密钥加密到相同的输出,但对于任何数字n,都无法预测n + 1的输出。

# Written in Ruby -- implement in PHP left as an exercise for the reader
require 'openssl'

def encrypt_block(b, k)
    cipher = OpenSSL::Cipher::Cipher.new 'AES-128-ECB'
    cipher.encrypt
    cipher.key = k
    cipher.update(b) + cipher.final
end

def round_key(i, k)
    encrypt_block(i.to_s, k)
end

def prf(c, k)
    encrypt_block(c.chr, k)[0].ord
end

def encrypt(m, key)
    left = (m >> 8) & 0xff
    right = m & 0xff
    (1..7).each do |i|
        copy = right
        right = left ^ prf(right, round_key(i, key))
        left = copy
    end
    (left << 8) + right
end

def decrypt(m, key)
    left = (m >> 8) & 0xff
    right = m & 0xff
    (1..7).each do |i|
        copy = left
        left = right ^ prf(left, round_key(8 - i, key))
        right = copy
    end
    (left << 8) + right
end

key = "0123456789abcdef"

# This shows no fails and no collisions
x = Hash.new
(0..65535).each do |n|
    c = encrypt(n, key)
    p = decrypt(c, key)
    puts "FAIL" if n != p
    puts "COLLISION" if x.has_key? c
    x[c] = n
end

# Here are some samples
(0..10).each do |n|
    c = encrypt(n, key)
    p = decrypt(c, key)
    puts "#{n} --> #{c}"
end
(0..10).each do
    n = rand(65536)
    c = encrypt(n, key)
    p = decrypt(c, key)
    puts "#{n} --> #{c}"
end

一些例子:

0 --> 39031
1 --> 38273
2 --> 54182
3 --> 59129
4 --> 18743
5 --> 7628
6 --> 8978
7 --> 15474
8 --> 49783
9 --> 24614
10 --> 58570
1343 --> 19234
19812 --> 18968
6711 --> 31505
42243 --> 29837
62617 --> 52334
27174 --> 56551
3624 --> 31768
38685 --> 40918
27826 --> 42109
62589 --> 25562
20377 --> 2670

答案 5 :(得分:0)

一个简单的函数,它会整理较小的数字(如果你需要保持幅度):

    function switchquartets($n){
        return ((0x0000000F & $n) << 4) + ((0x000000F0& $n)>>4)
        + ((0x00000F00 & $n) << 4) + ((0x0000F000& $n)>>4)
        + ((0x000F0000 & $n) << 4) + ((0x00F00000& $n)>>4)
        + ((0x0F000000 & $n) << 4) + ((0xF0000000& $n)>>4);
    }

答案 6 :(得分:0)

“双平方”方法:

function dsCrypt($input,$decrypt=false) {
            $o = $s1 = $s2 = array(); // Arrays for: Output, Square1, Square2
            // формируем базовый массив с набором символов
            $basea = array('?','(','@',';','$','#',"]","&",'*'); // base symbol set
            $basea = array_merge($basea, range('a','z'), range('A','Z'), range(0,9) );
            $basea = array_merge($basea, array('!',')','_','+','|','%','/','[','.',' ') );
            $dimension=9; // of squares
            for($i=0;$i<$dimension;$i++) { // create Squares
                for($j=0;$j<$dimension;$j++) {
                    $s1[$i][$j] = $basea[$i*$dimension+$j];
                    $s2[$i][$j] = str_rot13($basea[($dimension*$dimension-1) - ($i*$dimension+$j)]);
                }
            }
            unset($basea);
            $m = floor(strlen($input)/2)*2; // !strlen%2
            $symbl = $m==strlen($input) ? '':$input[strlen($input)-1]; // last symbol (unpaired)
            $al = array();
            // crypt/uncrypt pairs of symbols
            for ($ii=0; $ii<$m; $ii+=2) {
                $symb1 = $symbn1 = strval($input[$ii]);
                $symb2 = $symbn2 = strval($input[$ii+1]);
                $a1 = $a2 = array();
                for($i=0;$i<$dimension;$i++) { // search symbols in Squares
                    for($j=0;$j<$dimension;$j++) {
                        if ($decrypt) {
                            if ($symb1===strval($s2[$i][$j]) ) $a1=array($i,$j);
                            if ($symb2===strval($s1[$i][$j]) ) $a2=array($i,$j);
                            if (!empty($symbl) && $symbl===strval($s2[$i][$j])) $al=array($i,$j);
                        }
                        else {
                            if ($symb1===strval($s1[$i][$j]) ) $a1=array($i,$j);
                            if ($symb2===strval($s2[$i][$j]) ) $a2=array($i,$j);
                            if (!empty($symbl) && $symbl===strval($s1[$i][$j])) $al=array($i,$j);
                        }
                    }
                }
                if (sizeof($a1) && sizeof($a2)) {
                    $symbn1 = $decrypt ? $s1[$a1[0]][$a2[1]] : $s2[$a1[0]][$a2[1]];
                    $symbn2 = $decrypt ? $s2[$a2[0]][$a1[1]] : $s1[$a2[0]][$a1[1]];
                }
                $o[] = $symbn1.$symbn2;
            }
            if (!empty($symbl) && sizeof($al)) // last symbol
                $o[] = $decrypt ? $s1[$al[1]][$al[0]] : $s2[$al[1]][$al[0]];
            return implode('',$o);
        }
    echo dsCrypt('586851105743');
    echo '<br />'.dsCrypt('tdtevmdrsdoc', 1);