Java字符串加密 [英] Java string encrypt

查看:130
本文介绍了Java字符串加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用目标C加密类为我的iPhone应用程序,但我在努力获得相同的功能,从我的Andr​​oid应用程序在JAVA工作。我的加密code是如下:

 的NSString * _secret = @密码;
的NSString * _key = @1428324560542678;

StringEncryption *加密= [[StringEncryption页头]初始化];
的NSData * _secretData = [_secret dataUsingEncoding:NSUTF8StringEncoding]。
CCOptions填充= kCCOptionPKCS7Padding;
NSData的*的EncryptedData = [密码加密:_secretData键:[_键dataUsingEncoding:NSUTF8StringEncoding]填充:放大器;填充]。
 

我试图复制它在JAVA但我得到一个不同的字符串,当我连接code相同的数据。所以,我做错了什么,但我不能弄明白。这是我的JAVA code:

 字节[]键=1428324560542678.getBytes();

密码C = NULL;
            尝试 {
                C = Cipher.getInstance(AES / ECB / PKCS7Padding);
            }赶上(抛出:NoSuchAlgorithmException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(NoSuchPaddingException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

SecretKeySpec K =新SecretKeySpec(键,AES);
            尝试 {
                c.init(Cipher.ENCRYPT_MODE中,k);
            }赶上(InvalidKeyException将E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

    尝试 {
        EditText上tv1passwordText =(EditText上)findViewById(R.id.password);
        字符串password = URLEn coder.en code(tv1passwordText.getText()的toString(),UTF-8);

            字节[]的EncryptedData = c.doFinal(password.getBytes());
 

任何人都可以看到我错了?

根据下面我加的GetBytes,但所产生的字符串仍然不同的意见:

 字节[]键= NULL;
            尝试 {
                关键=1428324560542678.getBytes(UTF-8);
            }赶上(UnsupportedEncodingException E2){
                // TODO自动生成的catch块
                e2.printStackTrace();
            }

            密码C = NULL;
            尝试 {
                C = Cipher.getInstance(AES / ECB / PKCS7Padding);
            }赶上(抛出:NoSuchAlgorithmException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }赶上(NoSuchPaddingException E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

            SecretKeySpec K =新SecretKeySpec(键,AES);
            尝试 {
                c.init(Cipher.ENCRYPT_MODE中,k);
            }赶上(InvalidKeyException将E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }

            尝试 {
                EditText上tv1passwordText =(EditText上)findViewById(R.id.password);

                。byte []的密码= tv1passwordText.getText()的toString()的GetBytes(UTF-8);

                字节[]的EncryptedData = c.doFinal(密码);
 

解决方案

下面是加密和解密的例子:

 公共静态SecretKey的generateKey()抛出抛出:NoSuchAlgorithmException,InvalidKeySpecException {
    返回秘密=新SecretKeySpec(password.getBytes(),AES);
}

公共静态的byte [] encryptMsg(字符串消息,SecretKey的秘密)抛出抛出:NoSuchAlgorithmException,NoSuchPaddingException,InvalidKeyException将,InvalidParameterSpecException,IllegalBlockSizeException,BadPaddingException,UnsupportedEncodingException {
/ *加密该消息。 * /
    密码加密= NULL;
    密码= Cipher.getInstance(AES / ECB / PKCS5Padding);
    cipher.init(Cipher.ENCRYPT_MODE,秘密);
    byte []的密文= cipher.doFinal(message.getBytes(UTF-8));
    返回密文;
}

公共静态字符串decryptMsg(byte []的密文,SecretKey的秘密)抛出NoSuchPaddingException,抛出:NoSuchAlgorithmException,InvalidParameterSpecException,InvalidAlgorithmParameterException使用,InvalidKeyException将,BadPaddingException,IllegalBlockSizeException,UnsupportedEncodingException {

    / *解密消息,由于衍生encContentValues​​和初始化向量。 * /
    密码加密= NULL;
    密码= Cipher.getInstance(AES / ECB / PKCS5Padding);
   cipher.init(Cipher.DECRYPT_MODE,秘密);
    字符串decryptString =新的String(cipher.doFinal(密文),UTF-8);
    返回decryptString;
}
 

要加密:

  SecretKey的秘密= EncUtil.generateKey();
    EncUtil.encryptMsg(小于字符串加密>中密))
 

解密

  EncUtil.decryptMsg(小于byte []的>中密))
 

I am using the encryption class in Objective C for my iPhone app but I am struggling to get the same functionality working in JAVA from my android app. My encryption code is below:

NSString * _secret = @"password";
NSString * _key = @"1428324560542678";

StringEncryption *crypto = [[StringEncryption alloc] init];
NSData *_secretData = [_secret dataUsingEncoding:NSUTF8StringEncoding];
CCOptions padding = kCCOptionPKCS7Padding;
NSData *encryptedData = [crypto encrypt:_secretData key:[_key dataUsingEncoding:NSUTF8StringEncoding] padding:&padding];

I have tried to replicate it in JAVA but I get a different string when I encode the same data. So I am doing something wrong but I can't figure it out. Here is my JAVA code:

byte[] key = "1428324560542678".getBytes();

Cipher c = null;
            try {
                c = Cipher.getInstance("AES/ECB/PKCS7Padding");
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

SecretKeySpec k =  new SecretKeySpec(key, "AES");
            try {
                c.init(Cipher.ENCRYPT_MODE, k);
            } catch (InvalidKeyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    try {
        EditText tv1passwordText = (EditText) findViewById(R.id.password);
        String password = URLEncoder.encode(tv1passwordText.getText().toString(), "UTF-8");

            byte[] encryptedData = c.doFinal( password.getBytes());

Can anyone see where I am going wrong?

Based on the comments below I added getBytes but the strings produced are still different:

byte[] key = null;
            try {
                key = "1428324560542678".getBytes("UTF-8");
            } catch (UnsupportedEncodingException e2) {
                // TODO Auto-generated catch block
                e2.printStackTrace();
            }

            Cipher c = null;
            try {
                c = Cipher.getInstance("AES/ECB/PKCS7Padding");
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (NoSuchPaddingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            SecretKeySpec k =  new SecretKeySpec(key, "AES");
            try {
                c.init(Cipher.ENCRYPT_MODE, k);
            } catch (InvalidKeyException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            try {
                EditText tv1passwordText = (EditText) findViewById(R.id.password);

                byte[] password = tv1passwordText.getText().toString().getBytes("UTF-8");

                byte[] encryptedData = c.doFinal(password);

解决方案

Here is a sample of encryption and decryption:

public static SecretKey generateKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    return secret = new SecretKeySpec(password.getBytes(), "AES");
}

public static byte[] encryptMsg(String message, SecretKey secret) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
/* Encrypt the message. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
    return cipherText;
}

public static String decryptMsg(byte[] cipherText, SecretKey secret) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {

    /* Decrypt the message, given derived encContentValues and initialization vector. */
    Cipher cipher = null;
    cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
   cipher.init(Cipher.DECRYPT_MODE, secret);
    String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");
    return decryptString;
}

To encrypt:

    SecretKey secret = EncUtil.generateKey();
    EncUtil.encryptMsg(<String to Encrypt>, secret))

to decrypt

    EncUtil.decryptMsg(<byte[]>, secret))

这篇关于Java字符串加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