由网友(心如薄荷天然凉)分享简介:我相信这是从段 androidsnippets.org - 这是为什么不funcioning在Android上的 2.3 ?如何解决这个问题?错误 03-05 23:19:17.479:警告/ System.err的(3598):javax.crypto.BadPaddingException:垫块损坏03-05...
我相信这是从段 androidsnippets.org - 这是为什么不funcioning在Android上的 2.3 ?如何解决这个问题?
错误
03-05 23:19:17.479:警告/ System.err的(3598):javax.crypto.BadPaddingException:垫块损坏
03-05 23:19:17.518:WARN / System.err的(3598):在org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:715)
03-05 23:19:17.518:WARN / System.err的(3598):在javax.crypto.Cipher.doFinal(Cipher.java:1090)
我发现了关于这一主题的几个答案,但我无法找到一个体面的方式如何确定这一点,添加 NoPadding 参数或不同的算法或..?
公共静态字符串code(字符串stringTo code){
尝试 {
stringTo code =加密(somekey,stringTo code);
}赶上(例外E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
返回stringTo code;
}
公共静态字符串德code(字符串stringToDe code){
尝试 {
stringToDe code =解密(somekey,stringToDe code);
}赶上(例外E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
返回stringToDe code;
}
公共静态字符串加密(字符串种子,字符串明文)抛出异常{
byte []的rawKey = getRawKey(seed.getBytes());
byte []的结果=加密(rawKey,cleartext.getBytes());
返回toHex(结果);
}
公共静态字符串解密(字符串种子,字符串加密)抛出异常{
byte []的rawKey = getRawKey(seed.getBytes());
byte []的ENC = toByte(加密);
byte []的结果=解密(rawKey,ENC);
返回新的字符串(结果);
}
私有静态的byte [] getRawKey(byte []的种子)抛出异常{
的KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecureRandom的SR = SecureRandom.getInstance(SHA1PRNG);
sr.setSeed(种子);
kgen.init(128,SR); // 192和256位可能无法使用
SecretKey的SKEY = kgen.generateKey();
byte []的原料= skey.getEn codeD();
回归原始;
}
私有静态byte []的加密(byte []的原料,byte []的明确)抛出异常{
SecretKeySpec skeySpec =新SecretKeySpec(原AES);
密密码= Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
byte []的加密= cipher.doFinal(清晰);
返回加密;
}
私有静态的byte []解密(byte []的原料,byte []的加密)抛出异常{
SecretKeySpec skeySpec =新SecretKeySpec(原AES);
密密码= Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
byte []的解密= cipher.doFinal(加密);
返回解密;
}
公共静态字符串toHex(字符串TXT){
返回toHex(txt.getBytes());
}
公共静态字符串fromHex(十六进制字符串){
返回新的String(toByte(十六进制));
}
公共静态的byte [] toByte(字符串十六进制串){
INT的len = hexString.length()/ 2;
byte []的结果=新的字节[长度];
的for(int i = 0; I< LEN;我++)
结果[I] = Integer.valueOf(hexString.substring(2 * I,2 * I + 2),16).byteValue();
返回结果;
}
公共静态字符串toHex(byte []的BUF){
如果(BUF == NULL)
返回 ;
StringBuffer的结果=新的StringBuffer(2 * buf.length);
的for(int i = 0; I< buf.length;我++){
appendHex(结果,BUF [I]);
}
返回result.toString();
}
私人最终静态字符串HEX =0123456789ABCDEF;
私有静态无效appendHex(StringBuffer的某人,字节B){
sb.append(HEX.charAt((B>> 4)及为0x0F))。追加(HEX.charAt(B&安培;为0x0F));
}
解决方案
这是问题BouncyCastle的(BouncyCastle升级到1.45 时AES错误)。
我所发现的是,这 结果在卑诗省两个不同的值 1.34 VS 1.45。
我找不到解决办法,但...
I believe this is a snippet from androidsnippets.org - why this isn't funcioning on Android 2.3? How to fix it?
Error
03-05 23:19:17.479: WARN/System.err(3598): javax.crypto.BadPaddingException: pad block corrupted
03-05 23:19:17.518: WARN/System.err(3598): at org.bouncycastle.jce.provider.JCEBlockCipher.engineDoFinal(JCEBlockCipher.java:715)
03-05 23:19:17.518: WARN/System.err(3598): at javax.crypto.Cipher.doFinal(Cipher.java:1090)
I found several answers on this topic but I couldn't find a decent way how to fix this, add NoPadding parameter or different algorithm or..?
public static String code(String stringToCode) {
try {
stringToCode = encrypt("somekey",stringToCode);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return stringToCode;
}
public static String decode(String stringToDecode) {
try {
stringToDecode = decrypt("somekey",stringToDecode);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return stringToDecode;
}
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));
}
解决方案
It's problem with BouncyCastle (BouncyCastle AES error when upgrading to 1.45).
What I have found is that this results in two different values for BC 1.34 vs 1.45.
I couldn't find the solution, yet...
相关推荐
最新文章