aes/cbc/nop添加加密和解密

avwztpqn  于 2021-07-11  发布在  Java
关注(0)|答案(1)|浏览(278)

这个问题在这里已经有答案了

java aes/cbc解密后初始字节不正确(10个答案)
上个月关门了。
我正在使用一个微控制器与nfc,所以我需要发送加密的数据,这就是为什么我使用aes/cbc/nopadding,而我在安卓应用程序工作,我有一个加密和解密的问题,这是我的代码工作

String message="(*my_Log*y7VRMh5Wau80bRebLh/JaHMru/0Sj05E+lKUvT6d8Rw=*1604137444*19)111111111112";
    textView.setText("before encp:- "+message);
    try {
        String data=encrypt(message,"Thats my Kung Fu");
        Log.d("data after encode:-",data);
        textView.append("\n after encode:- \n"+data);
        String data2=decrypt(data,"Thats my Kung Fu");
        Log.d("data after decode:-",data2);
        textView.append("\n after decode:- \n"+data2);

    } catch (Exception e) {
        e.printStackTrace();
    }

}

@RequiresApi(api = Build.VERSION_CODES.O)
private String decrypt(String data, String key) throws Exception {
    SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher=null;
    byte[] ivBytes = new byte[16];
    cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
    cipher.init(Cipher.DECRYPT_MODE,skey,new IvParameterSpec(ivBytes));
    byte[] decodeval=Base64.getDecoder().decode(data);
    byte[] decval= cipher.doFinal(decodeval);
    return new String(decval);

}

@RequiresApi(api = Build.VERSION_CODES.O)
private String encrypt(String message, String key) throws Exception{
    byte[] encval=null;
    SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE,skey);
    encval=cipher.doFinal(message.getBytes());
    String encryptedValue = Base64.getEncoder().encodeToString(encval);
    return encryptedValue;

}

这是我的log:- d/data 之后 encode:-:zjsew6h+abzfkwna/pqpdzumnfhy0kmz2lxf23tdavim1c5l5loimagwxg6nrt0udciy/xeaehketf32+5wvq420/tsljaxer7ynj5+hgfmmo1qjhrwb8tzyuzmivokug
数据/数据 之后 decode:-: �5吨�����u0级³u%wau80breblh/jahmru/0sj05e+lkuvt6d8rw=160413744419)11111111 2
不完全解码也在编码前字符串大小是80编码后字符串大小是128和十年后它的agin 80

a0x5cqrl

a0x5cqrl1#

@RequiresApi(api = Build.VERSION_CODES.O)
private String encrypt(String message, String key) throws Exception{
    byte[] encval=null;
    SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    byte[] ivBytes = new byte[16];
    cipher.init(Cipher.ENCRYPT_MODE,skey,new IvParameterSpec(ivBytes));   //iv here
    encval=cipher.doFinal(message.getBytes());
    String encryptedValue = Base64.getEncoder().encodeToString(encval);
    return encryptedValue;

}

你也必须提供静脉注射

相关问题