首页
社区
课程
招聘
[求助]python android java 无法解密aes算法的问题
发表于: 2016-4-19 10:04 15711

[求助]python android java 无法解密aes算法的问题

2016-4-19 10:04
15711
最近在用python java解密 aes密文,发现个问题
相同的密钥 相同的算法,相同的padding 居然无法解密(但是android的可以解密) ,实在找不到原因,请大牛帮忙 。类似的问题是  这些程序的加密后的结果也不同  网上找过资料  什么密钥长度问题 padding问题  这些都考虑到了  但是还是找不到原因

python代码:(报错是 M2Crypto.EVP.EVPError: bad decrypt)
from M2Crypto.EVP import Cipher
from base64 import b64encode, b64decode
from M2Crypto import util  
 
key = 'This is the super secret key 123'
iv = '\0' * 16
ciphertext = b64decode('DTrW2VXjSoFdg0e61fHxJg==')

decipher = Cipher(alg='aes_128_cbc', key=key, op=0, iv=iv)
decipher.set_padding(padding = 1)
plaintext = decipher.update(ciphertext)
plaintext += decipher.final()
print plaintext


Java代码:(提示错误 Given final block not properly padded)
public static String Decrypt(String sSrc, String sKey) throws Exception {
        try {
            // 判断Key是否正确
            if (sKey == null) {
                System.out.print("Key为空null");
                return null;
            }
            // 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                //return null;
            }
            byte[] raw = sKey.getBytes();
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            IvParameterSpec iv = new IvParameterSpec("0000000000000000"
                    .getBytes());
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);//先用base64解密
            try {
                byte[] original = cipher.doFinal(encrypted1);
                String originalString = new String(original);
                return originalString;
            } catch (Exception e) {
                System.out.println(e.toString());
                return null;
            }
        } catch (Exception ex) {
            System.out.println(ex.toString());
            return null;
        }
    }

    public static void main(String[] args) throws Exception {
        /*
         * 加密用的Key 可以用26个字母和数字组成,最好不要用保留字符,虽然不会错,至于怎么裁决,个人看情况而定
         * 此处使用AES-128-CBC加密模式,key需要为16位。
         */
    	//String cKey = "1234567890123456";
    	String cKey = "This is the super secret key 123";
        
        // 解密
        lStart = System.currentTimeMillis();
        String enString2 = "DTrW2VXjSoFdg0e61fHxJg==";
        String DeString = AESwithBASE64.Decrypt(enString2, cKey);
        System.out.println("解密后的字串是:" + DeString);
        lUseTime = System.currentTimeMillis() - lStart;
        System.out.println("解密耗时:" + lUseTime + "毫秒");
    }


Android代码:

