当前位置: 首页 > 知识库问答 >
问题:

使用AES算法的加密和解密

颜高朗
2023-03-14

我正在为我的应用程序制作加密/解密模块。我按照这个教程

它没有给出任何错误,也没有显示输出。

日志文件

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

还有AESHelper.java:25

        byte[] result = decrypt(rawKey, enc);

共有1个答案

冯星阑
2023-03-14

在花了很多时间研究Java中的AES加密/解密之后,我从我在 StackOverflow 上读到的所有信息中整理了以下文章 - http://netnix.org/2015/04/19/aes-encryption-with-hmac-integrity-in-java/

也许有帮助,也许没有。

 类似资料:
  • 本文向大家介绍原生js的RSA和AES加密解密算法,包括了原生js的RSA和AES加密解密算法的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了js中RSA和AES加密解密详细代码,供大家参考,具体内容如下 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 问题内容: 我迅速编写了一个应用程序,我需要AES加密和解密功能,我从另一个.Net解决方案中接收了加密数据,但是我找不到解决办法。 这是我的.net加密: 我需要迅速解密功能。 问题答案: 我找到了解决方案,它是一个很好的库。 跨平台256位AES加密/解密。 此项目包含在所有平台(C#,iOS,Android)上均可使用的256位AES加密的实现。关键目标之一是通过简单的实现使AES在所有平台

  • 问题内容: 我正在尝试使用PyCrypto构建两个接受两个参数的函数:消息和密钥,然后对消息进行加密/解密。 我在网络上找到了几个链接可以帮助我,但是每个链接都有缺陷: 在codekoala上的此代码使用了os.urandom,PyCrypto不建议这样做。 此外,我不能保证给函数的键具有预期的确切长度。我该怎么做才能做到这一点? 另外,有几种模式,推荐哪种?我不知道该怎么用:/ 最后,IV到底是

  • 我试图使用PyCrypto构建两个函数,它们接受两个参数:消息和密钥,然后加密/解密消息。 我在网上找到了几个帮助我的链接,但每一个都有缺陷: 编辑:删除了代码部分,因为它不安全。

  • 我想加密test.txt文件我正在使用这个java类进行加密和解密。在我的目录中,我有三个文件private.txt用于保存私钥,public.txt用于公钥,test.txt用于加密。

  • 我之所以问这个问题,是因为两天来我读了很多关于crypto AES加密的帖子,就在我以为我得到了它的时候,我意识到我根本没有得到它。 这个帖子是最接近我的问题,我有完全相同的问题,但它没有得到回答: CryptoJS AES加密与JAVA AES解密值不匹配 我得到的是已经加密的字符串(我得到的代码只是为了看看他们是怎么做的),所以修改加密方式不是一个选项。这就是为什么所有类似的问题对我来说都不是