当前位置: 首页 > 面试题库 >

Java字符串加密

仰翰采
2023-03-14
问题内容

我在我的iPhone应用程序中使用Objective C中的加密类,但是我在努力通过Android应用程序在JAVA中使用相同的功能。我的加密代码如下:

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

我试图在JAVA中复制它,但是当我编码相同的数据时却得到了不同的字符串。所以我做错了事,但我无法弄清楚。这是我的JAVA代码:

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

谁能看到我要去哪里错了?

根据以下注释,我添加了getBytes,但是生成的字符串仍然不同:

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

问题答案:

这是加密和解密的示例:

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

加密:

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

解密

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


 类似资料:
  • 这是正确的吗?可以说“它是加密的”吗?我的期望是得到像“werwerwer”这样的字面字符串

  • 问题内容: 有没有简单的方法可以向现有字符串中添加n次字符或另一个字符串?我无法找到任何东西,等等。 问题答案: 您可以使用Java 8流API来执行此操作。以下代码从创建字符串:

  • 问题内容: 我在Java程序中有两个字符串,我想以某种方式混合以形成两个新字符串。为此,我必须从每个字符串中提取一些构成字符并将其添加以形成新的字符串。我有这样的代码(this.eka和this.toka是原始字符串): 我正在获取.charAt(x)部分的数字,那么如何将字符转换为字符串? 问题答案: 只使用永远使用代替 例如,当位置arent不是固定值而变量 其中x,y,z是保存从中提取位置的

  • 我正在编写一个程序,以这种方式加密一个给定的字符串: 如果我们有一个整数V和一个只有元音的数组v={a,e,I,o,u}如果字符串的字母是一个元音,那么用它前面V个位置的元音替换它,只考虑元音的数组(不是整个字母表!). 要明确: 所以为了解决我的问题,我写了: 代码采用字符串的每个元素来验证它是否是元音,然后如果它是元音,则将字符串的考虑元素替换为 V 位置之前的元音。 如果字符串只有元音 i,

  • V1.1.1新增 <?php $string='666666'; $string=sp_authencode($string);//加密字符串 echo $string;//输出加密后的字符串 ?>

  • 问题内容: 如何使用AES加密整个字符串。我下面的代码仅加密最多识别的第一个空格:(。我该如何解决这个问题?谢谢 编辑 OMG,我不愿意相信这一点,但我怎么可能会这样想:(因为我的扫描仪接下来是nextLine而不是nextLine,所以让我整日感到困扰是多么令人困扰,直到现在我才真正想到要进行检查。问题已解决:)谢谢大家 问题答案: 我没有看到任何你的代码错误,除了尝试打印任意使用。尝试以下尺寸