package com.android.dns.exploitaes;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    TextView textViewAESPassword;
    TextView textViewPlainPassword ;
    Button exploitAes_buttons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final String key = "This is the super secret key 123";
        final String theString="DTrW2VXjSoFdg0e61fHxJg==";
        //	The initialization vector used by the encryption function
        final byte[] ivBytes = {
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        };

        textViewAESPassword = (TextView)findViewById(R.id.textViewAESPassword);
        textViewPlainPassword = (TextView)findViewById(R.id.textViewPlainPassword);

        textViewAESPassword.setText(theString);
        final String[] plainText = new String[1];
        final byte[][] cipherData = new byte[1][1];
        exploitAes_buttons = (Button) findViewById(R.id.buttonExploit);

        exploitAes_buttons.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                try {
                    byte[] keyBytes = key.getBytes("UTF-8");
                    cipherData[0] = MainActivity.aes256decrypt(ivBytes, keyBytes, Base64.decode(theString.getBytes("UTF-8"), Base64.DEFAULT));
                    plainText[0] = new String(cipherData[0], "UTF-8");
                    Toast.makeText(getApplicationContext(), plainText[0], Toast.LENGTH_LONG).show();
                    textViewPlainPassword.setText(plainText[0]);



                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvalidKeyException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchAlgorithmException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (NoSuchPaddingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (InvalidAlgorithmParameterException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalBlockSizeException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (BadPaddingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });


    }
    /*
    The function that handles the aes256 decryption.
    ivBytes: Initialization vector used by the decryption function
    keyBytes: Key used as input by the decryption function
    textBytes: Ciphertext input to the decryption function
    */
    public static byte[] aes256decrypt(byte[] ivBytes, byte[] keyBytes, byte[] textBytes)
            throws java.io.UnsupportedEncodingException,
            NoSuchAlgorithmException,
            NoSuchPaddingException,
            InvalidKeyException,
            InvalidAlgorithmParameterException,
            IllegalBlockSizeException,
            BadPaddingException {

        AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
        SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
        return cipher.doFinal(textBytes);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}


[课程]Android-CTF解题方法汇总!

收藏
免费 0
支持
分享
最新回复 (12)
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
Android代码 用的是aes 256,python里面的是aes 128, java代码里面的iv byte不为0
2016-4-19 10:56
0
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
python:
      decipher = Cipher(alg='aes_256_cbc', key=key, op=0, iv=iv)
java:
      final byte[] ivBytes = {
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
        };
      IvParameterSpec iv = new IvParameterSpec(ivBytes);
2016-4-19 11:46
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
[QUOTE=msccllss;1425839]python:
      decipher = Cipher(alg='aes_256_cbc', key=key, op=0, iv=iv)
java:
      final byte[] ivBytes = {
                0x00, 0x00, 0x00, 0x00, ...[/QUOTE]

非常感谢  还是细节问题没注意到    感谢
2016-4-19 13:56
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
我补充下 java支持情况
AES/CBC/NoPadding (128)
AES/CBC/PKCS5Padding (128)
AES/ECB/NoPadding (128)
AES/ECB/PKCS5Padding (128)
DES/CBC/NoPadding (56)
DES/CBC/PKCS5Padding (56)
DES/ECB/NoPadding (56)
DES/ECB/PKCS5Padding (56)
DESede/CBC/NoPadding (168)
DESede/CBC/PKCS5Padding (168)
DESede/ECB/NoPadding (168)
DESede/ECB/PKCS5Padding (168)
RSA/ECB/PKCS1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)
2016-4-19 14:18
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
6
请问下如果java里面改256位 需要在哪改啊
2016-4-19 14:35
0
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
// 判断Key是否为16位
            if (sKey.length() != 16) {
                System.out.print("Key长度不是16位");
                //return null;
            }

把这段删除就行
2016-4-19 15:00
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
java 这个默认是256加密么? 我查的那个官方文档是128加密啊?
2016-4-19 15:21
0
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
9
Every implementation of the Java platform is required to support the following standard Cipher transformations with the keysizes in parentheses:
AES/CBC/NoPadding (128)
AES/CBC/PKCS5Padding (128)
AES/ECB/NoPadding (128)
AES/ECB/PKCS5Padding (128)
DES/CBC/NoPadding (56)
DES/CBC/PKCS5Padding (56)
DES/ECB/NoPadding (56)
DES/ECB/PKCS5Padding (56)
DESede/CBC/NoPadding (168)
DESede/CBC/PKCS5Padding (168)
DESede/ECB/NoPadding (168)
DESede/ECB/PKCS5Padding (168)
RSA/ECB/PKCS1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)

你应该是少看了前面一句话了,是至少要实现的算法
AES is a 128-bit block cipher supporting keys of 128, 192, and 256 bits
2016-4-19 15:30
0
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
10
String cKey = "This is the super secret key 123";
这个刚好是32个字符,256位哦
2016-4-19 15:31
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
11
嗯嗯 谢谢
假设不看密钥  怎么看出这是256位呢?  或者说在哪指定长度?  包括刚才您说的if那个判断长度 实际这些都不是最关键的吧?
2016-4-19 15:59
0
雪    币: 100
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
12
好像只能根据传入的密钥长度来判断的
2016-4-19 16:18
0
雪    币: 35
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
13
我试了下 减少了密钥长度也可以处理的
2016-4-19 17:20
0
游客
登录 | 注册 方可回帖
返回
//