JAVA和PHP的3DES加密算法

2022-10-11 21:17:32 170 0
魁首哥

JAVA 和php对等的3DES加密算法,ECB的加密模式没有CBC安全,iv是初始向量相当于种子

JAVA代码:

JAVA和PHP的3DES加密算法

package org.jamie.demo;

import java.security.NoSuchAlgorithmException;

import java.security.Security;

import java.util.Random;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;

import javax.crypto.SecretKeyFactory;

import javax.crypto.spec.DESedeKeySpec;

import javax.crypto.spec.IvParameterSpec;

@SuppressWarnings(“restriction”)

public class TripleDES {

static {

Security.addProvider(new com.sun.crypto.provider.SunJCE());

}

private static final String MCRYPT_TRIPLEDES = “DESede”;

private static final String TRANSFORMATION = “DESede/CBC/PKCS5Padding”;

public static Byte [] decrypt(byte[] data, byte[] key, byte[] iv) throws Exception {

DESedeKeySpec spec = new DESedeKeySpec(key);

SecretKeyFactory keyFactory = SecretKeyFactory. getInstance (MCRYPT_TRIPLEDES);

SecretKey sec = keyFactory.generateSecret(spec);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);

IvParameterSpec IvParameters = new IvParameterSpec(iv);

cipher.init(Cipher.DECRYPT_MODE, sec, IvParameters);

return cipher.doFinal(data);

}

public static byte[] encrypt(byte[] data, byte[] key, byte[] iv) throws Exception {

DESedeKeySpec spec = new DESedeKeySpec(key);

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(“DESede”);

SecretKey sec = keyFactory.generateSecret(spec);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);

IvParameterSpec IvParameters = new IvParameterSpec(iv);

cipher.init(Cipher.ENCRYPT_MODE, sec, IvParameters);

return cipher.doFinal(data);

}

public static byte[] generateSecretKey() throws NoSuchAlgorithmException {

KeyGenerator keygen = KeyGenerator.getInstance(MCRYPT_TRIPLEDES);

return keygen.generateKey().getEncoded();

}

public static byte[] randomIV bytes () {

Random ran = new Random();

byte[] bytes = new byte[8];

for (int i = 0; i < bytes.length; ++i) {

bytes[i] = (byte) ran.nextInt(Byte.MAX_VALUE + 1);

}

return bytes;

}

public static void main(String args[]) throws Exception {

String plainText = “a12*&1c中文”;

final byte[] secretBytes = TripleDES.generateSecretKey();

final byte[] ivbytes = TripleDES.randomIVBytes();

System.out.println(“plain text: ” + plainText);

byte[] encrypt = TripleDES.encrypt(plainText.getBytes(), secretBytes, ivbytes);

System.out.println(“cipher text: ” + encrypt);

System.out.println(“decrypt text: ” + new String(TripleDES.decrypt(encrypt, secretBytes, ivbytes), “UTF-8”));

}

}

PHP代码:

class TripleDES {

public static function genIvParameter() {

return mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_TRIPLEDES,MCRYPT_MODE_CBC), MCRYPT_RAND);

}

private static function pkcs5Pad($text, $blocksize) {

$pad = $blocksize – (strlen($text) % $blocksize); // in php, strlen returns the bytes of $text

return $text . str_repeat(chr($pad), $pad);

}

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

}

public static function encryptText($plain_text, $key, $iv) {

$padded = TripleDES::pkcs5Pad($plain_text, mcrypt_get_block_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_CBC));

return mcrypt_encrypt(MCRYPT_TRIPLEDES, $key, $padded, MCRYPT_MODE_CBC, $iv);

}

public static function decryptText($cipher_text, $key, $iv) {

$plain_text = mcrypt_decrypt(MCRYPT_TRIPLEDES, $key, $cipher_text, MCRYPT_MODE_CBC, $iv);

return TripleDES::pkcs5Unpad($plain_text);

}

};

/*

function main() {

$iv = TripleDES::genIvParameter();

print “\$iv=$iv\n”;

$plain_text=”this is a test,包括中文”;

$key=”ABCDEFGHIJ0123456789ABCD”; //此处要注意key 必须是24位的,不够的要补全

$cipher_text = TripleDES::encryptText($plain_text, $key, $iv);

print “\$cipher_text=$cipher_text\n”;

$plain_text = TripleDES::decryptText($cipher_text, $key, $iv);

print “\$plain_text=$plain_text\n”;

}

main();

*/

?>

收藏
分享
海报
0 条评论
170
上一篇:PHP基础之在Dreamweaver下使用PHP实现文件上传 下一篇:谈谈:高端网站定制开发的流程和步骤有哪些?

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码