前进的轮胎 發表於 2021-8-29 17:32:00

Java & PHP 3DES 互通加密、解密

<p>3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。</p>

<p>3DES又称Triple DES,是DES加密算法的一种模式,它使用2条不同的56位的密钥对数据进行三次加密。</p>
<p>数据加密标准(DES)是美国的一种由来已久的加密标准,它使用对称密钥加密法,并于1981年被ANSI组织规范为ANSI X.3.92。</p>
<p>DES使用56位密钥和密码块的方法,而在密码块的方法中,文本被分成64位大小的文本块然后再进行加密。比起最初的DES,3DES更为安全。</p>
<p>3DES(即Triple DES)是DES向AES过渡的加密算法(1999年,NIST将3-DES指定为过渡的加密标准),加密算法,其具体实现如下:设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,P代表明文,C代表密文,这样:</p>
<ul>
<li>
<p>3DES加密过程为:C=Ek3(Dk2(Ek1(P)))</p>
</li>
<li>
<p>3DES解密过程为:P=Dk1(EK2(Dk3(C)))</p>
</li>
</ul>
<h1 id="版本">版本</h1>
<table>
<thead>
<tr>
<th style="text-align: center">语言</th>
<th style="text-align: center">版本</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align: center">PHP</td>
<td style="text-align: center">7.3.11</td>
</tr>
<tr>
<td style="text-align: center">Java</td>
<td style="text-align: center">1.8.0_231</td>
</tr>
</tbody>
</table>
<h1 id="加密">加密</h1>
<h2 id="php">PHP</h2>
<p><strong>CryptUtil.encrypt</strong></p>
<pre><code class="language-php">public static function encrypt($data, $key)
{
$strPadded = $data;
if (strlen($strPadded) % 8) {
    $strPadded = str_pad($strPadded,strlen($strPadded) + 8 - strlen($strPadded) % 8, "\0");
}
$key = self::getKey($key);
$encData = openssl_encrypt($strPadded, self::CIPHER_ALGO, $key, self::OPTIONS, self::IV);
return base64_encode($encData);
}
</code></pre>
<h2 id="java">Java</h2>
<p><strong>CryptUtil.encryptBy3DesAndBase64</strong></p>
<pre><code class="language-java">public static String encryptBy3DesAndBase64(String content, String keyStr, String encoding) throws Exception {
byte output[] = null;
byte input[] = null;
int residue = (content.getBytes(encoding).length) % 8;
if (0 != residue) {
    int padLen = 8 - residue;
    StringBuffer strBuf = new StringBuffer(content);
    for (int i = 0; i &lt; padLen; i++) {
      strBuf.append(' ');
    }
    input = (new String(strBuf)).getBytes(encoding);
} else {
    input = content.getBytes(encoding);
}
output = encryptBy3Des(input, keyStr);
return Base64.encode(output).replaceAll("[\\n\\r]", "");
}
</code></pre>
<h1 id="解密">解密</h1>
<h2 id="php-1">PHP</h2>
<p><strong>CryptUtil.decrypt</strong></p>
<pre><code class="language-php">public static function decrypt($data, $key)
{
$key   = self::getKey($key);
$data    = base64_decode($data);
$decData = openssl_decrypt($data, self::CIPHER_ALGO, $key, self::OPTIONS, self::IV);
return trim($decData);
}
</code></pre>
<h2 id="java-1">Java</h2>
<p><strong>CryptUtil.decryptBy3DesAndBase64</strong></p>
<pre><code class="language-java">public static String decryptBy3DesAndBase64(String content, String keyStr, String encoding) throws Exception {
byte output[] = null;
byte input[] = null;
input = Base64.decode(content);
output = decryptBy3Des(input, keyStr);
String retStr = new String(output, encoding);
return (retStr.trim());
}
</code></pre>
<h1 id="demo">Demo</h1>
<p>完整 Demo 源码:BNDong/demo/PhpJava3DES</p><br><br>
来源:https://www.cnblogs.com/bndong/p/15203080.html
頁: [1]
查看完整版本: Java & PHP 3DES 互通加密、解密