好多人 android 使用上述 AES 显现 javax.crypto.BadPaddingException: pad block corrupted 下面的代码发布没问题,比较自己。不解释!

public static class cryptogram{

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", "Crypto");

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,new IvParameterSpec(new byte[cipher.getBlockSize()]));

   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,new IvParameterSpec(new byte[cipher.getBlockSize()]));

   byte[] decrypted = cipher.doFinal(encrypted);

return decrypted;

}





private static String toHex(String txt) {

return toHex(txt.getBytes());

}

private static String fromHex(String hex) {

return new String(toByte(hex));

}



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

}





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

}

}

版权声明:本文博主原创文章,博客,未经同意不得转载。

左右 android AES 所述机器的一部分 javax.crypto.BadPaddingException: pad block corrupted的更多相关文章

  1. android AES 部分机器javax.crypto.BadPaddingException: pad block corrupted

    package com.bbguoxue.poetry.util; import java.security.SecureRandom; import javax.crypto.Cipher; imp ...

  2. android 上AES解密是报错javax.crypto.BadPaddingException: pad block corrupted

    网上看到两种方法: 1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES"); private sta ...

  3. Android AES加密报错处理:javax.crypto.IllegalBlockSizeException: error:1e00007b:Cipher functions:OPENSSL_internal:WRONG_FINAL_BLOCK_LENGTH

    一.问题说明 今天写AES加/解密功能的apk,设想是四个控件(测试用的,界面丑这种东西请忽略) 一个编缉框----用于输入要加密的字符串 一个文本框----用于输出加密后的字符串,和加密后点击解密按 ...

  4. android 开发解密时出现pad block corrupted 错误

    情景:在虚拟机上运行正常的,但是到我的真机上就解密失败,出现pad block corrupted  ,据说是版本原因:我机器是小米3 最新版的android  4.2 出现问题的代码: privat ...

  5. Android AES加密工具类实现(基础回顾)

    package com.powercreator.cms.util; import java.security.SecureRandom; import javax.crypto.Cipher; im ...

  6. Android AES加密算法及事实上现

    昨天老大叫我看看android加密算法.于是网上找了找,找到了AES加密算法.(当然还有MD5,BASE64什么的http://snowolf.iteye.com/blog/379860这篇文章列举了 ...

  7. Android AES加密算法,现在实际上

    昨天,老板让我来看看android加密算法.于是在网上找了找,发现AES加密算法.(当然,MD5,BASE64什么http://snowolf.iteye.com/blog/379860这篇文章列举了 ...

  8. android AES 加密解密

    import java.security.Provider; import java.security.SecureRandom; import javax.crypto.Cipher; import ...

  9. Android AES加密算法及其实现

    找到了AES加密算法.(当然还有MD5,BASE64什么的http://snowolf.iteye.com/blog/379860这篇文章列举了很多,但是基本都是j2se平台的,android平台不一 ...

随机推荐

  1. Codeforces 325D

    #include <cstdio> #include <algorithm> #include <cstring> #include <cstdlib> ...

  2. poj1503---大数加法

    先讲一种错误的做法:WA了n次,大神一定帮我看一下//看到有说数组大小开到250,我改了之后还是不//思路是将arr这个数组的每一行附上输入的值,然后求每列所有数之和,当然进位 //maxlen记录这 ...

  3. #include <time.h>

    1 _strtime 2 difftime 3 srand 4 time 1 _strtime 函数简介 函数名: _strtime 头文件: time.h 函数原型: char * _strtime ...

  4. ios模拟器安装.app

    相对于xcode的run,然后再在安装到模拟器上测试,如果是个人开发的话,那还好. 要是是团队开发,那每次其他的童鞋就都需要update最新的文件下来再编译运行了. 而且,一些测试的童鞋也不会打开xc ...

  5. [置顶] java 通过classloader加载类再通过classforname实例化

    自己写一个web服务器,发现class.forName用不了,找不到类,原因是找不到类路径, 可以过加载需要的类的文件路径,然后再通过ClassLoader来加载所需要的类.其中的原 理就不多说了,下 ...

  6. 分布式-在win7上安装linux操作系统

    由于大多数的分布式计算框架.分布式存储系统.分布式平台都支持在unix类似的环境下.所以要做分布式的实验和学习的话,还是推荐装个linux系统.我这里安装的是ubuntu,由于之前装过这个. 一.下载 ...

  7. 【LeetCode】4Sum 解题报告

    [题目] Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d  ...

  8. google自定义站内搜索

    ttps://www.google.com/cse/docs/cref.html?hl=zh-cn 重要表单参数: action 字段:您希望存储结果的网址(在该例中,我们使用 http://www. ...

  9. BFS+状态压缩 HDU1429

    胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  10. SQLite语法

    一.建立数据库 sqlite3.exe test.db 二.双击sqlite-3_6_16目录下的程序sqlite3.exe,即可运行 三.退出 .exit 或者 .quit 四.SQLite支持如下 ...