PHP   发布时间:2019-11-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了将PHP Rijndael算法重写为Java(Android)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要在 Javaphp中编码一个字符串,其结果必须相同.

给出以下条件:

>算法:RIJNDAEL-128
>键:5P443m2Q1R9A7f5r3e1z08642
>模式:ECB
>初始化向量:N / A(因为我们使用的是ECB,所以忽略了IV)

要编码的字符串:201412181656005P443m2Q1R9A7f5r3e1z08642

PHP

<?php
        class Cipher
        {
            private $securekey,$iv;

            function __construct($textkey)
            {
                $this->securekey = $textkey;
                $this->iv = mcrypt_create_iv(32);
            }

            function encryptR($input)
            {
                $enc = mcrypt_encrypt(MCRYPT_RIJNDAEL_128,$this->securekey,$input,MCRYPT_MODE_ECB,$this->iv);
                return base64_encode($enc);
            }

            function decryptR($input)
            {
                return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,base64_decode($input),$this->iv));
            }
        }

        $raw_text = '201412181656005P443m2Q1R9A7f5r3e1z08642';
        $secretKey = '5P443m2Q1R9A7f5r3e1z08642';

        $cipher = new Cipher($secretKey);
        $encrypted = $cipher->encryptR($raw_text);     
?>

输出:MbDHhIanWgySlMTOX ItgVKudVLXbtj7ig2GMQacVM9JhyAPvVQxLJnHpEj / vhqW

JAVA

encrypted = encrypt("201412181656005P443m2Q1R9A7f5r3e1z08642","5P443m2Q1R9A7f5r3e1z08642");

public class Crypt {

    private final String characterEncoding = "UTF-8";
    private final String cipherTransformation = "AES/ECB/PKCS5Padding";
    private final String aesEncryptionAlgorithm = "AES";

    public  byte[] decrypt(byte[] cipherText,byte[] key) throws Exception
    {
        Cipher cipher = Cipher.geTinstance(cipherTransformation);
        SecretKeySpec secretKeySpecy = new SecretKeySpec(key,aesEncryptionAlgorithm);
        cipher.init(Cipher.DECRYPT_MODE,secretKeySpecy);
        cipherText = cipher.doFinal(cipherText);
        return cipherText;
    }

    public byte[] encrypt(byte[] plaintext,byte[] key) throws Exception
    {
        Cipher cipher = Cipher.geTinstance(cipherTransformation);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key,aesEncryptionAlgorithm);
        cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
        plaintext = cipher.doFinal(plaintext);
        return plaintext;
    }

    private byte[] getKeyBytes(String key) throws UnsupportedEncodingException{
        byte[] keyBytes= new byte[16];
        byte[] parameterKeyBytes= key.getBytes(characterEncoding);
        System.arraycopy(parameterKeyBytes,keyBytes,Math.min(parameterKeyBytes.length,keyBytes.length));
        return keyBytes;
    }

    @SuppressLint("NewApi")
    public String encrypt(String plaintext,String key) throws Exception {
        byte[] plaintextbytes = plaintext.getBytes(characterEncoding);
        byte[] keyBytes = getKeyBytes(key);
        // Log.i("iv",""+keyBytesIV);
        return Base64.encodeToString(encrypt(plaintextbytes,keyBytes),Base64.DEFAULT);
    }

    @SuppressLint("NewApi")
    public String decrypt(String encryptedText,String key) throws Exception {
        byte[] cipheredBytes = Base64.decode(encryptedText,Base64.DEFAULT);
        byte[] keyBytes = getKeyBytes(key);

        return new String(decrypt(cipheredBytes,characterEncoding);
    }

}

输出:wd0FHYpLbgdpHhcSql7VVCiKWJWN5hvP0W9F4sgKWAWeDcSjvfKWTM5LHBCZJSRw

更新:

我将填充从NoPadding更改为PKCS5Padding

它是否正确?我不确定,因为你看看php代码.没有指定任何填充(我自己基于语法的假设).

Info on Mcrypt

额外洞察力:

阅读此document关于填充(无填充).一定是和这个问题有关.

解决方法

看起来您的php版本使用AES-128,根据定义,它使用128位(16字节)密钥.然而,看起来你传入了一个25字节的密钥(5P443m2Q1R9A7f5r3e1z08642),我不确定当发生这种情况时php会做什么.

您的Java版本的getKeyBytes()方法仅返回提供的密钥的前16个字节,因此仅使用它加密.

尝试将php版本中的密钥截断为5P443m2Q1R9A7f5r,您将得到相同的结果.除了可能不同的末端部分.那时,问题就是填充.您可以在明文上应用pkcs5_pad php函数,使其与您的Java版本匹配.

所有这一切,如果这只是为了学习目的,那没关系.否则,对于实际使用,重要的是你do not use ECB cipher mode.

大佬总结

以上是大佬教程为你收集整理的将PHP Rijndael算法重写为Java(Android)全部内容,希望文章能够帮你解决将PHP Rijndael算法重写为Java(Android)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。