密码块链接/密文窃取java
为了更好地理解CBC和CTS,我尝试实现我自己的类,该类可以在不使用java内置CTS模式的情况下进行加密和解密。Im使用AES包装类作为底层算法,但使用CTS作为操作模式。到目前为止,我一直在研究加密方法,但不确定从那里走到哪里。我不确定如何在CTS模式结束时实现块交换 以下是我到目前为止为我的加密方法编写的代码(不要担心AES类,它可以100%工作):密码块链接/密文窃取java,java,encryption,cryptography,aes,Java,Encryption,Cryptography,Aes,为了更好地理解CBC和CTS,我尝试实现我自己的类,该类可以在不使用java内置CTS模式的情况下进行加密和解密。Im使用AES包装类作为底层算法,但使用CTS作为操作模式。到目前为止,我一直在研究加密方法,但不确定从那里走到哪里。我不确定如何在CTS模式结束时实现块交换 以下是我到目前为止为我的加密方法编写的代码(不要担心AES类,它可以100%工作): 静态字节[]加密(字节[]ptBytes,javax.crypto.SecretKey密钥,字节[]IV){ 字节[]ct; 字节[]pt;
静态字节[]加密(字节[]ptBytes,javax.crypto.SecretKey密钥,字节[]IV){
字节[]ct;
字节[]pt;
字节[]ptBlock,ctBlock;
//将阵列填充到适当的长度
pt=Arrays.copyOf(ptBytes,(int)(Math.ceil((ptBytes.length)/16)*16);
//ctBlock=一块密文
ctBlock=新字节[16];
//使ct成为填充pt的长度
ct=新字节[pt.长度];
//加密吗
//我代表当前的明文/密文块
对于(int i=1;i<(int)((Math.ceil((ptBytes.length)/16)+1));i++){
如果(i==1){
//使ptBlock成为整个纯文本的第一个块
ptBlock=Arrays.copyOfRange(pt,0,(i*16)-1);
//因为i=1,所以使用IV执行XOR以获得新的纯文本
对于(int j=0;j
如果有人能给出一些关于如何完成这个方法的见解,那就太好了,因为我仍然在学习CBC/CTS的细节
谢谢
static byte[] encrypt(byte[] ptBytes, javax.crypto.SecretKey key, byte[] IV){
byte [] ct;
byte [] pt;
byte [] ptBlock, ctBlock;
//pad the array to proper length
pt = Arrays.copyOf(ptBytes, (int) (Math.ceil( ( ptBytes.length )/16)*16) );
//ctBlock = one block of cipher text
ctBlock = new byte [16];
//make ct the length of the padded pt
ct = new byte [pt.length];
//do the encryption
//i is for the current block of plain / cipher text we are on
for( int i = 1; i < (int) ((Math.ceil( ( ptBytes.length )/16)+1)); i++){
if( i == 1 ){
//make ptBlock the first block of the entire plain text
ptBlock = Arrays.copyOfRange(pt, 0, (i*16)-1);
//since i = 1 do the XOR to get new plain text with IV
for (int j = 0; j < ptBlock.length - 1; j++){
ptBlock[j] = (byte)(ptBlock[j] ^ IV[j]);
}
//now time to do the encryption between the current block of plain text and the key
try {
ctBlock = AES.encrypt(ptBlock, key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//now put the cipher text block we just got into the final cipher text array
for( int k = 0; k < ctBlock.length; k++){
ct[k] = ctBlock[k];
}
}
else{
//make ptBlock the current number block of entire plain text
ptBlock = Arrays.copyOfRange(pt, (i-1)*16, (i*16)-1);
//now XOR the plain text block with the prior cipher text block
for(int j = 0; j < ptBlock.length - 1; j++){
ptBlock[i] = (byte) (ptBlock[j] ^ ctBlock[j]);
}
//now time to do the encryption between the current block of plain text and the key
try {
ctBlock = AES.encrypt(ptBlock, key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//now put the cipher text block we just got into the final cipher text array
for( int k = (i-1)*16; k < (i*16)-1; k++){
ct[k] = ctBlock[k-16];
}
}
}
return ct;
}