pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

無法在android App中解密字符串

Unable to decrypt String in android App(無法在android App中解密字符串)
本文介紹了無法在android App中解密字符串的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試開發(fā)一個可以加密和解密值的 android 應(yīng)用程序.所以我關(guān)注了這個鏈接在此處輸入鏈接描述

I was trying to develop an android application that could encrypt and decrypt values. So I have followed this link enter link description here

到目前為止,我能夠加密文本,但無法解密它.在我的代碼中,我使用了提供的鏈接中提到的相同的 AESHelper 類.

So far I was able to encrypt a text but I was not able to decrypt it. In my code I have used the same AESHelper class which is mentioned in the link provided.

以下是我用來加密和解密值的活動類

The below is my activity class that i have used to Encrypt and decrypt the values

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {


    EditText text ;
    TextView encp,decriptom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        text = (EditText) findViewById(R.id.editText);
        encp = (TextView) findViewById(R.id.valueexcript);
        decriptom = (TextView) findViewById(R.id.deexcript);
    }

    @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);
    }


    public void Ecript(View v)
    {
        String Key = "avc";

        try {
           String normalTextEnc = AHShelper.encrypt(Key, text.getText().toString());
            Toast.makeText(this,normalTextEnc,Toast.LENGTH_LONG).show();

            encp.setText(normalTextEnc);


        } catch (Exception e) {
            e.printStackTrace();
        }
      //  Toast.makeText(this,"Hello",Toast.LENGTH_LONG).show();


        String decript;
        try {
            decript = AHShelper.decrypt(Key,encp.getText().toString());
            decriptom.setText(decript);
            Toast.makeText(this,decript,Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
        }




    }




}

我用過的AHShelper類如下

AHShelper class that i have used is below

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;


public class AHShelper {
    public static String encrypt(String seed, String cleartext)
            throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] result = encrypt(rawKey, cleartext.getBytes());
        return toHex(result);
    }

    public static String decrypt(String seed, String encrypted)
            throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] enc = toByte(encrypted);
        byte[] result = decrypt(rawKey, enc);
        return new String(result);
    }

    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        sr.setSeed(seed);
        kgen.init(128, sr); // 192 and 256 bits may not be available
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return raw;
    }

    private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;
    }

    private static byte[] decrypt(byte[] raw, byte[] encrypted)
            throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;
    }

    public static String toHex(String txt) {
        return toHex(txt.getBytes());
    }

    public static String fromHex(String hex) {
        return new String(toByte(hex));
    }

    public static byte[] toByte(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
                    16).byteValue();
        return result;
    }

    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private final static String HEX = "0123456789ABCDEF";

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }

}

推薦答案

基本上,這段代碼依賴于一個小技巧:如果你在使用之前為 SUN 提供者和 Bouncy Castle 提供者播種 SHA1PRNG,那么它總是會生成相同的隨機(jī)字節(jié)流.

Basically this code relies on a little trick: if you seed the SHA1PRNG for the SUN provider and Bouncy Castle provider before it is used then it will always generate the same stream of random bytes.

但并非所有提供商都如此;其他供應(yīng)商只是簡單地混合種子.換句話說,他們可以使用預(yù)先播種的 PRNG 并混合種子.在這種情況下,getRawKey 方法會為加密和解密生成不同的密鑰,這將導(dǎo)致解密失敗.

This is not always the case for every provider though; other providers simply mix in the seed. In other words, they may use a pre-seeded PRNG and mix-in the seed instead. In that case the getRawKey method generates different keys for the encrypt and decrypt, which will result in a failure to decrypt.

也可能是提供商決定完全使用基于 SHA-1 的不同算法,因為 SUN/Oracle 使用的算法沒有很好地指定 - 至少是公開的.

It could also be the case that a provider decides to use a different algorithm based on SHA-1 altogether, as the algorithm used by SUN/Oracle is not well specified - publicly at least.

基本上,這個可怕的代碼片段濫用 SHA1PRNG 作為密鑰派生函數(shù)或 KDF.如果輸入是密碼,則應(yīng)使用真正的 KDF,例如 PBKDF2;如果輸入是密鑰,則應(yīng)使用 HKDF.PBKDF2 內(nèi)置于 Java 中.

Basically this horrible code snippet abuses the SHA1PRNG as a Key Derivation Function or KDF. You should use a true KDF such as PBKDF2 if the input is a password or HKDF if the input is a key. PBKDF2 is build into Java.

應(yīng)該刪除該代碼段.它是從 Android 代碼片段中復(fù)制的,但我再也找不到那個網(wǎng)站了.換句話說,它似乎比它可用時更不正常.

That code snippet should be removed. It has been copied from Android snippets, but I cannot find that site anymore. It seems even more dysfunctional then when it was available in other words.

