加密和解密使用AES算法 [英] Encryption and decryption using AES Algorithms
问题描述
我想提出一个加密/解密模块为我的应用程序。我跟着本教程
这是不给任何错误,也没有显示输出。
日志文件
七月七日至23日:29:06.480:W / System.err的(795):javax.crypto.BadPaddingException:垫块损坏
七月七日至23日:29:06.629:W / System.err的(795):在com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:710)
七月七日至23日:29:06.629:W / System.err的(795):在javax.crypto.Cipher.doFinal(Cipher.java:1111)
七月七日至23日:29:06.660:W / System.err的(795):在com.example.generatesha384.AESHelper.decrypt(AESHelper.java:52)
七月七日至23日:29:06.690:W / System.err的(795):在com.example.generatesha384.AESHelper.decrypt(AESHelper.java:25)
七月七日至23日:29:06.690:W / System.err的(795):在com.example.generatesha384.MainActivity.onCreate(MainActivity.java:24)
七月七日至23日:29:06.700:W / System.err的(795):在android.app.Activity.performCreate(Activity.java:5133)
七月七日至23日:29:06.730:W / System.err的(795):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
七月七日至23日:29:06.730:W / System.err的(795):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
七月七日至23日:29:06.770:W / System.err的(795):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
七月七日至23日:29:06.770:W / System.err的(795):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
七月七日至23日:29:06.770:W / System.err的(795):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
七月七日至23日:29:06.800:W / System.err的(795):在android.os.Handler.dispatchMessage(Handler.java:99)
七月七日至23日:29:06.800:W / System.err的(795):在android.os.Looper.loop(Looper.java:137)
七月七日至23日:29:06.840:W / System.err的(795):在android.app.ActivityThread.main(ActivityThread.java:5103)
七月七日至23日:29:06.840:W / System.err的(795):在java.lang.reflect.Method.invokeNative(本机方法)
七月七日至23日:29:06.872:W / System.err的(795):在java.lang.reflect.Method.invoke(Method.java:525)
七月七日至23日:29:06.880:W / System.err的(795):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)
七月七日至23日:29:06.910:W / System.err的(795):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
七月七日至23日:29:06.910:W / System.err的(795):在dalvik.system.NativeStart.main(本机方法)
MainActivity.Java
字符串seedValue =这是MySecure@覆盖
保护无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); TextView的文本=(TextView的)findViewById(R.id.result); 字符串normalText =VIJAY;
串normalTextEnc; 尝试{
normalTextEnc = AESHelper.encrypt(seedValue,normalText);
字符串normalTextDec = AESHelper.decrypt(seedValue,normalTextEnc); 字符串textResult =普通文本::+ normalText
+\\ n加密值::+ normalTextEnc
+\\ n解密值::+ normalTextDec;
text.setText(textResult); }赶上(例外五){
// TODO自动生成catch块
e.printStackTrace();
}
}
AESHelper.Java
公共类AESHelper {公共静态加密字符串(种子字符串,字符串明文)
抛出异常{
字节[] = rawKey getRawKey(seed.getBytes(UTF-8));
字节[]结果=加密(rawKey,cleartext.getBytes(UTF-8));
返回toHex(结果);
}公共静态字符串解密(种子字符串,字符串加密)
抛出异常{
字节[] = rawKey getRawKey(seed.getBytes(UTF-8));
字节[] = ENC toByte(加密);
字节[]结果=解密(rawKey,ENC);
返回新的字符串(结果);
}私人静态的byte [] getRawKey(字节[]种子)抛出异常{
的KeyGenerator kgen = KeyGenerator.getInstance(AES);
SecureRandom的SR = SecureRandom.getInstance(SHA1PRNG);
sr.setSeed(种子);
kgen.init(128,SR); // 192和256位可能无法使用
SecretKey的SKEY = kgen.generateKey();
字节[] =原skey.getEn codeD();
返回原料;
}私人静态的byte []加密(字节[]生,字节[]清)抛出异常{
SecretKeySpec skeySpec =新SecretKeySpec(生,AES);
密密码= Cipher.getInstance(AES);
cipher.init(Cipher.ENCRYPT_MODE,skeySpec);
字节[] =加密cipher.doFinal(清晰);
返回加密;
}私人静态的byte []解密(字节[]生,字节[]加密)
抛出异常{
SecretKeySpec skeySpec =新SecretKeySpec(生,AES);
密密码= Cipher.getInstance(AES);
cipher.init(Cipher.DECRYPT_MODE,skeySpec);
字节[]解密= cipher.doFinal(加密);
Log.d(DEC,解密:+解密的);
返回解密;
}公共静态字符串toHex(字符串TXT){
返回toHex(txt.getBytes());
}公共静态字符串fromHex(十六进制字符串){
返回新的String(toByte(十六进制));
}公共静态的byte [] toByte(字符串十六进制串){
INT LEN = hexString.length()/ 2;
字节[]结果=新的字节[LEN]
的for(int i = 0; I< LEN,我++)
结果由[i] = Integer.valueOf(hexString.substring(2 * I 2 * I + 2),
16).byteValue();
返回结果;
}公共静态字符串toHex(字节[] buf中){
如果(BUF == NULL)
返回;
StringBuffer的结果=新的StringBuffer(2 * buf.length);
的for(int i = 0; I< buf.length;我++){
appendHex(结果,BUF [I]);
}
返回result.toString();
}私人最终静态字符串HEX =0123456789ABCDEF;私有静态无效appendHex(StringBuffer的某人,BYTE B){
sb.append(HEX.charAt((B>> 4)及为0x0F))。追加(HEX.charAt(B&安培;为0x0F));
}
}
AESHelper.java:52
字节[]解密= cipher.doFinal(加密);
和AESHelper.java:25
字节[]结果=解密(rawKey,ENC);
花了很多时间研究Java中的AES加密/解密后,我放在一起的所有信息下面的文章,我在计算器上阅读 - 的 http://netnix.org/2015/04/19 / AES加密,与-HMAC-诚信在的Java /
也许有帮助,也许它没有。
I am making an encryption/decryption module for my app. I followed This Tutorial
It is not giving any error and also not showing the output.
Logfile
07-23 07:29:06.480: W/System.err(795): javax.crypto.BadPaddingException: pad block corrupted
07-23 07:29:06.629: W/System.err(795): at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(BaseBlockCipher.java:710)
07-23 07:29:06.629: W/System.err(795): at javax.crypto.Cipher.doFinal(Cipher.java:1111)
07-23 07:29:06.660: W/System.err(795): at com.example.generatesha384.AESHelper.decrypt(AESHelper.java:52)
07-23 07:29:06.690: W/System.err(795): at com.example.generatesha384.AESHelper.decrypt(AESHelper.java:25)
07-23 07:29:06.690: W/System.err(795): at com.example.generatesha384.MainActivity.onCreate(MainActivity.java:24)
07-23 07:29:06.700: W/System.err(795): at android.app.Activity.performCreate(Activity.java:5133)
07-23 07:29:06.730: W/System.err(795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-23 07:29:06.730: W/System.err(795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-23 07:29:06.770: W/System.err(795): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-23 07:29:06.770: W/System.err(795): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-23 07:29:06.770: W/System.err(795): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-23 07:29:06.800: W/System.err(795): at android.os.Handler.dispatchMessage(Handler.java:99)
07-23 07:29:06.800: W/System.err(795): at android.os.Looper.loop(Looper.java:137)
07-23 07:29:06.840: W/System.err(795): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-23 07:29:06.840: W/System.err(795): at java.lang.reflect.Method.invokeNative(Native Method)
07-23 07:29:06.872: W/System.err(795): at java.lang.reflect.Method.invoke(Method.java:525)
07-23 07:29:06.880: W/System.err(795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-23 07:29:06.910: W/System.err(795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-23 07:29:06.910: W/System.err(795): at dalvik.system.NativeStart.main(Native Method)
MainActivity.Java
String seedValue = "This Is MySecure";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView)findViewById(R.id.result);
String normalText = "VIJAY";
String normalTextEnc;
try {
normalTextEnc = AESHelper.encrypt(seedValue, normalText);
String normalTextDec = AESHelper.decrypt(seedValue, normalTextEnc);
String textResult = "Normal Text ::" + normalText
+ " \n Encrypted Value :: " + normalTextEnc
+ " \n Decrypted value :: " + normalTextDec;
text.setText(textResult);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
AESHelper.Java
public class AESHelper {
public static String encrypt(String seed, String cleartext)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes("UTF-8"));
byte[] result = encrypt(rawKey, cleartext.getBytes("UTF-8"));
return toHex(result);
}
public static String decrypt(String seed, String encrypted)
throws Exception {
byte[] rawKey = getRawKey(seed.getBytes("UTF-8"));
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);
Log.d("DEC", "Decrypted: " + decrypted);
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));
}
}
AESHelper.java:52
byte[] decrypted = cipher.doFinal(encrypted);
and AESHelper.java:25
byte[] result = decrypt(rawKey, enc);
After spending a lot of time researching AES encryption/decryption in Java, I put together the following article from all the information that I read on StackOverflow - http://netnix.org/2015/04/19/aes-encryption-with-hmac-integrity-in-java/
Maybe it helps, maybe it doesn't.
这篇关于加密和解密使用AES算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!