AES加密J2ME

编程入门 行业动态 更新时间:2024-10-28 06:27:28
本文介绍了AES加密J2ME的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图做的AES加密j2me.I用几乎相同的code的Andr​​oid和它的正常工作there.Following是code块。我得到空的输出

I am trying to do AES encryption in j2me.I used almost same code for android and it's working fine there.Following is the block of code. I'm getting null as output

package cartoon; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class MCrypt { private String iv = "0123456789abcdef";// iv private IvParameterSpec ivspec; private SecretKeySpec keyspec; private Cipher cipher; private String SecretKey = "fedcba9876543210";// secretKey public MCrypt() { ivspec = new IvParameterSpec(iv.getBytes(), 0, iv.getBytes().length); keyspec = new SecretKeySpec(SecretKey.getBytes(), 0, iv.getBytes().length, "AES"); } String Decrypt(String text) throws Exception { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] results = null; int results1 = cipher.doFinal(Base64.decode(text), 0, Base64.decode(text).length, results, 0); System.out.println("String resultssssssssssssss " + results1); return new String(results, "UTF-8"); } String Encrypt(String text) throws Exception { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); System.out.println("String input : " + text); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] results = null; int results1 = cipher.doFinal(text.getBytes(), 0, text.getBytes().length, results, 0); return Base64.encode(results); } }

印刷效果

MCrypt mcrypt = new MCrypt(); try { encrypted = mcrypt.Encrypt("id=450"); decrypted = new String(mcrypt.Decrypt(encrypted)); System.out.println("Encrypted Text : " + encrypted); System.out.println("Decrypted Text : " + decrypted); } catch (Exception e) { e.printStackTrace(); }

我在哪里去了?

Where am I going wrong?

推荐答案

请尝试以下

您应该初始化字节数组第一个适当

You should initialize the byte array first appropriately

String Decrypt(String text) throws Exception { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); // here byte[] results = cipher.doFinal(Base64.decode(text)); int results1 = cipher.doFinal(Base64.decode(text), 0, Base64.decode(text).length, results, 0); System.out.println("String resultssssssssssssss " + results1); return new String(results, "UTF-8"); } String Encrypt(String text) throws Exception { cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); System.out.println("String input : " + text); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); // and here byte[] results = cipher.doFinal(text.getBytes()); int results1 = cipher.doFinal(text.getBytes(), 0, text.getBytes().length, results, 0); return Base64.encode(results); }

更多推荐

AES加密J2ME

本文发布于:2023-11-27 22:49:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639928.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:AES   J2ME

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!