java漢明碼解密

來源:互聯網
上載者:User

標籤:out   throws   ascii   字串   異或   rtu   new   tun   cep   

前言:最近由於學校的比賽,接觸了漢明碼解密,在這裡給大家分享一下(只用了幾種比較簡單的方法)。

當時題目是 使用給的原始資訊由6個ASCII碼(7bit)字元構成,經過漢明碼編碼後得到6位元組的資料,使用該資料作為紅外線控制碼。具體代碼如下(因為是自學所以編碼可能不太規範):

  1 1 public class asf {  2   2     static String rtun="ABCASD";  3   3     static int dx=1,j=0,b=0,z=0,m=0,k=0,b1=0,b2=0,b3=0;  4   4     static String ch="";  5   5     static String a1="";  6   6     static String a2="";  7   7     static String a3="";  8   8     static String ch1="";  9   9     static String zhongjian; 10  10     static char zifu; 11  11     static char a=‘1‘; 12  12     static int h=1; 13  13 public static void main(String[] args) throws Exception { 14  14     System.out.print(a1); 15  15     for(int i=0;i<rtun.length();i++){ 16  16         char c=rtun.charAt(i); 17  17         //判斷字串中的大寫字母 18  18         if(((int)c>=65)&&((int)c<=90)){ 19  19             a1=a1+(char)c; 20  20             b1++; 21  21         } 22  22         //判斷是否為字串中的小寫字母 23  23         else if(((int)c>=97)&&((int)c<=122)){ 24  24             b2++; 25  25             a2=a2+(char)c; 26  26         }//剩下的字元 27  27         else{ 28  28             a3=a3+(char)c; 29  29             b3++; 30  30         } 31  31     } 32  32     for(int j2=0;j2<a1.length();j2++){ 33  33         //轉變為位元 34  34          35  35         /* 進行拼接 36  36          * 字串之間的拼接 37  37          * 注意: 38  38          *   低位是第一個   高位是最後一個 39  39          */ 40  40         zifu = a1.charAt(j2); 41  41         zhongjian =(String)Integer.toBinaryString(zifu); 42  42         //System.out.print(zhongjian); 43  43         //假如是數位話 前邊補零 44  44         while(zhongjian.length()<7) 45  45         { 46  46             zhongjian=‘0‘+zhongjian; 47  47         } 48  48         ch1=ch1+zhongjian; 49  49 }     50  50     //System.out.print(ch1); 51  51     for(int i=1;i<ch1.length()+7;i++){ 52  52     if(i==dx){ 53  53             dx = dx*2; 54  54             zifu=‘ ‘; 55  55         }else{ 56  56             zifu = ch1.charAt(j); 57  57             j++; 58  58         } 59  59         ch=ch+zifu; 60  60     } 61  61     //System.out.print("/n"); 62  62     //System.out.print(ch); 63  63     //System.out.print("/n"); 64  64     //把“資料”行值為1的位置值進行二進位編碼異或 65  65     int hu=0xff; 66  66     for(int i=0;i<ch.length();i++){ 67  67         zifu=ch.charAt(i); 68  68         if(a==zifu){ 69  69             int[] shuju=new int[100];  70  70             shuju[z] = i+1; 71  71             System.out.println(shuju[z]); 72  72             if(h==1) 73  73             { 74  74                 //System.out.print(i); 75  75                 hu=shuju[z]; 76  76                 z++; 77  77                 h--; 78  78             } 79  79             else 80  80             { 81  81             hu = hu ^ shuju[z]; 82  82             //System.out.println(hu); 83  83             z++; 84  84             } 85  85         } 86  86          87  87     } 88  88     //得來的資料進行二進位編碼 89  89     String shu=""; 90  90     shu = Integer.toBinaryString(hu); 91  91     while(shu.length()!=6) 92  92     { 93  93         shu = ‘0‘ + shu; 94  94     } 95  95     System.out.println(shu); 96  96     //對效驗碼進行逆順序排列 97  97     String nima=""; 98  98     for(int i=shu.length()-1;i>=0;i--){ 99  99         nima=nima + shu.charAt(i);100 100     }101 101     //把效驗碼填入到資料中102 102     String numb="";103 103     for(int i=0;i<ch.length();i++){104 104         if(ch.charAt(i)==‘ ‘){105 105             numb = numb + nima.charAt(m);106 106             m++;107 107         }else{108 108             numb = numb + ch.charAt(i);109 109         }110 110     }111 111     System.out.println(numb);112 112     char yu;113 113     String stry="";114 114     int[] str= new int[6];115 115         for(int i=0;i<numb.length();i++){116 116         yu=numb.charAt(i);117 117         stry=stry + yu;118 118         if((i+1)%8==0){119 119             //把字串二進位變為十進位120 120             String asd = Integer.valueOf("0101",2).toString();121 121             int ddy =  Integer.parseInt(asd);122 122             //十進位轉化為十六進位123 123             String ssa = Integer.toHexString(ddy);124 124             //int n = OxStringtoInt(ssa);125 125            126 126         }127 127     }128 128   }129 129 }

 

java漢明碼解密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.