一个加密数字和逗号且减少空间的算法

注意本算法只能处理由 0,1,2,3,4,5,6,7,8,9 和逗号 这是个字符组成的内容, 如要支持 小数点,回车等,可做简单的扩展。
本算法将 0 到  9 , 加上 逗号, 共 11 个字符, 二个字符一组作为排列组合, 共有 121 总排列,用一个字节表示, 这样理论上
 
n 字符的串,可变成 n/2 个长度的字符, 然后将这 n/2 长度的字节数据做 base64 编码, 长度又增加为 4/3 陪, 所以总的长度为:
 
n/2 * 4/3 =2n/3 , 即长度大概缩小了 三分之一 字符数,内容也得到了初步的加密。 算法可逆。
程序如下,请大家评测:
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;  
public class ZipTest {
 
public static String decode(byte[] b){
String ret="";
for(int i=0;i<b.length;i++){
ret+=decode(b[i]);
}
return ret;
}
public static String decode(byte b){
int i=b;
if(i<0){
i+=256;
}
if(i>=200){
i=i-200;
return String.valueOf(toChar(i));
}
int h=i/11;
int l=i%11;
return String.valueOf(toChar(h))+String.valueOf(toChar(l)); 
}
public static byte[] encode(String msg){
int len=msg.length()/2;
if(msg.length()%2>0){
len++;
}
byte []e=new byte[len];
 
int i=0;
int k=0;
while(i<msg.length() && i+1<msg.length()){
e[k]=encode(msg.charAt(i),msg.charAt(i+1)); 
i+=2;
k++;
}
if(i<msg.length()){
e[k]=encode(msg.charAt(i)); 
}
return e;
}
public static int toInt(char c){
if(c==',') return 0;
return (c-'0')+1;
}
public static char toChar(int i){
if(i==0) return ',';
return (char)(i+'0'-1);
}
public static byte encode(char c1,char c2){
int h=toInt(c1);
int l=toInt(c2);
int i=h*11+l;
return (byte)i;
}
public static byte encode(char c1){
int h=toInt(c1);  
int i=200+h;
return (byte)i;
}
public static String replaceChars(String ret) { 
ret = ret.replace('+', '_');
ret = ret.replace('/', '.');
ret = ret.replace('=', '-');
return ret;
}
public static String restoreChars(String ret) { 
ret = ret.replace('_', '+');
ret = ret.replace('.', '/');
ret = ret.replace('-', '=');
return ret;
}
public static void main(String []args)throws Exception { 
//<月亮智商人民>下载地址http://wap.music.ovi.com.cn/rt/d/?p=44字符
String msg="10234567,"+"12345678901,"+System.currentTimeMillis()+",0,0,0";
System.out.println(msg+"\t"+msg.length());
System.out.println();
byte []ec=encode(msg);
for(int i=0;i<ec.length;i++){
System.out.print((int)ec[i]+",");
}
System.out.println();
BASE64Encoder base64=new BASE64Encoder();
        String ret=replaceChars(base64.encode(ec));  
System.out.println(ret);
BASE64Decoder de=new BASE64Decoder();
byte []m=de.decodeBuffer(restoreChars(ret));
for(int i=0;i<m.length;i++){
System.out.print((int)m[i]+",");
}
System.out.println(); 
 
System.out.println(decode(m));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
仿射密码是一种基于数学原理的加密算法,它可以通过随机生成密钥a、b来加密和解密任意满足条件的数据。下面是一个基于C语言实现的仿射加密算法的示例代码: ```c #include <stdio.h> // 求两数的最大公约数 int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } // 求a模N=26的逆 int exgcd(int a, int n) { int p = a, q = n; int x = 0, y = 1; int z = q / p; while (p != 1 && q != 1) { int t = p; p = q % p; q = t; t = y; y = x - y * z; x = t; z = q / p; } y %= n; if (y < 0) y += n; return y; } // 加密函数 void encrypt(char *plaintext, char *ciphertext, int a, int b) { int i; for (i = 0; plaintext[i] != '\0'; i++) { if (plaintext[i] >= 'a' && plaintext[i] <= 'z') { ciphertext[i] = (a * (plaintext[i] - 'a') + b) % 26 + 'a'; } else if (plaintext[i] >= 'A' && plaintext[i] <= 'Z') { ciphertext[i] = (a * (plaintext[i] - 'A') + b) % 26 + 'A'; } else { ciphertext[i] = plaintext[i]; } } ciphertext[i] = '\0'; } // 解密函数 void decrypt(char *ciphertext, char *plaintext, int a, int b) { int i, a_inv = exgcd(a, 26); for (i = 0; ciphertext[i] != '\0'; i++) { if (ciphertext[i] >= 'a' && ciphertext[i] <= 'z') { plaintext[i] = (a_inv * (ciphertext[i] - 'a' - b + 26)) % 26 + 'a'; } else if (ciphertext[i] >= 'A' && ciphertext[i] <= 'Z') { plaintext[i] = (a_inv * (ciphertext[i] - 'A' - b + 26)) % 26 + 'A'; } else { plaintext[i] = ciphertext[i]; } } plaintext[i] = '\0'; } int main() { char plaintext[100], ciphertext[100], decrypted[100]; int a, b; printf("请输入密钥a和b(用逗号隔开):"); scanf("%d,%d", &a, &b); if (gcd(a, 26) != 1) { printf("密钥a必须与26互质!\n"); return 0; } printf("请输入明文:"); scanf("%s", plaintext); encrypt(plaintext, ciphertext, a, b); printf("加密后的密文为:%s\n", ciphertext); decrypt(ciphertext, decrypted, a, b); printf("解密后的明文为:%s\n", decrypted); return 0; } ``` 在这个示例代码中,我们定义了两个函数encrypt和decrypt,分别用于加密和解密数据。在加密时,我们将明文中的每个字符都转换为一个数字,然后使用公式(a * x + b) % 26来计算密文中的每个字符。在解密时,我们使用公式(a' * (y - b) % 26)来计算明文中的每个字符,其中a'是a模26的逆元。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值