![]() |
||
Major Member
![]() 加入日期: Sep 2002
文章: 155
|
人腦debug太累
可以把exception的log 貼出來嗎 |
|||||||
![]() |
![]() |
*停權中*
加入日期: Dec 2010
文章: 341
|
引用:
其實主要 執行到 代碼:
System.out.print( "input to Hex to byte to 32 hexstr2arrayb : "+hexstr2arrayb(seed,64)); System.out.println(); 就會exception 我是用System.out.print來測試hexstr2arrayb 然後exception是說 Exception in thread "main" java.lang.NullPointerException at aestest.hexstr2arrayb(aestest.java:271) at aestest.encrypt(aestest.java:99) at aestest.main(aestest.java:35) 話說找到在java補0的方法了 可是範例上是說16bit的補0 如果要強制補0到128要怎麼補? 引用:
代碼:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author Jacker */ import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import sun.misc.BASE64Decoder; public class Encryption { public static void main(String args[]) throws Exception { System.out.println(encrypt()); System.out.println(desEncrypt()); } public static String encrypt() throws Exception { try { String data = "Test String"; String key = "1234567812345678"; String iv = "1234567812345678"; Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new sun.misc.BASE64Encoder().encode(encrypted); } catch (Exception e) { e.printStackTrace(); return null; } } public static String desEncrypt() throws Exception { try { String data = "2fbwW9+8vPId2/foafZq6Q=="; String key = "1234567812345678"; String iv = "1234567812345678"; byte[] encrypted1 = new BASE64Decoder().decodeBuffer(data); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original); return originalString; } catch (Exception e) { e.printStackTrace(); return null; } } } 怪異的是 這一篇又說可以 ZeroBytePadding http://blog.csdn.net/whu_zhangmin/a...etails/30284809 代碼:
package com.example.crypto; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; /** * Created with IntelliJ IDEA. * User: zhangmin * Module: Safe * Date: 14-6-9 * Time: 下午3:13 */ public class AESCrypto { 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/ECB/ZeroBytePadding"); 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/ECB/ZeroBytePadding"); 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)); } } 謝謝 此文章於 2014-06-23 12:49 AM 被 公不 編輯. |
||||
![]() |
![]() |
Major Member
![]() 加入日期: Sep 2002
文章: 155
|
雖然我不是專業的
debug還可以啦 byte[] data = null; data[j / 2] = (byte) ((Character.digit(input.charAt(j), 16) << 4) + Character.digit(input.charAt(j+1), 16)); 陣列不能這樣寫啦,你要先給他new 個instance 如byte[] = new byte[10] 才能用data[j/2] 不然data[]是空的,data[j/2]當然會null exception 此文章於 2014-06-23 01:13 AM 被 stalin 編輯. |
![]() |
![]() |
Amateur Member
![]() 加入日期: Jun 2002 您的住址: 太陽系
文章: 32
|
|
![]() |
![]() |