在使用 SUN 加密時檢索數(shù)據(jù)的一種可能解決方案是在 Oracle 提供的 JDK 上對其進(jìn)行解密.否則,您還可以復(fù)制 SHA1PRNG 的內(nèi)部實現(xiàn)類的代碼并使用它來解密您的數(shù)據(jù).請注意,您確實需要記住 SUN 的來源是 GPL 的;如果你這樣做,你需要遵守該許可證.對于較舊的 Android 版本,您可以使用其源代碼.我強(qiáng)烈建議以后刪除這段可怕的代碼,改用 PBKDF2.

A possible solution to retrieve your data when encrypted with SUN is either to decrypt it on an Oracle provided JDK. Otherwise you could also copy the code of the inner implementation class of the SHA1PRNG and use that to decrypt your data. Note that you do need to keep in mind that the sources of SUN are GPL'ed; you need to adhere to that license if you do. For older Android versions you can use the source code of that. I would strongly advice to remove this horrible piece of code afterwards and rely on PBKDF2 instead.

如果您使用的是返回完全隨機(jī)密鑰的實現(xiàn),那么您就完全不走運了.您的數(shù)據(jù)已消失,期間.請放心,我們將永遠(yuǎn)保密.

If you're using an implementation that returns a fully random key then you're completely out of luck. Your data is gone, period. Rest assured that it will be kept confidential to the end of times.

這篇關(guān)于無法在android App中解密字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 镀锌方管,无缝方管,伸缩套管,方矩管_山东重鑫致胜金属制品有限公司 | 阻垢剂,反渗透阻垢剂,缓蚀阻垢剂-山东普尼奥水处理科技有限公司 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 冷却塔降噪隔音_冷却塔噪声治理_冷却塔噪音处理厂家-广东康明冷却塔降噪厂家 | 阜阳在线-阜阳综合门户| 武汉印刷厂-不干胶标签印刷厂-武汉不干胶印刷-武汉标签印刷厂-武汉标签制作 - 善进特种标签印刷厂 | 广州/东莞小字符喷码机-热转印打码机-喷码机厂家-广州瑞润科技 | 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 澳洁干洗店加盟-洗衣店干洗连锁「澳洁干洗免费一对一贴心服务」 干洗加盟网-洗衣店品牌排行-干洗设备价格-干洗连锁加盟指南 | 中国品牌门窗网_中国十大门窗品牌_著名门窗品牌 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 冲击式破碎机-冲击式制砂机-移动碎石机厂家_青州市富康机械有限公司 | 立式硫化罐-劳保用品硫化罐-厂家直销-山东鑫泰鑫硫化罐厂家 | 美甲贴片-指甲贴片-穿戴美甲-假指甲厂家--薇丝黛拉 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 森旺-A级防火板_石英纤维板_不燃抗菌板装饰板_医疗板 | 闪电优家-卫生间防水补漏_酒店漏水渗水维修_防水堵漏公司 | 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 仓储笼_金属箱租赁_循环包装_铁网箱_蝴蝶笼租赁_酷龙仓储笼租赁 测试治具|过炉治具|过锡炉治具|工装夹具|测试夹具|允睿自动化设备 | 网站seo优化_seo云优化_搜索引擎seo_启新网络服务中心 | 代写标书-专业代做标书-商业计划书代写「深圳卓越创兴公司」 | 天津试验仪器-电液伺服万能材料试验机,恒温恒湿标准养护箱,水泥恒应力压力试验机-天津鑫高伟业科技有限公司 | 物和码官网,物和码,免费一物一码数字化营销SaaS平台 | 盘扣式脚手架-附着式升降脚手架-移动脚手架,专ye承包服务商 - 苏州安踏脚手架工程有限公司 | 房在线-免费房产管理系统软件-二手房中介房屋房源管理系统软件 | 宁夏活性炭_防护活性炭_催化剂载体炭-宁夏恒辉活性炭有限公司 | 便民信息网_家电维修,家电清洗,开锁换锁,本地家政公司 | 首页|专注深圳注册公司,代理记账报税,注册商标代理,工商变更,企业400电话等企业一站式服务-慧用心 | 大白菜官网,大白菜winpe,大白菜U盘装系统, u盘启动盘制作工具 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 小型气象站_便携式自动气象站_校园气象站-竞道气象设备网 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | 电梯乘运质量测试仪_电梯安全评估测试仪-武汉懿之刻 | 超声波焊接机_超音波熔接机_超声波塑焊机十大品牌_塑料超声波焊接设备厂家 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 送料机_高速冲床送料机_NC伺服滚轮送料机厂家-东莞市久谐自动化设备有限公司 | 探伤仪,漆膜厚度测试仪,轮胎花纹深度尺厂家-淄博创宇电子 |