Java的ANSI X923填充 [英] Java ANSI X923 Padding

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

问题描述

我有一个新的应用程序用Java编写的,需要读取加密值了分贝。的问题是,在db所有值是通过使用ANSI x923填充方案.NET code加密。我做了一些研究,它看起来并不像Java的TripleDes的图书馆有一种方法来指定这个填充方案。我想知道是否有人知道,如果我是正确的,而ANSI x923不支持java的,或者是否有办法让这个工作。

I have a new application written in java that needs to read encrypted values out of a db. The problem is that all the values in the db were encrypted by .NET code that uses the ANSI x923 padding scheme. I have done some research and it doesn't look like the Java TripleDes libraries have a way to specify this padding scheme. I was wondering if anyone knows if I am correct and the ANSI x923 isn't supported in java, or if there is a way to get this working.

推荐答案

如果您使用充气城堡JCE,它支持X923填充。你可以得到密码是这样的(假设你使用CBC模式),

If you use Bouncy Castle JCE, it supports X923 padding. You can get cipher like this (assuming you use CBC mode),

cipher = Cipher.getInstance("DESede/CBC/X9.23PADDING");

我不认为Sun的JCE支持。但是,你可以简单地将其解密不填充和消除填充自己。与X9.23,最后一个字节是加入填充的数量。所以,你可以做这样的事情,

I don't think Sun's JCE supports it. But you can simply decrypt it without padding and remove padding yourself. With X9.23, the last byte is the number of padding added. So you can do something like this,

cipher = Cipher.getInstance("DESede/CBC/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, key, ivSpec);
int outSize = cipher.getOutputSize(cipherText.length);  
plainText = new byte[outSize];
length = cipher.update(cipherText, plainText, 0);
cipher.doFinal(plainText, length);

//Remove padding
int newLen = plainText.length - (plainText[plainText.length-1] & 0xFF);
byte[] data = new byte[newLen];
System.arraycopy(plainText, 0, data, 0, newLen);

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

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